繁体   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