我试图在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是最新版本,但它也显示了我在顶部显示的文本。我认为问题是ppa:chris-lea/node.js,但我不知道如何解决它。


在最新版本的Node.js中,npm会自动与Node.js一起安装。当你在终端中输入node—version和npm—version时,你会看到什么?

你也可以使用npm本身来升级npm:

[sudo] npm install -g npm

简单地按照这里给出的说明:

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. 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 As of Node.js v0.10.0, the nodejs package from Chris Lea's repo includes both npm and nodejs-dev.

不要给sudo apt-get install nodejs npm。只需sudo apt-get install nodejs。


我的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。


可以使用nvm安装Node.js。它允许您使用不同的版本而没有冲突。


到今天为止,你可以简单地安装它:

sudo apt-get install nodejs

您也可以像这样从源代码编译它

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/


这是轻松安装Node.js的最佳方式。这也适用于Ubuntu 12.04 (Precise穿山甲),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

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

nvm install v0.10.33

只需使用nvm进行Node.js版本控制- nvm。


我个人是这样做的:

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)相比,它太过时了)。


遵循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

现在你可以简单地安装:

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

确保预先安装了Python和C解释器/编译器。如果没有,执行:

sudo apt-get install python g++ make

下面是使用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

用下面的命令运行app:

$ 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:命令没有找到,只需在上面的第二个命令之前运行这个命令:sudo apt-get install software-properties-common

如果未找到sudo: add-apt-repository:命令,需要在添加存储库前执行sudo 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


编辑更新

对于最新版本,您也可以使用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

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 -v
v11.5.0

最新版本的npm将作为节点快照的一部分安装。NPM应该在节点repl之外运行,在正常的shell中。安装节点snap后,运行以下命令启用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(节点版本管理器),例如:

sudo snap refresh node --channel=11/stable

用户可以通过切换来测试可以从最新边缘通道安装的Node.js的前沿版本:

sudo snap switch node --edge

这种方法只推荐给那些愿意参与上游测试和错误报告的用户。

Node.js LTS调度

Release Status Codename Initial release LTS Start Maintenance Start Maintenance End
6.x EOL Boron 2016-04-26 2016-10-18 2018-04-30 2019-04-30
7.x EOL 2017-05-30 2017-06-30
8.x EOL Carbon 2016-10-25 2017-10-31 2019-01-01 2019-12-31
9.x EOL 2017-10-01 2018-06-30
10.x EOL Dubnium 2018-04-24 2018-10-30 2020-05-19 2021-04-30
11.x EOL 2018-10-23 2019-06-01
12.x Maintenance LTS Erbium 2019-04-23 2019-10-21 2020-11-301 2022-04-30
13.x EOL 2019-10-22 2020-06-01
14.x Maintenance LTS Fermium 2020-04-21 2020-10-27 2021-10-30 2023-04-30
16.x Active LTS Gallium 2021-04-20 2021-10-26 2022-10-18 2024-04-30
17.x Current 2021-10-19 2022-04-01 2022-06-01
18.x Current 2022-04-19 2022-10-25 2023-10-18 2025-04-30

获取最新的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

很简单:

sudo apt install nodejs

然后输入:

nodejs

使用它。


Sudo apt安装nodejs 必须安装NPM curl - sl https://deb.nodesource.com/setup_14.x | sudo - e bash - . Sudo apt-get install -y nodejs


只要按照这里的官方说明来安装Ubuntu

安装说明

Node.js LTS(截至2022年1月是v16.x):

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

Node.js Current(截至2022年1月是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

如果你正在寻找一个简单,快速(没有构建过程),本地(没有sudo)安装ubuntu,请查看:

install-node-on-linux

声明:我是作者。

您只需要克隆repo并运行setup.sh,就可以安装最新的节点版本。如果你想要一个特定的版本,只需运行change-version.sh。


我已经阅读了上面的答案,我尝试了,工作得很好,但有时你会在使用这些答案时遇到小问题。如果你从Ubuntu商店安装nodejs,我试过了,vallah,任务完成了。

首先,你必须卸载你已经安装的旧垃圾

sudo apt-get remove nodejs

我也试图通过apt安装节点,但失败了,或者给我版本10.x。X已经过时了。然后我随机地去ubuntu商店安装了最新的版本,检查它的节点——版本


你可以用简单的命令来完成

sudo apt-get update

安装要求。文件

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

安装 Nodejs

sudo apt install nodejs

检查是否安装正确。

node --version