linux安装nvm和nodejs

Posted 不想下火车的人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux安装nvm和nodejs相关的知识,希望对你有一定的参考价值。

  下载nvm包:

[root@centos ~]# wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
=> Downloading nvm as script to \'/root/.nvm\'

=> Appending nvm source string to /root/.bashrc
=> Appending bash_completion source string to /root/.bashrc
=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \\. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \\. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

  上面提示相关的环境变量已经在配置文件/root/.bashrc中给我们自动添加好了,需要重新进入ssh。

  按它说的,重新进入ssh后,开始安装nodejs和npm:

[root@centos ~]# nvm ls-remote --lts
         v4.2.0   (LTS: Argon)
         v4.2.1   (LTS: Argon)
         v4.2.2   (LTS: Argon)
         v4.2.3   (LTS: Argon)
         v4.2.4   (LTS: Argon)
         v4.2.5   (LTS: Argon)
         v4.2.6   (LTS: Argon)
         v4.3.0   (LTS: Argon)
         v4.3.1   (LTS: Argon)
         v4.3.2   (LTS: Argon)
         v4.4.0   (LTS: Argon)
         v4.4.1   (LTS: Argon)
         v4.4.2   (LTS: Argon)
         v4.4.3   (LTS: Argon)
         v4.4.4   (LTS: Argon)
         v4.4.5   (LTS: Argon)
         v4.4.6   (LTS: Argon)
         v4.4.7   (LTS: Argon)
         v4.5.0   (LTS: Argon)
         v4.6.0   (LTS: Argon)
         v4.6.1   (LTS: Argon)
         v4.6.2   (LTS: Argon)
         v4.7.0   (LTS: Argon)
         v4.7.1   (LTS: Argon)
         v4.7.2   (LTS: Argon)
         v4.7.3   (LTS: Argon)
         v4.8.0   (LTS: Argon)
         v4.8.1   (LTS: Argon)
         v4.8.2   (LTS: Argon)
         v4.8.3   (LTS: Argon)
         v4.8.4   (LTS: Argon)
         v4.8.5   (LTS: Argon)
         v4.8.6   (LTS: Argon)
         v4.8.7   (LTS: Argon)
         v4.9.0   (LTS: Argon)
         v4.9.1   (Latest LTS: Argon)
         v6.9.0   (LTS: Boron)
         v6.9.1   (LTS: Boron)
         v6.9.2   (LTS: Boron)
         v6.9.3   (LTS: Boron)
         v6.9.4   (LTS: Boron)
         v6.9.5   (LTS: Boron)
        v6.10.0   (LTS: Boron)
        v6.10.1   (LTS: Boron)
        v6.10.2   (LTS: Boron)
        v6.10.3   (LTS: Boron)
        v6.11.0   (LTS: Boron)
        v6.11.1   (LTS: Boron)
        v6.11.2   (LTS: Boron)
        v6.11.3   (LTS: Boron)
        v6.11.4   (LTS: Boron)
        v6.11.5   (LTS: Boron)
        v6.12.0   (LTS: Boron)
        v6.12.1   (LTS: Boron)
        v6.12.2   (LTS: Boron)
        v6.12.3   (LTS: Boron)
        v6.13.0   (LTS: Boron)
        v6.13.1   (LTS: Boron)
        v6.14.0   (LTS: Boron)
        v6.14.1   (LTS: Boron)
        v6.14.2   (LTS: Boron)
        v6.14.3   (LTS: Boron)
        v6.14.4   (LTS: Boron)
        v6.15.0   (LTS: Boron)
        v6.15.1   (LTS: Boron)
        v6.16.0   (LTS: Boron)
        v6.17.0   (LTS: Boron)
        v6.17.1   (Latest LTS: Boron)
         v8.9.0   (LTS: Carbon)
         v8.9.1   (LTS: Carbon)
         v8.9.2   (LTS: Carbon)
         v8.9.3   (LTS: Carbon)
         v8.9.4   (LTS: Carbon)
        v8.10.0   (LTS: Carbon)
        v8.11.0   (LTS: Carbon)
        v8.11.1   (LTS: Carbon)
        v8.11.2   (LTS: Carbon)
        v8.11.3   (LTS: Carbon)
        v8.11.4   (LTS: Carbon)
        v8.12.0   (LTS: Carbon)
        v8.13.0   (LTS: Carbon)
        v8.14.0   (LTS: Carbon)
        v8.14.1   (LTS: Carbon)
        v8.15.0   (LTS: Carbon)
        v8.15.1   (LTS: Carbon)
        v8.16.0   (LTS: Carbon)
        v8.16.1   (LTS: Carbon)
        v8.16.2   (Latest LTS: Carbon)
       v10.13.0   (LTS: Dubnium)
       v10.14.0   (LTS: Dubnium)
       v10.14.1   (LTS: Dubnium)
       v10.14.2   (LTS: Dubnium)
       v10.15.0   (LTS: Dubnium)
       v10.15.1   (LTS: Dubnium)
       v10.15.2   (LTS: Dubnium)
       v10.15.3   (LTS: Dubnium)
       v10.16.0   (LTS: Dubnium)
       v10.16.1   (LTS: Dubnium)
       v10.16.2   (LTS: Dubnium)
       v10.16.3   (LTS: Dubnium)
       v10.17.0   (Latest LTS: Dubnium)
       v12.13.0   (Latest LTS: Erbium)

  挑最新版12.13.0安装,失败了:

