簡體   English   中英

在 Ubuntu 上安裝 Node.js

[英]Install Node.js on Ubuntu

我正在嘗試在Ubuntu 12.10 (Quantal Quetzal) 上安裝 Node.js,但終端向我顯示有關丟失包的錯誤。 我試過這個:

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs npm

但是當我來到最后一行時sudo apt-get install nodejs npm顯示這個錯誤:

Failed to install some packages. This may mean that
you requested an impossible situation or if you are using the distribution
distribution that some required packages have not yet been created or been
been moved out of Incoming.
The following information may help to resolve the situation:
The following packages have unmet dependencies:
nodejs: Conflicts: npm
E: Failed to correct problems, you have held broken packages.

然后我卸載了ppa:chris-lea/node.js並嘗試了第二個選項:

sudo apt-get install node.js
sudo apt-add-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs npm

同樣的錯誤,終端npm is the latest version ,但它也顯示了我在頂部顯示的文本。 我認為問題出在ppa:chris-lea/node.js ,但我不知道如何解決。

只需按照此處給出的說明進行操作:

安裝示例:

 sudo apt-get install python-software-properties python g++ make sudo add-apt-repository ppa:chris-lea/node.js sudo apt-get update sudo apt-get install nodejs

它在當前穩定的 Ubuntu 上安裝當前穩定的節點。 Quantal (12.10) 用戶可能需要安裝 software-properties-common 軟件包才能使add-apt-repository命令工作: sudo apt-get install software-properties-common

從 Node.js v0.10.0 開始,Chris Lea 的 repo 中的 nodejs 包包括 npm 和 nodejs-dev。

不要給sudo apt-get install nodejs npm 只需sudo apt-get install nodejs

從今天開始,您可以簡單地安裝它:

sudo apt-get install nodejs

在最新版本的 Node.js 中,npm 會自動與 Node.js 一起安裝。 當您在終端中鍵入node --versionnpm --version時,您會看到什么?

您也可以使用 npm 本身升級 npm:

[sudo] npm install -g npm

我的apt-get又舊又壞了,所以我不得不從源代碼安裝。 這對我有用:

# Get the latest version from nodejs.org. At the time of this writing, it was 0.10.24
curl -o ~/node.tar.gz http://nodejs.org/dist/v0.10.24/node-v0.10.24.tar.gz
cd
tar -zxvf node.tar.gz
cd node-v0.6.18
./configure && make && sudo make install

這些步驟主要取自Joyent 的安裝 wiki

這是輕松安裝 Node.js 的最佳方式。 這也適用於Ubuntu 12.04 (Precise Pangolin)、 Ubuntu 13.04 (Raring Ringtail) 和Ubuntu 14.04 (Trusty Tahr)

添加 Node.js 存儲庫

[sudo] apt-get install python-software-properties
[sudo] apt-add-repository ppa:chris-lea/node.js
[sudo] apt-get update

Node.js 安裝

[sudo] apt-get install nodejs

現在檢查 Node.js 版本

node -v

輸出

v0.10.20

這個命令應該安裝 npm

npm install

檢查 npm 版本

npm -v

輸出

1.4.3

如果由於某種原因,如果你看到 npm 沒有安裝,你可以嘗試運行:

[sudo] apt-get install npm

要更新 npm,您可以嘗試運行:

[sudo] npm install -g npm

現在您可以簡單地安裝

sudo apt-get install nodejs
sudo apt-get install npm

確保您已預安裝 Python 和 C 解釋器/編譯器。 如果沒有,請執行:

sudo apt-get install python g++ make

您可以使用nvm安裝 Node.js。 它允許您使用不同的版本而不會發生沖突。

wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.19.0/install.sh | bash

nvm install v0.10.33

只需將 nvm 用於 Node.js 版本控制 - nvm

只需按照此處的官方說明在 Ubuntu 上安裝

安裝說明

