常规安装

以安装v12.x.x版本为例

1
2
3
4
5
6
7
#Ubuntu/Debian 
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
apt update && apt install -y nodejs

#CentOS/RedHat
curl --silent --location https://rpm.nodesource.com/setup_12.x | sudo bash -
yum update && yum install -y nodejs

然后执行node -vnpm -v查看是否安装成功

NVM安装

Node Version Manager - NVM
一款由 bash 写的 NodeJS 版本管理器

下载

1
2
3
4
5
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.35.2/install.sh | bash

#或使用wget
 
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.35.2/install.sh | bash

激活nvm

source ~/.nvm/nvm.sh

激活后,重启下命令行窗口,或者直接打开一个新的命令行窗口,进行后续操作。

列出node版本

nvm ls-remote

nvm ls-remote会列出所有可用的 Nodejs 版本,如果输出中看到很多很多个版本号,就表示 nvm 安装好了。最后一个版本号就是当前最新的 Nodejs 版本,这里安装以本文发行时间的最新版本v12.7.0为例

nvm install v12.7.0

1
2
3
4
5
6
7
root@Wittoy:~# nvm install v12.7.0
Downloading and installing node v12.7.0...
Downloading https://nodejs.org/dist/v12.7.0/node-v12.7.0-linux-x64.tar.xz...
############################################################ 100.0%Computing checksum with sha256sum
Checksums matched!
Now using node v12.7.0 (npm v6.10.0)
Creating default alias: default -> v12.7.0

到此已经成功安装Nodejs v12.7.0 npm v6.10.0版本

切换版本

1
nvm install v11.0.0

装好之后,可以执行nvm ls查看安装的版本,以及 default指向的默认版本。

修改默认版本方法:

1
nvm alias default v*.*.*

其中*替换成对应版本即可

FNM安装

Fast Node Manager - FNM
一款由 rust 写的 NodeJS 版本管理器

1
curl -fsSL https://fnm.vercel.app/install | bash
1
2
3
4
fnm install lts

#切换对应版本,如v11.0.0
fnm install 11.0.0

n安装

n – Interactively Manage Your Node.js Versions
一款由 bash 写的 NodeJS 版本管理器

1
2
3
4
5
6
npm install -g n

#如果未安装npm,则使用下面流程安装。
curl -L https://raw.githubusercontent.com/tj/n/master/bin/n -o n
bash n lts
npm install -g n

切换版本

1
2
3
4
n lts

#切换对应版本,如v11.0.0
n 11.0.0