【问题标题】:Install Node.js on Ubuntu在 Ubuntu 上安装 Node.js
【发布时间】:2013-04-24 12:33:51
【问题描述】:

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

【问题讨论】:

    标签: node.js ubuntu npm


    【解决方案1】:

    只需按照here 的官方说明在 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 当前(截至 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
    

    【讨论】:

      【解决方案2】:

      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 应该在 node repl 之外运行,在你的普通 shell 中。安装 node 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(Node Version Manager)等其他工具,例如:

      sudo snap refresh node --channel=11/stable
      

      用户可以通过切换来测试可以从当前跟踪 Node.js 版本 12 的最新边缘通道安装的 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 Pending 2022-04-19 2022-10-25 2023-10-18 2025-04-30

      【讨论】:

        【解决方案3】:

        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 &amp;&amp; sudo apt-get autoremove &amp;&amp; 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
        

        【讨论】:

          【解决方案4】:
          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

          【讨论】:

          • node已经自带NPM
          【解决方案5】:

          真的很简单:

          sudo apt install nodejs
          

          然后输入:

          nodejs
          

          使用它。

          【讨论】:

            【解决方案6】:

            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
            

            【讨论】:

              【解决方案7】:

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

              【讨论】:

                【解决方案8】:

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

                Express 应用程序生成器:

                $ 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
                

                【讨论】:

                  【解决方案9】:

                  现在您可以简单地安装

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

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

                  sudo apt-get install python g++ make
                  

                  【讨论】:

                    【解决方案10】:

                    按照 NodeSource 上的 here 给出的说明进行操作,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
                    

                    【讨论】:

                      【解决方案11】:

                      我个人是这样做的:

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

                      【讨论】:

                        【解决方案12】:
                        wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.19.0/install.sh | bash
                        
                        nvm install v0.10.33
                        

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

                        【讨论】:

                          【解决方案13】:

                          这是轻松安装 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
                          

                          【讨论】:

                            【解决方案14】:
                            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/

                            【讨论】:

                              【解决方案15】:

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

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

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

                              【讨论】:

                                【解决方案16】:

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

                                【讨论】:

                                  【解决方案17】:

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

                                  【讨论】:

                                    【解决方案18】:

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

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

                                    [sudo] npm install -g npm
                                    

                                    【讨论】:

                                      【解决方案19】:

                                      只需按照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 上安装当前稳定的节点。量子 (12.10) 用户可能需要安装软件-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

                                      【讨论】:

                                        【解决方案20】:

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

                                        sudo apt-get install nodejs
                                        

                                        【讨论】:

                                        • 记住,安装不包括 npm。
                                        • 使用此解决方案,您无法选择节点版本,也无法获取最新版本,请改用this
                                        猜你喜欢
                                        • 2018-07-21
                                        • 2023-03-16
                                        • 1970-01-01
                                        • 1970-01-01
                                        • 1970-01-01
                                        • 2011-10-27
                                        • 1970-01-01
                                        • 2015-06-24
                                        • 2018-06-17
                                        相关资源
                                        最近更新 更多