如何在Ubuntu上安装npm和Node.js?

14

我已通过以下方式安装Node.js:

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

安装完成后,当我尝试安装npm时,

sudo apt-get install npm
我收到了这个输出:

我收到了这个输出:

Reading package lists... Done
Building dependency tree
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 npm : Depends: node-gyp (>= 0.10.9) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

我该如何使用这个?

当我运行npm --version时,输出结果如下:

The program 'npm' is currently not installed. You can install it by typing:

sudo apt install npm

我认为npm已经包含在nodejs中了。 - Imre_G
请查看我的更新后的问题。 - godot
我在使用 node 8.x 时也遇到了相同的问题。似乎在 node 10.x 上表现良好。 - Fabien Snauwaert
6个回答

21

TL;DR

nodejs需要安装libssl1.0-dev,但当已经安装了libcurl4-openssl-devlibssl-dev时,无法安装。

解决方法是卸载libcurl4-openssl-devlibssl-dev,或安装libssl1.0-dev以强制删除其他两个软件包。

完整故事

我也遇到了同样的问题,所以我尝试手动安装每个被抱怨的软件包。

$ sudo apt install npm
...
The following packages have unmet dependencies:
 npm : Depends: node-gyp (>= 0.10.9) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.
$ sudo apt install node-gyp
...
The following packages have unmet dependencies:
 node-gyp : Depends: nodejs-dev but it is not going to be installed
E: Unable to correct problems, you have held broken packages.
$ sudo apt install nodejs-dev
...
The following packages have unmet dependencies:
 nodejs-dev : Depends: libssl1.0-dev (>= 1.0.2) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.
$ sudo apt install libssl1.0-dev
...
The following packages will be REMOVED:
  libcurl4-openssl-dev libssl-dev
The following NEW packages will be installed:
  libssl1.0-dev
0 to upgrade, 1 to newly install, 2 to remove and 0 not to upgrade.
Need to get 1,364 kB of archives.
After this operation, 1,017 kB disk space will be freed.
Do you want to continue? [Y/n] y
Get:1 http://au.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libssl1.0-dev amd64 1.0.2n-1ubuntu5.1 [1,364 kB]
Fetched 1,364 kB in 4s (368 kB/s)
(Reading database ... 323301 files and directories currently installed.)
Removing libcurl4-openssl-dev:amd64 (7.58.0-2ubuntu3.3) ...
Removing libssl-dev:amd64 (1.1.0g-2ubuntu4.1) ...
Selecting previously unselected package libssl1.0-dev:amd64.
(Reading database ... 323196 files and directories currently installed.)
Preparing to unpack .../libssl1.0-dev_1.0.2n-1ubuntu5.1_amd64.deb ...
Unpacking libssl1.0-dev:amd64 (1.0.2n-1ubuntu5.1) ...
Setting up libssl1.0-dev:amd64 (1.0.2n-1ubuntu5.1) ...
Processing triggers for man-db (2.8.3-2) ...

看这里:

$ sudo apt install npm
...
The following additional packages will be installed:
  gyp libc-ares2 libhttp-parser2.7.1 libjs-async libjs-inherits libjs-node-uuid libuv1 libuv1-dev node-abbrev node-ansi node-ansi-color-table node-archy node-async node-balanced-match node-block-stream
  node-brace-expansion node-builtin-modules node-combined-stream node-concat-map node-cookie-jar node-delayed-stream node-forever-agent node-form-data node-fs.realpath node-fstream node-fstream-ignore
  node-github-url-from-git node-glob node-graceful-fs node-gyp node-hosted-git-info node-inflight node-inherits node-ini node-is-builtin-module node-isexe node-json-stringify-safe node-lockfile
  node-lru-cache node-mime node-minimatch node-mkdirp node-mute-stream node-node-uuid node-nopt node-normalize-package-data node-npmlog node-once node-osenv node-path-is-absolute node-pseudomap node-qs
  node-read node-read-package-json node-request node-retry node-rimraf node-semver node-sha node-slide node-spdx-correct node-spdx-expression-parse node-spdx-license-ids node-tar node-tunnel-agent
  node-underscore node-validate-npm-package-license node-which node-wrappy node-yallist nodejs nodejs-dev nodejs-doc
Suggested packages:
  node-hawk node-aws-sign node-oauth-sign node-http-signature
The following NEW packages will be installed:
  gyp libc-ares2 libhttp-parser2.7.1 libjs-async libjs-inherits libjs-node-uuid libuv1 libuv1-dev node-abbrev node-ansi node-ansi-color-table node-archy node-async node-balanced-match node-block-stream
  node-brace-expansion node-builtin-modules node-combined-stream node-concat-map node-cookie-jar node-delayed-stream node-forever-agent node-form-data node-fs.realpath node-fstream node-fstream-ignore
  node-github-url-from-git node-glob node-graceful-fs node-gyp node-hosted-git-info node-inflight node-inherits node-ini node-is-builtin-module node-isexe node-json-stringify-safe node-lockfile
  node-lru-cache node-mime node-minimatch node-mkdirp node-mute-stream node-node-uuid node-nopt node-normalize-package-data node-npmlog node-once node-osenv node-path-is-absolute node-pseudomap node-qs
  node-read node-read-package-json node-request node-retry node-rimraf node-semver node-sha node-slide node-spdx-correct node-spdx-expression-parse node-spdx-license-ids node-tar node-tunnel-agent
  node-underscore node-validate-npm-package-license node-which node-wrappy node-yallist nodejs nodejs-dev nodejs-doc npm
0 to upgrade, 74 to newly install, 0 to remove and 0 not to upgrade.
Need to get 8,422 kB of archives.
After this operation, 40.9 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

这个问题有一个Ubuntu的错误报告:https://bugs.launchpad.net/ubuntu/+source/nodejs/+bug/1794589 - fjarlq

3

请检查您的系统中是否已经安装了npm?如果已经安装,请更新它。这可能会起作用。

npm -v
sudo npm install npm@latest -g

好的,我在您编辑的帖子中发现npm不在其中。我建议您快速更新一下Ubuntu系统。

sudo apt-get update && sudo apt-get -y upgrade

然后重新安装Node.js:

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

请注意,上面的命令适用于Node.js版本9,如果您需要使用版本8,请将其替换。

1

这是一个非常好的指南,介绍如何在Ubuntu上安装和运行最新版本的Node.js:

在Ubuntu 18.04上安装Node.js的方法

该指南涵盖了PPA的安装方式,或者如果您不想将非官方存储库添加到Ubuntu安装中,它还告诉您如何下载和运行nvm,它将完成在/home目录中运行您选择的任何版本的Node.js的繁重工作。


不客气!我今天发现了这个,它帮了我很多。在谷歌搜索时,我还发现了这个问题,我觉得它会是一个不错的补充 :) - Crankycyclops

1

我遇到了相同的问题,通过添加nodesource仓库来解决了这个问题。

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

sudo apt-get install nodejs

0

错误信息为:

E: Unable to correct problems, you have held broken packages.

您可以输入以下命令来检查哪些软件包被保留:

apt-mark showhold

无论哪个在那里,都可以使用以下方法取消保持:

sudo apt-mark unhold <package name>

然后重新安装Node.js。


如果失败了,你可以尝试使用aptitude 来安装npm - 它比apt-get更擅长解决依赖问题。
sudo aptitude install npm

如果您没有安装aptitude,可以使用以下命令进行安装:
sudo apt install aptitude

-1
  1. 下载node.tar.xz
  2. 解压缩(例如在某个名为"node"的目录中)
  3. 复制到/usr/

     sudo cp -r node/{bin,include,lib,share} /usr/
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接