Skip to content

nvm-sh Installation Guide

nvm-sh for Linux/MacOS/WSL Installation

Using the Installation Script

Open the terminal and run one of the following commands:

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

or:

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

nvm-sh

The installation script will clone the NVM repository to the ~/.nvm directory and try to add the following snippet to the correct configuration file (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc).

bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # Load NVM
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # Load NVM bash completion

Note

On Linux, after running the installation script, if you get nvm: command not found or no feedback from the terminal after typing command -v nvm, simply close the current terminal, open a new terminal, and try again. Or, you can run the following commands for different shells:

bash
# bash: 
source ~/.bashrc

# zsh: 
source ~/.zshrc

#ksh: 
. ~/.profile

Manual Installation

If you prefer manual installation, follow these steps:

  1. 下载 NVM 源码压缩包:
bash
wget https://github.com/nvm-sh/nvm/archive/refs/tags/v0.39.3.tar.gz
  1. 创建 NVM 目录并解压缩:
bash
mkdir -p ~/.nvm
tar -zxvf v0.39.3.tar.gz -C ~/.nvm
  1. 配置环境变量,编辑 ~/.bashrc 文件:
bash
vim ~/.bashrc
  1. 在文件末尾添加:
bash
export NVM_DIR="$HOME/.nvm/nvm-0.39.3"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # 加载 NVM
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # 加载 NVM bash 补全
  1. 使配置生效:
bash
source ~/.bashrc

验证安装

安装完成后,关闭并重新打开终端,或者运行 source ~/.bashrc,然后输入以下命令验证安装:

bash
nvm --version

如果显示 NVM 版本号,说明安装成功。

权限问题解决(MacOS)

在使用 Node.js 的过程中,特别是使用 npm 安装全局包时,由于 MacOS 系统安全性的限制,经常出现安装权限问题或者安装完成后使用时出现 Command not found 的情况。

使用 NVM 管理 Node.js 可以有效解决这些权限问题,因为 NVM 安装的 Node.js 位于用户目录下,不需要管理员权限。

卸载nvm

手动卸载 要手动卸载nvm,请执行以下步骤:

首先,使用nvm unload从您的终端会话中移除nvm命令,并删除安装目录:

bash
$ nvm_dir="${NVM_DIR:-~/.nvm}"
$ nvm unload
$ rm -rf "$nvm_dir"

编辑 ~/.bashrc (或其他 shell 资源配置文件),并删除以下行:

bash
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

Built with VitePress