nodejs CentOS安装介绍
安装方法
通过nvm安装
安装NVM
解决curl无法链接到raw.githubusercontent.com的问题
解决方案1
下载nvm-sh源码压缩包
通过Xshell上传到系统中并解压
运行nvm解压后目录里面的install.sh脚本
解决方案2
验证nvm命令
nvm 安装指定版本的nodejs
安装最新稳定版的nodejs
nvm 切换Node.js版本
查看当前机器已安装node版本号
设置默认的node版本
升级npm
nodejs配置
使用淘宝镜像
参考资料
使用EPEL安装 (yum安装方式)
nodejs CentOS安装介绍
进入Github上的仓库:https://github.com/nodesource/distributions
按下ctrl+f打开搜索,输入centos,找到要centos对应的安装的方式:
或者直接点击下方链接进入
Installation instructions
NOTE: If you are using RHEL 6 or CentOS 6, you might want to read about running Node.js on older distros.
The Nodesource RPM package signing key is available here: https://rpm.nodesource.com/pub/el/NODESOURCE-GPG-SIGNING-KEY-EL
Run on RHEL, CentOS, CloudLinux, Amazon Linux or Fedora:
……
Node.js v12.x
As root
1 curl -fsSL https://rpm.nodesource.com/setup_12.x | bash -
No root privileges
1 curl -fsSL https://rpm.nodesource.com/setup_12.x | sudo bash -
……
To compile and install native addons from npm you may also need to install build tools:
yum install gcc-c++ make
or: yum groupinstall ‘Development Tools’
安装方法
root用户,执行命令:1
curl -fsSL https://rpm.nodesource.com/setup_12.x | bash -
然后根据提示,输入命令:1
sudo yum install -y nodejs
等待安装完毕即可
验证是否安装成:1
2node -v
npm -v
安装成功效果:1
2
3
4
5
6
7
8
9
10
11
12 正在安装 : 2:nodejs-12.21.0-1nodesource.aarch64 1/1
验证中 : 2:nodejs-12.21.0-1nodesource.aarch64 1/1
已安装:
nodejs.aarch64 2:12.21.0-1nodesource
完毕!
[root@localhost ~]# npm -v
6.14.11
[root@localhost ~]# node -v
v12.21.0
[root@localhost ~]#
通过nvm安装
安装NVM
打开网址https://github.com/nvm-sh/nvm#installing-and-updating
复制命令:1
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
或者1
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
命令进行安装NVM.
解决curl无法链接到raw.githubusercontent.com的问题
不知道怎么回事,命令之中无法连接到raw.githubusercontent.com,报错如下:1
2
3
4[root@localhost ~]# curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0curl: (7) Failed connect to raw.githubusercontent.com:443; 拒绝连接
解决方案1
下载nvm-sh源码压缩包
进入nvm-sh仓库的releases页面:https://github.com/nvm-sh/nvm/releases,手动下载nvm-sh的.tar.gz包:https://github.com/nvm-sh/nvm/archive/v0.37.2.tar.gz
通过Xshell上传到系统中并解压
通过Xshell,将源码包上传到/opt目录下,然后解压:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16[root@localhost ~]# cd /opt
[root@localhost opt]# ls
apache-tomcat-8.5.63.tar.gz java jdk-8u281-linux-aarch64.tar.gz nvm-0.37.2.tar.gz tomcat tomcat85
[root@localhost opt]# mkdir nvm
[root@localhost opt]# ls
apache-tomcat-8.5.63.tar.gz java jdk-8u281-linux-aarch64.tar.gz nvm nvm-0.37.2.tar.gz tomcat tomcat85
[root@localhost opt]# tar -zxvf nvm-0.37.2.tar.gz -C nvm
nvm-0.37.2/
nvm-0.37.2/.dockerignore
nvm-0.37.2/.editorconfig
......
nvm-0.37.2/test/sourcing/setup
nvm-0.37.2/test/sourcing/teardown
nvm-0.37.2/update_test_mocks.sh
[root@localhost opt]# ls
apache-tomcat-8.5.63.tar.gz java jdk-8u281-linux-aarch64.tar.gz nvm nvm-0.37.2.tar.gz tomcat tomcat85
运行nvm解压后目录里面的install.sh脚本
1 | [root@localhost opt]# cd nvm |
使NVM命令立即生效:1
source ~/.bashrc
解决方案2
使用git克隆仓库,然后再运行install.sh脚本
验证nvm命令
1 | [root@localhost nvm-0.37.2]# nvm list-remote |
可以看到nvm安装成功了,后面可以通过nvm来更新nodejs
nvm 安装指定版本的nodejs
1 | nvm install v8.6.0 |
安装最新稳定版的nodejs
1 | nvm install stable |
nvm 切换Node.js版本
1 | nvm use v8.6.0 |
查看当前机器已安装node版本号
1 | nvm list |
设置默认的node版本
1 | nvm alias default v9.5.0 |
升级npm
1 | npm -g |
nodejs配置
使用淘宝镜像
1 | npm config set registry https://registry.npm.taobao.org |
查看设置效果:1
npm config list
运行效果:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19[root@localhost nvm-0.37.2]# npm config set registry https://registry.npm.taobao.org
[root@localhost nvm-0.37.2]# npm config list
; cli configs
metrics-registry = "https://registry.npm.taobao.org/"
scope = ""
user-agent = "npm/6.14.11 node/v12.21.0 linux arm64"
; project config /opt/nvm/nvm-0.37.2/.npmrc
package-lock = false
; userconfig /root/.npmrc
registry = "https://registry.npm.taobao.org/"
; node bin location = /usr/bin/node
; cwd = /opt/nvm/nvm-0.37.2
; HOME = /root
; "npm config ls -l" to show all defaults.
[root@localhost nvm-0.37.2]#
参考资料
https://www.cnblogs.com/fps2tao/p/9956139.html
https://github.com/nodesource/distributions