NVM – 在 Linux 上安装多个 Node.js 版本

Node版本管理器(简称NVM)是一个简单的bash脚本,用于管理Linux系统上的多个活动node.js版本。它允许您安装多个node.js版本,并查看所有可安装版本和系统上所有已安装版本。

Nvm还支持运行特定node.js版本,并可以显示安装路径到可执行文件的路径,以及更多。

在本文中,我们将解释如何在您的Linux发行版上安装Node版本管理器(NVM)以管理多个活动node.js版本。

在Linux中安装NVM(Node版本管理器)

要在您的Linux发行版上安装或更新nvm,您可以使用curlwget命令行工具下载自动安装脚本。

# curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
OR
# wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash

上述自动安装脚本将nvm存储库克隆到您的主目录~/.nvm中,并将所需的源命令添加到您的shell启动脚本,即~/.bash_profile~/.zshrc~/.profile~/.bashrc,具体取决于您使用的shell程序,如下面的输出所示。

Install NVM in Linux
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 15916  100 15916    0     0  34227      0 --:--:-- --:--:-- --:--:-- 34301
=> Downloading nvm from git to '/root/.nvm'
=> Cloning into '/root/.nvm'...
remote: Enumerating objects: 359, done.
remote: Counting objects: 100% (359/359), done.
remote: Compressing objects: 100% (305/305), done.
remote: Total 359 (delta 40), reused 168 (delta 28), pack-reused 0
Receiving objects: 100% (359/359), 219.46 KiB | 6.86 MiB/s, done.
Resolving deltas: 100% (40/40), done.
* (HEAD detached at FETCH_HEAD)
  master
=> Compressing and cleaning up git repository

=> Appending nvm source string to /root/.bashrc
=> Appending bash_completion source string to /root/.bashrc
=> You currently have modules installed globally with `npm`. These will no
=> longer be linked to the active version of Node when you install a new node
=> with `nvm`; and they may (depending on how you construct your `$PATH`)
=> override the binaries of modules installed with `nvm`:
...

安装完成后,请关闭当前的终端窗口并打开一个新的窗口,或者简单地加载您的shell配置文件以加载nvm。例如,如果您使用的是bash,请运行以下命令:

$ source ~/.bashrc
OR
$ source ~/.profile

接下来,使用以下命令验证nvm是否已安装在您的系统上。

# command -v nvm

nvm

如果安装成功,它将显示“nvm”作为输出。

使用NVM安装多个Node.js版本

现在是学习如何在Linux中使用Node版本管理器的时候了。要下载、编译和安装最新版本的node,请运行以下命令:

# nvm install node 

请注意,在上述命令中,“node”是最新版本的别名。

Install Latest Node Version in Linux
Downloading and installing node v20.0.0...
Downloading https://nodejs.org/dist/v20.0.0/node-v20.0.0-linux-x64.tar.xz...
########################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v20.0.0 (npm v9.6.4)
Creating default alias: default -> node (-> v20.0.0)

要安装特定的“node”版本,请首先列出可用的node版本,然后按照所示安装版本。

# nvm ls-remote
# nvm install 19.0.0  
Install Specific Node Version in Linux
Downloading and installing node v19.0.0...
Downloading https://nodejs.org/dist/v19.0.0/node-v19.0.0-linux-x64.tar.xz...
########################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v19.0.0 (npm v8.19.2)

安装了一个使用 nvm 的 Node.js 版本后,系统现在应该指向 nvm 下的 Node.js 和 npm 版本。您可以通过运行以下命令检查正在使用的 node 和 npm 二进制/命令的位置:

# which node
/root/.nvm/versions/node/v20.0.0/bin/node

# which npm
/root/.nvm/versions/node/v20.0.0/bin/npm

# node -v
v20.0.0

# npm -v
9.6.4

列出已安装的 Node.js 版本

您可以使用以下命令检查所有已安装的版本:

# nvm ls
List Installed Node Versions in Linux
->      v19.0.0
        v20.0.0
         system
default -> node (-> v20.0.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v20.0.0) (default)
stable -> 20.0 (-> v20.0.0) (default)
lts/* -> lts/hydrogen (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.12 (-> N/A)
lts/fermium -> v14.21.3 (-> N/A)
lts/gallium -> v16.20.0 (-> N/A)
lts/hydrogen -> v18.16.0 (-> N/A)

为项目选择 Node 版本

这就是 nvm 变得有趣的地方。现在,您可以根据需求为正在进行的每个项目选择要使用的 node.js 版本。让我们看看如何做到这一点,如下所述。

打开多个终端并进入项目目录。对于本指南,我们在 ~/projects/app-server 和 ~/projects/test-ui 下有两个项目,它们分别需要虚假地使用节点版本 19.0.0 和 20.0.0。

------------ Project 1 ------------ 
$ cd ~/projects/api-server
$ nvm use 19.0.0
$ node -v

------------ Project 2 ------------
$ cd ~/projects/test-ui
$ nvm use 20.0.0
$ node -v
Important: Note that when switching between different versions of Node.js, any global npm packages that you have installed will not work. This is because the multiple Node.js version installations are completely independent.

例如,如果在版本 19.0.0 激活时安装了全局软件包,则在切换到版本 20.0.0 时,您需要在新版本下重新安装全局软件包。

重要的是,您可以查看特定节点版本安装到的可执行文件路径:

# nvm which 20.0.0
# nvm which 19.0.0
# nvm which system  #check system-installed version of a node using “system” alias

在 Linux 中设置默认的 Node.js 版本

此外,要手动设置一个默认的 node 版本以在任何新的 shell 中使用,请使用如下所示的别名 “default”

# nvm alias default 20.0.0
# nvm alias default system
# nvm alias default 19.0.0
Note: You can create a .nvmrc initialization file in your project root directory (or any parent directory) and add a node version number or any other flags or usage options that nvm understands, in it. Then use some of the commands we have just looked at above to operate with the specified version in the file.

在 Linux 中卸载 Node.js 版本

要卸载或删除某个 Node.js 版本,执行以下命令(不要忘记将 19.0.0 替换为版本号):

# nvm uninstall 19.0.0

在 Linux 中卸载 NVM

要手动从您的系统中删除 nvm,请运行以下命令:

# rm -rf "$NVM_DIR"
OR
# rm -rf ~/.nvm

接下来,编辑 ~/.bashrc(或适用的 shell 配置文件,如上所述),并删除以下配置行:

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

有关更多信息,请参阅 nvm --help 或访问 Node 版本管理器 Github 代码库:https://github.com/nvm-sh/nvm

就这样! Node 版本管理器 是一个简单的 bash 脚本,用于在 Linux 系统上管理多个活动的 node.js 版本。使用下面的反馈表单来提问或与我们分享您的意见。

Source:
https://www.tecmint.com/nvm-install-multiple-nodejs-versions-in-linux/