How To Install NVM on macOS with Homebrew

Umar Farooque Khan
3 min readApr 30, 2024

--

Check NVM on macOS

To check if NVM (Node Version Manager) is installed on your macOS system, you can use the following command in your terminal:

nvm --version

If NVM is installed, this command will return the version number of NVM installed on your system. If it’s not installed, you’ll likely see a message indicating that the command nvm is not found, suggesting that NVM is not installed.

Additionally, you can check if the NVM directory exists in your home folder by running:

ls ~/.nvm

If NVM is installed, you should see the .nvm directory listed. If it's not installed, this command will return an error indicating that the directory doesn't exist.

Install NVM on macOS

Before installing any packages, it’s recommended to update Homebrew to the latest version and refresh the available formulae.

brew update

After updating, you can install NVM using the following command:

brew install nvm

Next, create a .nvm directory in your home folder:

mkdir ~/.nvm

Now, you need to configure the required environment variables. Open the configuration file in your home directory:

vim ~/.bash_profile

Add the following lines to your .bash_profile (or use .zshrc if you're on macOS Catalina or a newer version):

export NVM_DIR="$HOME/.nvm"
[ -s "/usr/local/opt/nvm/nvm.sh" ] && \. "/usr/local/opt/nvm/nvm.sh"
[ -s "/usr/local/opt/nvm/etc/bash_completion" ] && \. "/usr/local/opt/nvm/etc/bash_completion"

Save and close the file (ESC + :wq).

Next, load the updated environment variables into the current shell. From your next login, these settings will load automatically.

source ~/.bash_profile  # or source ~/.zshrc

You have successfully installed NVM on your macOS system. Proceed to the next step to install various versions of Node.js with NVM.

Using NVM

To view the Node.js versions available for installation, type:

nvm ls-remote

You can install any version from the list. Additionally, you can use aliases such as node for the latest version or lts for the latest LTS version.

nvm install node   # Install the latest version
nvm install 20 # Install Node.js 20.X version

After installation, verify the installed versions with:

nvm ls

If you’ve installed multiple Node.js versions, you can set a specific version as the default at any time. For example, to set Node.js 20 as the default version, use:

nvm use 20

Similarly, you can install and switch between other Node.js versions like 12, 16, 18, and 21.

Conclusion

Being able to manage and switch between different Node.js versions is crucial for modern web development, and NVM offers this flexibility. Homebrew simplifies the installation process on macOS, allowing you to get NVM up and running efficiently. Once installed, you can seamlessly work on various Node.js projects regardless of their version requirements.

This guide provides you with the information needed to install NVM on macOS using Homebrew. Now, you’re prepared to handle any Node.js project, with the ability to manage and switch between Node.js versions with ease.

--

--

Umar Farooque Khan

Experienced software developer with a passion for clean code and problem-solving. Full-stack expertise in web development. Lifelong learner and team player.