[root@centos ~]# nvm install 12.13.0
Downloading and installing node v12.13.0...
Downloading https://nodejs.org/dist/v12.13.0/node-v12.13.0-linux-x86.tar.xz...

curl: (22) The requested URL returned error: 404 Not Found
Binary download from https://nodejs.org/dist/v12.13.0/node-v12.13.0-linux-x86.tar.xz failed, trying source.
grep: /root/.nvm/.cache/bin/node-v12.13.0-linux-x86/node-v12.13.0-linux-x86.tar.xz: No such file or directory
Provided file to checksum does not exist.
Binary download failed, trying source.
Detected that you have 1 CPU core(s)
Number of CPU core(s) less than or equal to 2, running in single-threaded mode
Downloading https://nodejs.org/dist/v12.13.0/node-v12.13.0.tar.xz...
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
$>./configure --prefix=/root/.nvm/versions/node/v12.13.0 <
which: no python2.7 in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
Please use Python 2.7:

  /usr/bin/python2 ./configure --prefix=/root/.nvm/versions/node/v12.13.0
nvm: install v12.13.0 failed!

  提示说没有python2.7,确认下(默认会装2.6.6版本):

[root@centos ~]# yum list installed | grep python
abrt-addon-python.i686
abrt-python.i686      2.0.8-43.el6.centos @anaconda-CentOS-201703281202.i386/6.9
dbus-python.i686      0.83.0-6.1.el6      @anaconda-CentOS-201703281202.i386/6.9
libproxy-python.i686  0.3.0-10.el6        @anaconda-CentOS-201703281202.i386/6.9
libreport-python.i686 2.0.9-33.el6.centos @anaconda-CentOS-201703281202.i386/6.9
libxml2-python.i686   2.7.6-21.el6_8.1    @anaconda-CentOS-201703281202.i386/6.9
newt-python.i686      0.52.11-4.el6       @anaconda-CentOS-201703281202.i386/6.9
python.i686           2.6.6-66.el6_8      @anaconda-CentOS-201703281202.i386/6.9
python-argparse.noarch
python-babel.noarch   0.9.4-5.1.el6       @os                                   
python-backports.i686 1.0-5.el6           @os                                   
python-backports-ssl_match_hostname.noarch
python-chardet.noarch 2.2.1-1.el6         @os                                   
python-cheetah.i686   2.4.1-1.el6         @os                                   
python-configobj.noarch
python-crypto2.6.i686 2.6.1-2.el6         @os                                   
python-dmidecode.i686 3.10.15-1.el6       @anaconda-CentOS-201703281202.i386/6.9
python-iniparse.noarch
python-jinja2.i686    2.2.1-3.el6         @os                                   
python-jsonpatch.noarch
python-jsonpointer.noarch
python-libs.i686      2.6.6-66.el6_8      @anaconda-CentOS-201703281202.i386/6.9
python-markdown.noarch
python-oauthlib.noarch
python-prettytable.noarch
python-pycurl.i686    7.19.0-9.el6        @anaconda-CentOS-201703281202.i386/6.9
python-pygments.noarch
python-requests.noarch
python-setuptools.noarch
python-six.noarch     1.9.0-2.el6         @os                                   
python-urlgrabber.noarch
python-urllib3.noarch 1.10.2-3.el6        @os                                   
rpm-python.i686       4.8.0-55.el6        @anaconda-CentOS-201703281202.i386/6.9

  把2.6.6升级到2.7(参见linux升级python到2.7版本)再试试:

[root@centos ~]# nvm install 12.13.0
Downloading and installing node v12.13.0...
Downloading https://nodejs.org/dist/v12.13.0/node-v12.13.0-linux-x86.tar.xz...

curl: (22) The requested URL returned error: 404 Not Found
Binary download from https://nodejs.org/dist/v12.13.0/node-v12.13.0-linux-x86.tar.xz failed, trying source.
grep: /root/.nvm/.cache/bin/node-v12.13.0-linux-x86/node-v12.13.0-linux-x86.tar.xz: No such file or directory
Provided file to checksum does not exist.
Binary download failed, trying source.
Detected that you have 1 CPU core(s)
Number of CPU core(s) less than or equal to 2, running in single-threaded mode
Local cache found: $NVM_DIR/.cache/src/node-v12.13.0/node-v12.13.0.tar.xz
Checksums match! Using existing downloaded archive $NVM_DIR/.cache/src/node-v12.13.0/node-v12.13.0.tar.xz
$>./configure --prefix=/root/.nvm/versions/node/v12.13.0 <
WARNING: failed to autodetect C++ compiler version (CXX=g++)
ERROR: Did not find a new enough assembler, install one or build with
       --openssl-no-asm.
       Please refer to BUILDING.md
nvm: install v12.13.0 failed!

  还是失败了,换个低版本试试:

[root@centos /]# nvm install 8.16.2
Downloading and installing node v8.16.2...
Downloading https://nodejs.org/dist/v8.16.2/node-v8.16.2-linux-x86.tar.xz...
###########################################                               59.8%
######################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v8.16.2 (npm v6.4.1)
Creating default alias: default -> 8.16.2 (-> v8.16.2 *)

  这次可以了:

[root@centos /]# node -v
v8.16.2

 

以上是关于linux安装nvm和nodejs的主要内容,如果未能解决你的问题,请参考以下文章

Linux安装nodejs和npm

yarnnpmnrm与nvm的安装和使用详解

linux安装nvm

快速安装node.js以及换国内源

自己安装的node nvm识别吗

Linux安装NVM