【问题标题】:How can I install npm and Node.js on Ubuntu?如何在 Ubuntu 上安装 npm 和 Node.js?
【发布时间】:2018-07-21 02:59:47
【问题描述】:

我通过以下方式安装了 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中了
  • 看看我更新的问题
  • 我在使用节点 8.x 时遇到了同样的问题。节点 10.x 似乎没问题。

标签: node.js ubuntu npm


【解决方案1】:

你能检查一下你的系统上是否已经有 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。

【讨论】:

    【解决方案2】:

    错误是:

    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
    

    【讨论】:

      【解决方案3】:
      1. 下载node.tar.xz
      2. 提取(例如在某些“节点”目录中)
      3. 复制到/usr/:

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

      【讨论】:

        【解决方案4】:

        TL;DR

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

        解决方案是卸载libcurl4-openssl-dev 和libssl-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
        

        【讨论】:

        【解决方案5】:

        这是一个非常好的使用 Ubuntu 启动和运行最新版本的 Node.js 的指南:

        How To Install Node.js on Ubuntu 18.04

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

        【讨论】:

        • 不客气!我今天发现了这一点,它对我帮助很大。在谷歌搜索时,我也发现了这个问题,我认为这将是一个很好的补充:)
        【解决方案6】:

        我遇到了同样的问题,我通过添加 nodesource repo 解决了这个问题

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-04-24
          • 2015-02-05
          • 2017-03-14
          • 2021-03-16
          • 1970-01-01
          • 2011-11-27
          相关资源
          最近更新 更多