Node.js LTS (截至 01/2022 為 v16.x):

curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs

Node.js 當前(截至 01/2022 為 v17.x):

curl -fsSL https://deb.nodesource.com/setup_current.x | sudo -E bash -
sudo apt-get install -y nodejs

Node.js v17.x

curl -fsSL https://deb.nodesource.com/setup_17.x | sudo -E bash -
sudo apt-get install -y nodejs

Node.js v16.x

curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt-get install -y nodejs

Node.js v14.x

curl -fsSL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs

Node.js v12.x

curl -fsSL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt-get install -y nodejs

可選:安裝構建工具

要從 npm 編譯和安裝本機插件,您可能還需要安裝構建工具:

sudo apt-get install -y build-essential

您也可以像這樣從源代碼編譯它

git clone git://github.com/ry/node.git
cd node
./configure
make
sudo make install

在此處查找詳細說明http://howtonode.org/how-to-install-nodejs

sudo apt-get install g++ curl libssl-dev apache2-utils
sudo apt-get install git-core
git clone git://github.com/ry/node.git
cd node
./configure
make
sudo make install

http://jstricks.com/install-node-js/

請遵循 NodeSource 提供的說明,該說明致力於為 Node.js 創建一個可持續的生態系統。

對於 Node.js >= 4.X

# Using Ubuntu
curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodejs

# Using Debian, as root
curl -sL https://deb.nodesource.com/setup_4.x | bash -
apt-get install -y nodejs

Node.js 在所有當前支持的 Ubuntu 版本中都可以作為snap包使用。 具體到 Node.js,開發人員可以從一個或多個當前支持的版本中進行選擇,並直接從 NodeSource 獲取定期自動更新。 Node.js 版本 6、8、9、10、11、13、14、15、16、17 和 18 目前可用,Snap Store 會在 Node.js 發布后的幾小時或幾分鍾內更新。

可以使用單個命令安裝 Node.js,例如:

sudo snap install node --classic --channel 11/stable

節點快照可以通過命令node訪問,例如:

$ node -v
v11.5.0

最新版本的 npm 將作為節點快照的一部分安裝。 npm 應該在 node repl 之外運行,在你的普通 shell 中。 安裝節點快照后,運行以下命令以啟用 npm 更新檢查:

sudo chown -R $USER:$(id -gn $USER) /home/<b>your-username</b>/.config

將上述命令中的your-username替換為您自己的用戶名。 然后運行npm -v檢查 npm 的版本是否是最新的。 例如,我檢查了 npm 是否是最新的,使用命令npm list yarn檢查已安裝的名為 yarn 的包的版本,然后使用命令npm update yarn將現有的 yarn 包更新到最新版本

用戶可以隨時在 Node.js 的版本之間切換,而無需涉及其他工具,例如 nvm(Node Version Manager),例如:

sudo snap refresh node --channel=11/stable

用戶可以通過切換來測試可以從最新的邊緣通道安裝的 Node.js 的前沿版本:

sudo snap switch node --edge

這種方法只推薦給那些願意參與上游測試和錯誤報告的用戶。

Node.js LTS 計划

發布 地位 代碼名稱 初始發行 LTS 開始 維護開始 維護結束
6.x 停產 2016-04-26 2016-10-18 2018-04-30 2019-04-30
7.x 停產 2017-05-30 2017-06-30
8.x 停產 2016-10-25 2017-10-31 2019-01-01 2019-12-31
9.x 停產 2017-10-01 2018-06-30
10.x 停產 杜比尼姆 2018-04-24 2018-10-30 2020-05-19 2021-04-30
11.x 停產 2018-10-23 2019-06-01
12.x 維護 LTS 2019-04-23 2019-10-21 2020-11-301 2022-04-30
13.x 停產 2019-10-22 2020-06-01
14.x 維護 LTS 2020-04-21 2020-10-27 2021-10-30 2023-04-30
16.x 主動 LTS 2021-04-20 2021-10-26 2022-10-18 2024-04-30
17.x 當前的 2021-10-19 2022-04-01 2022-06-01
18.x 當前的 2022-04-19 2022-10-25 2023-10-18 2025-04-30

