How to install NVM (Node Version Manager) on macOS

  1. Install & Update Script
  2. Verify your installation
  3. Install Node.js using NVM
  4. Troubleshooting

Install & Update Script

To install nvm, download the install script and make it executable.

1
2
chmod +x install.sh
sh install.sh

Since macOS 10.15, the default shell is zsh and nvm will look for .zshrc to update, none is installed by default. Create one with touch ~/.zshrc:

1
touch ~/.zshrc

Add the source lines to .zshrc profile file:

1
2
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

Verify your installation

You can verify that NVM is installed by running:

1
nvm --version

Install Node.js using NVM

1
2
3
4
5
6
7
8
9
10
11
12
$ nvm use 16
Now using node v16.9.1 (npm v7.21.1)
$ node -v
v16.9.1
$ nvm use 14
Now using node v14.18.0 (npm v6.14.15)
$ node -v
v14.18.0
$ nvm install 12
Now using node v12.22.6 (npm v6.14.5)
$ node -v
v12.22.6

Troubleshooting

If you have this error:

1
mkdir: /Users/rainyjune/.nvm/alias: Permission denied

Change permission on the .nvm directory:

1
sudo chmod -R 777 ~/.nvm 

yarn corepack error: “Error when performing the request to https://registry.npmjs.org/yarn/latest“, please run the command below in your terminal:

1
export COREPACK_NPM_REGISTRY=https://registry.npmmirror.com