如何在 Ubuntu 22.04 上安装和使用 NVM(节点版本管理器)
在此页
- 先决条件
- 安装 NVM
- 使用 NVM 安装 Node.js
- 使用 NVM 管理 Node.js 版本
- 结论
NVM,也称为“节点版本管理器”,用于在 Linux 中安装和管理多个 Node.js 版本。它提供了一个命令行实用程序,可帮助开发人员安装多个 Node 版本并根据项目要求在多个版本之间切换。如果您正在处理需要不同 Node 版本的多个项目,NVM 将非常有用。
在这篇文章中,我们将向您展示如何在 Ubuntu 22.04 上安装和使用 NVM 来管理 Node.js。
先决条件
- 一台运行 Ubuntu 22.04 的服务器。
- 在服务器上配置了根密码。
安装 NVM
默认情况下,NVM 不包含在 Ubuntu 默认存储库中。所以你需要从脚本中安装它。
首先,使用以下命令安装 CURL 和 Gnupg2:
apt-get install curl gnupg2 -y
接下来,运行以下命令下载并运行 NVM 安装脚本:
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
上述命令将安装 NVM 并在 .bashrc 文件中进行所有必需的环境设置。
=> `nvm` Nodes), you can remove them from the system Node as follows:
$ nvm use system
$ npm uninstall -g a_module
=> 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
现在,使用以下命令激活所有设置:
source ~/.bashrc
现在,使用以下命令验证 NVM 版本:
nvm --version
您应该看到以下输出:
0.39.1
使用 NVM 安装 Node.js
NVM 现已安装在您的系统中。您现在可以使用 NVM 命令行将任何 Node.js 版本安装到您的系统。
例如,要安装最新版本的 Node.js,请运行以下命令:
nvm install node
您应该看到以下输出:
Downloading and installing node v18.9.1...
Downloading https://nodejs.org/dist/v18.9.1/node-v18.9.1-linux-x64.tar.xz...
####################################################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v18.9.1 (npm v8.19.1)
Creating default alias: default -> node (-> v18.9.1)
要验证已安装的 Node.js 版本,请运行以下命令:
node --version
您应该看到以下输出:
v18.9.1
如果要安装最新的稳定版 Node.js,请运行以下命令:
nvm install node --lts
您应该看到以下输出:
v18.9.1 is already installed.
Now using node v18.9.1 (npm v8.19.1)
要安装特定的 Node.js 版本 (12.17.0),请运行以下命令:
nvm install 12.17.0
您应该看到以下输出:
Downloading and installing node v12.17.0...
Downloading https://nodejs.org/dist/v12.17.0/node-v12.17.0-linux-x64.tar.xz...
######################################################################################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v12.17.0 (npm v6.14.4)
现在,使用以下命令验证当前的 Node.js 版本:
node --version
您应该看到以下输出:
v12.17.0
使用 NVM 管理 Node.js 版本
要列出系统中所有已安装的 Node.js 版本,请运行以下命令:
nvm ls
您应该看到以下输出:
-> v12.17.0
v18.9.1
system
default -> node (-> v18.9.1)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v18.9.1) (default)
stable -> 18.9 (-> v18.9.1) (default)
lts/* -> lts/gallium (-> 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.20.1 (-> N/A)
lts/gallium -> v16.17.1 (-> N/A)
您可以使用以下命令找到所有可用的 Node.js 版本:
nvm ls-remote
要将默认 Node.js 版本设置为 12.17.0,请运行以下命令:
nvm use 12.17.0
您应该看到以下输出:
Now using node v12.17.0 (npm v6.14.4)
要查找当前用户的默认版本,请运行以下命令:
nvm run default --version
您应该看到以下输出:
Running node v16.9.0 (npm v7.21.1)
v16.9.0
您还可以使用以下命令运行具有特定 Node.js 版本的 Node 应用程序:
nvm run v12.17.0 app.js
要从系统中删除特定的 Node.js 版本,请运行以下命令:
nvm uninstall v12.17.0
结论
在本指南中,我们解释了如何在 Ubuntu 22.04 上安装和使用 NVM。您现在可以使用 NVM 来管理多个 Mode.js 版本并在它们之间切换。我希望您现在可以使用任何 Node.js 版本运行您的应用程序。