我個人是這樣做的:

sudo apt-get install python g++ make
wget http://nodejs.org/dist/node-latest.tar.gz
tar xvfvz node-latest.tar.gz
cd node-v0.12.0
./configure
make
sudo make install

如果要安裝特定版本,請從 Node.js 站點下載所需版本並執行最后的樹步驟。

我強烈建議不要使用來自分發市場的默認 Node.js 包,因為它可能已經過時(即,在 Ubuntu 市場上寫這篇文章時的當前版本是 v0.10.25,與最新版本相比太過時了(v0.12.0))。

這是使用 express 生成器創建第一個程序的完整描述,

Ubuntu的包管理器

要通過 apt-get 安裝 Node.js 和 npm,請運行以下命令:

sudo apt-get update
sudo apt-get install nodejs
sudo ln -s /usr/bin/nodejs /usr/bin/node
sudo apt-get install npm

快速應用程序生成器:

$ npm install express-generator -g

使用-h選項顯示命令選項:

$ express -h

  Usage: express [options] [dir]

  Options:

    -h, --help          output usage information
    -V, --version       output the version number
    -e, --ejs           add ejs engine support (defaults to jade)
        --hbs           add handlebars engine support
    -H, --hogan         add hogan.js engine support
    -c, --css <engine>  add stylesheet <engine> support (less|stylus|compass|sass) (defaults to plain css)
        --git           add .gitignore
    -f, --force         force on non-empty directory

例如,以下在當前工作目錄中創建一個名為myapp的 Express 應用程序:

$ express myapp

   create : myapp
   create : myapp/package.json
   create : myapp/app.js
   create : myapp/public
   create : myapp/public/javascripts
   create : myapp/public/images
   create : myapp/routes
   create : myapp/routes/index.js
   create : myapp/routes/users.js
   create : myapp/public/stylesheets
   create : myapp/public/stylesheets/style.css
   create : myapp/views
   create : myapp/views/index.jade
   create : myapp/views/layout.jade
   create : myapp/views/error.jade
   create : myapp/bin
   create : myapp/bin/www

然后安裝依賴:

$ cd myapp
$ npm install

使用以下命令運行應用程序:

$ DEBUG=myapp:* npm start

然后在瀏覽器中加載 http://localhost:3000/ 以訪問應用程序。

生成的應用程序具有以下目錄結構:

├── app.js
├── bin
│   └── www
├── package.json
├── public
│   ├── images
│   ├── javascripts
│   └── stylesheets
│       └── style.css
├── routes
│   ├── index.js
│   └── users.js
└── views
    ├── error.jade
    ├── index.jade
    └── layout.jade

7 directories, 9 files

Ubuntu 12.10 (Quantal Quetzal) 或Ubuntu 14.04 LTS (Trusty Tahr) 或Ubuntu 16.04.1 LTS (Xenial Xerus) 上安裝 Node.js。

請避免在 Ubuntu 上使用apt-get安裝 Node.js。 如果您已經使用內置包管理器安裝了 Node.js,請刪除它。 ( sudo apt-get purge nodejs && sudo apt-get autoremove && sudo apt-get autoclean )

Linux 上的安裝過程與 OS X 上相同。使用提供的腳本:

$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.3/install.sh | bash

$ nvm list
$ nvm ls-remote
$ nvm install 6.4.0
$ nvm use 6.4.0
$ nvm alias default 6.4.0
$ node -v
$ npm install -g npm
$ npm -v

安裝 Node JS 時的其他問題

不要使用sudo apt-get install nodejs npm 始終使用sudo apt-get install nodejs

如果您收到錯誤sudo: add-apt-repository: command not found只需在上面第二個之前運行此命令: sudo apt-get install software-properties-common

如果sudo: add-apt-repository: command not found並且需要在添加存儲庫之前運行 s udo apt-get install python-software-properties

如果您之前有舊版本或 node / npm 沒有出現在您的控制台中,請嘗試使用bash -r

鏈接說明已更新為curl -sL https://deb.nodesource.com/setup | sudo bash - sudo apt-get install -y nodejs curl -sL https://deb.nodesource.com/setup | sudo bash - sudo apt-get install -y nodejs


編輯更新

對於最新版本,您還可以使用 URL nodejs.org/dist/node-latest.tar.gz

還有一件事! 不要忘記運行以下命令,這會增加 inotify 監視的數量。

$ echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p

真的很簡單:

sudo apt install nodejs

然后輸入:

nodejs

使用它。

  1. sudo apt install nodejs
  2. sudo apt install npm
  3. curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
  4. sudo apt-get install -y nodejs

從終端獲取二進制文件

$ wget https://nodejs.org/dist/v16.13.1/node-v16.13.1-linux-x64.tar.xz
$ sudo mkdir -p /usr/local/lib/nodejs
$ sudo tar -xJvf node-v16.13.1-linux-x64.tar.xz -C /usr/local/lib/nodejs 

在 ~/.bashrc 文件末尾設置環境變量

# Nodejs
export PATH=/usr/local/lib/nodejs/node-v16.13.1-linux-x64/bin:$PATH

刷新你的 .bashrc 文件,或者關閉並重新打開你的終端,並檢查安裝的版本

$ . ~/.bashrc
$ npm version
$ npx -v
$ node -v

如果您正在尋找在 ubuntu 上簡單、快速(無構建過程)、本地(無 sudo)安裝,請查看:

在 Linux 上安裝節點

免責聲明:我是作者。

您只需克隆 repo 並運行setup.sh即可安裝最新的節點版本。 如果您想要特定版本,只需運行change-version.sh

我已經閱讀了上面的答案並嘗試過,效果很好,但有時您在使用這些答案時會遇到一些小問題。 如果您從Ubuntu store安裝nodejs怎么辦,我已經嘗試過了,成功了。

首先你必須卸載你安裝的舊垃圾

sudo apt-get remove nodejs

我也試圖通過apt安裝節點但失敗了,或者給我的版本10.xx已經過時了。 然后我隨機去了 ubuntu 商店並安裝了最新的,檢查它node --version

您可以使用簡單的命令來完成

sudo apt-get update

安裝要求。 文件

curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -

安裝節點

sudo apt install nodejs

檢查是否安裝正確。

node --version

對於最新的 Node.js

sudo apt-get install curl
curl -sL https://deb.nodesource.com/setup_13.x | sudo -E bash -

sudo apt-get install nodejs
node -v
npm -v

Node.js 包在 LTS 版本和當前版本中可用。 您可以根據需要選擇要在系統上安裝的版本。

使用當前版本:在本教程的最后一次更新中,Node.js 13 是當前可用的 Node.js 版本。

sudo apt-get install curl
curl -sL https://deb.nodesource.com/setup_13.x | sudo -E bash -

使用 LTS 版本:在本教程的最后一次更新中,Node.js 12.x 是可用的 LTS 版本。

sudo apt-get install curl
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -

您可以成功地將 Node.js PPA 添加到 Ubuntu 系統。 現在執行以下命令以使用 apt-get 在和 Ubuntu 上安裝 Node.js。 這也將使用 Node.js 安裝 NPM。 此命令還會在您的系統上安裝許多其他依賴包。

sudo apt-get install nodejs

安裝 Node.js 后,驗證並檢查安裝的版本。 您可以在 Node.js 官方網站上找到有關當前版本的更多詳細信息。

node -v

v13.0.1

另外,檢查 npm 版本:

npm -v

6.12.0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM