简体   繁体   English

在 Ubuntu 上安装 Node.js

[英]Install Node.js on Ubuntu

I'm trying install Node.js on Ubuntu 12.10 (Quantal Quetzal), but the terminal shows me an error about lost packages.我正在尝试在Ubuntu 12.10 (Quantal Quetzal) 上安装 Node.js,但终端向我显示有关丢失包的错误。 I tried with this:我试过这个:

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

But when I came to the last line sudo apt-get install nodejs npm shows this error:但是当我来到最后一行时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.

Then I uninstalled the ppa:chris-lea/node.js and I was trying a second option:然后我卸载了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

The same error, the terminal says npm is the latest version , but it also shows me the text I shown in the top.同样的错误,终端npm is the latest version ,但它也显示了我在顶部显示的文本。 I think the problem is ppa:chris-lea/node.js , but I don't know how solve it.我认为问题出在ppa:chris-lea/node.js ,但我不知道如何解决。

Simply follow the instructions given here :只需按照此处给出的说明进行操作:

Example install:安装示例:

 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

It installs current stable Node on the current stable Ubuntu.它在当前稳定的 Ubuntu 上安装当前稳定的节点。 Quantal (12.10) users may need to install the software-properties-common package for the add-apt-repository command to work: sudo apt-get install software-properties-common Quantal (12.10) 用户可能需要安装 software-properties-common 软件包才能使add-apt-repository命令工作: sudo apt-get install software-properties-common

As of Node.js v0.10.0, the nodejs package from Chris Lea's repo includes both npm and nodejs-dev.从 Node.js v0.10.0 开始,Chris Lea 的 repo 中的 nodejs 包包括 npm 和 nodejs-dev。

Don't give sudo apt-get install nodejs npm .不要给sudo apt-get install nodejs npm Just sudo apt-get install nodejs .只需sudo apt-get install nodejs

从今天开始,您可以简单地安装它:

sudo apt-get install nodejs

npm is automatically installed with Node.js in the latest version of Node.js.在最新版本的 Node.js 中,npm 会自动与 Node.js 一起安装。 What do you see when you type node --version and npm --version in the terminal?当您在终端中键入node --versionnpm --version时,您会看到什么?

You can upgrade npm using npm itself as well:您也可以使用 npm 本身升级 npm:

[sudo] npm install -g npm

My apt-get was old and busted, so I had to install from source.我的apt-get又旧又坏了,所以我不得不从源代码安装。 Here is what worked for me:这对我有用:

# 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

These steps were mostly taken from joyent's installation wiki .这些步骤主要取自Joyent 的安装 wiki

This is the best way to easyly install Node.js.这是轻松安装 Node.js 的最佳方式。 This also is actual for Ubuntu 12.04 (Precise Pangolin), Ubuntu 13.04 (Raring Ringtail), and Ubuntu 14.04 (Trusty Tahr) .这也适用于Ubuntu 12.04 (Precise Pangolin)、 Ubuntu 13.04 (Raring Ringtail) 和Ubuntu 14.04 (Trusty Tahr)

Adding Node.js repositories添加 Node.js 存储库

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

Node.js installation Node.js 安装

[sudo] apt-get install nodejs

Now checking Node.js version现在检查 Node.js 版本

node -v

Outputs输出

v0.10.20

This command should install npm这个命令应该安装 npm

npm install

Check npm version检查 npm 版本

npm -v

Outputs输出

1.4.3

If for some reason, if you see npm is not installed, you may try running:如果由于某种原因,如果你看到 npm 没有安装,你可以尝试运行:

[sudo] apt-get install npm

To update npm you may try running:要更新 npm,您可以尝试运行:

[sudo] npm install -g npm

Now you can simply install with:现在您可以简单地安装

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

Make sure you have the Python and C interpreters/compilers preinstalled.确保您已预安装 Python 和 C 解释器/编译器。 If not, perform:如果没有,请执行:

sudo apt-get install python g++ make

You can use nvm to install Node.js.您可以使用nvm安装 Node.js。 It allows you work with different versions without conflicts.它允许您使用不同的版本而不会发生冲突。

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

nvm install v0.10.33

Just use nvm for Node.js version control - nvm .只需将 nvm 用于 Node.js 版本控制 - nvm

Just follow the official instructions from here to install on Ubuntu只需按照此处的官方说明在 Ubuntu 上安装

Installation instructions安装说明

Node.js LTS (as of 01/2022 is v16.x): 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 Current (as of 01/2022 is v17.x): 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 : 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 : 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 : 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 : Node.js v12.x

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

Optional : install build tools可选:安装构建工具

To compile and install native addons from npm you may also need to install build tools:要从 npm 编译和安装本机插件,您可能还需要安装构建工具:

sudo apt-get install -y build-essential

You can also compile it from source like this您也可以像这样从源代码编译它

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

Find detailed instructions here http://howtonode.org/how-to-install-nodejs在此处查找详细说明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/ http://jstricks.com/install-node-js/

Follow the instructions given here at NodeSource which is dedicated to creating a sustainable ecosystem for Node.js.请遵循 NodeSource 提供的说明,该说明致力于为 Node.js 创建一个可持续的生态系统。

For Node.js >= 4.X对于 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 is available as a snap package in all currently supported versions of Ubuntu. Node.js 在所有当前支持的 Ubuntu 版本中都可以作为snap包使用。 Specific to Node.js, developers can choose from one or more of the currently supported releases and get regular automatic updates directly from NodeSource.具体到 Node.js,开发人员可以从一个或多个当前支持的版本中进行选择,并直接从 NodeSource 获取定期自动更新。 Node.js versions 6, 8, 9, 10, 11, 13, 14, 15, 16, 17 and 18 are currently available, with the Snap Store being updated within hours or minutes of a Node.js release. Node.js 版本 6、8、9、10、11、13、14、15、16、17 和 18 目前可用,Snap Store 会在 Node.js 发布后的几小时或几分钟内更新。

Node.js can be installed with a single command, for example:可以使用单个命令安装 Node.js,例如:

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

The node snap can be accessed by the command node , for example:节点快照可以通过命令node访问,例如:

$ node -v
v11.5.0

An up-to-date version of npm will installed as part of the node snap.最新版本的 npm 将作为节点快照的一部分安装。 npm should be run outside of the node repl, in your normal shell. npm 应该在 node repl 之外运行,在你的普通 shell 中。 After installing the node snap run the following command to enable npm update checking:安装节点快照后,运行以下命令以启用 npm 更新检查:

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

Replace your-username in the above command with your own username.将上述命令中的your-username替换为您自己的用户名。 Then run npm -v to check if the version of npm is up-to-date.然后运行npm -v检查 npm 的版本是否是最新的。 As an example I checked that npm was up-to-date, checked the version of an already installed package named yarn with the command npm list yarn and then updated the existing yarn package to the latest version with the command npm update yarn例如,我检查了 npm 是否是最新的,使用命令npm list yarn检查已安装的名为 yarn 的包的版本,然后使用命令npm update yarn将现有的 yarn 包更新到最新版本

Users can switch between versions of Node.js at any time without needing to involve additional tools like nvm (Node Version Manager), for example:用户可以随时在 Node.js 的版本之间切换,而无需涉及其他工具,例如 nvm(Node Version Manager),例如:

sudo snap refresh node --channel=11/stable

Users can test bleeding-edge versions of Node.js that can be installed from the latest edge channel by switching with:用户可以通过切换来测试可以从最新的边缘通道安装的 Node.js 的前沿版本:

sudo snap switch node --edge

This approach is only recommended for those users who are willing to participate in testing and bug reporting upstream.这种方法只推荐给那些愿意参与上游测试和错误报告的用户。

Node.js LTS schedule Node.js LTS 计划

Release发布 Status地位 Codename代码名称 Initial release初始发行 LTS Start LTS 开始 Maintenance Start维护开始 Maintenance End维护结束
6.x 6.x EOL停产 Boron 2016-04-26 2016-04-26 2016-10-18 2016-10-18 2018-04-30 2018-04-30 2019-04-30 2019-04-30
7.x 7.x EOL停产 2017-05-30 2017-05-30 2017-06-30 2017-06-30
8.x 8.x EOL停产 Carbon 2016-10-25 2016-10-25 2017-10-31 2017-10-31 2019-01-01 2019-01-01 2019-12-31 2019-12-31
9.x 9.x EOL停产 2017-10-01 2017-10-01 2018-06-30 2018-06-30
10.x 10.x EOL停产 Dubnium杜比尼姆 2018-04-24 2018-04-24 2018-10-30 2018-10-30 2020-05-19 2020-05-19 2021-04-30 2021-04-30
11.x 11.x EOL停产 2018-10-23 2018-10-23 2019-06-01 2019-06-01
12.x 12.x Maintenance LTS维护 LTS Erbium 2019-04-23 2019-04-23 2019-10-21 2019-10-21 2020-11-301 2020-11-301 2022-04-30 2022-04-30
13.x 13.x EOL停产 2019-10-22 2019-10-22 2020-06-01 2020-06-01
14.x 14.x Maintenance LTS维护 LTS Fermium 2020-04-21 2020-04-21 2020-10-27 2020-10-27 2021-10-30 2021-10-30 2023-04-30 2023-04-30
16.x 16.x Active LTS主动 LTS Gallium 2021-04-20 2021-04-20 2021-10-26 2021-10-26 2022-10-18 2022-10-18 2024-04-30 2024-04-30
17.x 17.x Current当前的 2021-10-19 2021-10-19 2022-04-01 2022-04-01 2022-06-01 2022-06-01
18.x 18.x Current当前的 2022-04-19 2022-04-19 2022-10-25 2022-10-25 2023-10-18 2023-10-18 2025-04-30 2025-04-30

I personally do it this way:我个人是这样做的:

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

If you want to install particular version then download the version you want from the Node.js site and execute the last tree steps.如果要安装特定版本,请从 Node.js 站点下载所需版本并执行最后的树步骤。

I would strongly suggest not using the default Node.js package from the distribution market, because it would be probably outdated (ie, the current for the time of writing this in the Ubuntu market is v0.10.25 which is too outdated compared to the latest (v0.12.0)).我强烈建议不要使用来自分发市场的默认 Node.js 包,因为它可能已经过时(即,在 Ubuntu 市场上写这篇文章时的当前版本是 v0.10.25,与最新版本相比太过时了(v0.12.0))。

Here is the full description to create the first program using the express generator,这是使用 express 生成器创建第一个程序的完整描述,

Ubuntu's package manager Ubuntu的包管理器

To install Node.js and npm via apt-get, run these commands:要通过 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

Express application generator:快速应用程序生成器:

$ npm install express-generator -g

Display the command options with the -h option:使用-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

For example, the following creates an Express application named myapp in the current working 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

Then install dependencies:然后安装依赖:

$ cd myapp
$ npm install

Run the app with this command:使用以下命令运行应用程序:

$ DEBUG=myapp:* npm start

Then load http://localhost:3000/ in your browser to access the application.然后在浏览器中加载 http://localhost:3000/ 以访问应用程序。

The generated application has the following directory structure:生成的应用程序具有以下目录结构:

├── 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

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

Please avoid installing Node.js with apt-get on Ubuntu.请避免在 Ubuntu 上使用apt-get安装 Node.js。 If you already installed Node.js with the built-in package manager, please remove that.如果您已经使用内置包管理器安装了 Node.js,请删除它。 ( sudo apt-get purge nodejs && sudo apt-get autoremove && sudo apt-get autoclean ) ( sudo apt-get purge nodejs && sudo apt-get autoremove && sudo apt-get autoclean )

The installation process on Linux is the same as on OS X. With the provided script: 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

Other issues while installing Node JS安装 Node JS 时的其他问题

Do not use sudo apt-get install nodejs npm .不要使用sudo apt-get install nodejs npm Always use sudo apt-get install nodejs始终使用sudo apt-get install nodejs

And If you get the error sudo: add-apt-repository: command not found just run this command prior to the second one above: sudo apt-get install software-properties-common如果您收到错误sudo: add-apt-repository: command not found只需在上面第二个之前运行此命令: sudo apt-get install software-properties-common

If sudo: add-apt-repository: command not found and needed to run s udo apt-get install python-software-properties before adding the repository如果sudo: add-apt-repository: command not found并且需要在添加存储库之前运行 s udo apt-get install python-software-properties

try to use a bash -r if you had an old version before or node / npm does not appear in your console如果您之前有旧版本或 node / npm 没有出现在您的控制台中,请尝试使用bash -r

The linked instructions have been updated to 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 curl -sL https://deb.nodesource.com/setup | sudo bash - sudo apt-get install -y nodejs


Edit Updated编辑更新

For the latest version you can also use the URL nodejs.org/dist/node-latest.tar.gz对于最新版本,您还可以使用 URL nodejs.org/dist/node-latest.tar.gz

One more thing!还有一件事! Don't forget to run the following command, which increases the amount of inotify watches.不要忘记运行以下命令,这会增加 inotify 监视的数量。

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

Really simple:真的很简单:

sudo apt install nodejs

And then type:然后输入:

nodejs

to use it.使用它。

  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

Fetch the binary from your terminal从终端获取二进制文件

$ 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 

Set the environment variable at the end of your ~/.bashrc file在 ~/.bashrc 文件末尾设置环境变量

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

Refresh your .bashrc file,or close and reopen your terminal, and check the versions installed刷新你的 .bashrc 文件,或者关闭并重新打开你的终端,并检查安装的版本

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

If you're looking for a simple, fast (no build process), local (no sudo) install on ubuntu, check out:如果您正在寻找在 ubuntu 上简单、快速(无构建过程)、本地(无 sudo)安装,请查看:

install-node-on-linux在 Linux 上安装节点

Disclaimer: I'm the author.免责声明:我是作者。

You just have to clone the repo and run setup.sh and you'll have the latest node version installed.您只需克隆 repo 并运行setup.sh即可安装最新的节点版本。 If you want a specific version just run change-version.sh .如果您想要特定版本,只需运行change-version.sh

I've read the answers above and I tried, worked fine but sometimes you will face little problem while using that answers.我已经阅读了上面的答案并尝试过,效果很好,但有时您在使用这些答案时会遇到一些小问题。 What if you install the nodejs from Ubuntu store , I have tried and vallah, job done.如果您从Ubuntu store安装nodejs怎么办,我已经尝试过了,成功了。

First you have to uninstall what old garbage you have install by首先你必须卸载你安装的旧垃圾

sudo apt-get remove nodejs

I was also trying to install node via apt but failing, or giving me the version 10.xx which was outdated.我也试图通过apt安装节点但失败了,或者给我的版本10.xx已经过时了。 I then randomly went to ubuntu store and installed the latest one, check it node --version然后我随机去了 ubuntu 商店并安装了最新的,检查它node --version

You can do it with just simple and easy commands您可以使用简单的命令来完成

sudo apt-get update

Install Req.安装要求。 Files文件

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

Install Nodejs安装节点

sudo apt install nodejs

Check if is it installed properly.检查是否安装正确。

node --version

For the latest Node.js对于最新的 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

The Node.js package is available in the LTS release and the current release. Node.js 包在 LTS 版本和当前版本中可用。 It's your choice to select which version you want to install on the system as per your requirements.您可以根据需要选择要在系统上安装的版本。

Use Current Release: At the last update of this tutorial, Node.js 13 is the current Node.js release available.使用当前版本:在本教程的最后一次更新中,Node.js 13 是当前可用的 Node.js 版本。

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

Use LTS Release: At the last update of this tutorial, Node.js 12.x is the LTS release available.使用 LTS 版本:在本教程的最后一次更新中,Node.js 12.x 是可用的 LTS 版本。

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

You can successfully add Node.js PPA to the Ubuntu system.您可以成功地将 Node.js PPA 添加到 Ubuntu 系统。 Now execute the below command to install Node.js on and Ubuntu using apt-get.现在执行以下命令以使用 apt-get 在和 Ubuntu 上安装 Node.js。 This will also install NPM with Node.js.这也将使用 Node.js 安装 NPM。 This command also installs many other dependent packages on your system.此命令还会在您的系统上安装许多其他依赖包。

sudo apt-get install nodejs

After installing Node.js, verify and check the installed version.安装 Node.js 后,验证并检查安装的版本。 You can find more details about the current version on the Node.js official website.您可以在 Node.js 官方网站上找到有关当前版本的更多详细信息。

node -v

v13.0.1

Also, check the npm version:另外,检查 npm 版本:

npm -v

6.12.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM