ChatGPT解决这个技术问题 Extra ChatGPT

在 Ubuntu 上安装 Node.js

我正在尝试在 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,但我不知道如何解决它。


P
Peter Mortensen

只需按照给定 here 的说明进行操作:

安装示例: 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 开始,nodejs 包来自Chris Lea 的 repo 包括 npm 和 nodejs-dev。

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


M
Manwal

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

sudo apt-get install nodejs

请记住,那是安装不包括 npm。
使用此解决方案,您无法选择节点版本,也无法获取最新版本,请改用 this
P
Peter Mortensen

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

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

[sudo] npm install -g npm

P
Peter Mortensen

我的 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's installation wiki


P
Peter Mortensen

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

P
Peter Mortensen

现在您可以简单地安装:

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

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

sudo apt-get install python g++ make

P
Peter Mortensen

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


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

nvm install v0.10.33

只需将 nvm 用于 Node.js 版本控制 - nvm


J
João Pimentel Ferreira

只需按照 here 的官方说明在 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

P
Peter Mortensen

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

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

在此处查找详细说明http://howtonode.org/how-to-install-nodejs


P
Peter Mortensen
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/


P
Peter Mortensen

遵循 NodeSource 提供的 here 说明,该说明致力于为 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

k
karel

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 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 维护 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 维护 LTS Fermium 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


P
Peter Mortensen

我个人是这样做的:

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))。


P
Peter Mortensen

这是使用 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

P
Peter Mortensen

真的很简单:

sudo apt install nodejs

然后输入:

nodejs

使用它。


k
kamalesh biswas

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


node 已经自带 NPM
G
Gupta

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 并且需要在添加存储库之前运行 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

D
David Weldon

如果您正在寻找在 ubuntu 上简单、快速(无构建过程)、本地(无 sudo)安装,请查看:

install-node-on-linux

免责声明:我是作者。

您只需克隆存储库并运行 setup.sh 即可安装最新的节点版本。如果您想要特定版本,只需运行 change-version.sh


P
Peter Mortensen

对于最新的 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

P
Peter Mortensen

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