How To Install NVM on macOS with Homebrew

Back to Blog page

How To Install NVM on macOS with Homebrew

We are going to go through NVM - Node Version Manager and how to setup via Homebrew

Author Pirasanthan Jesugeevegan

Pirasanthan Jesugeevegan

Dec 6, 2022 • 2 min read

The NVM (Node Version Manager) is a shell script that is used to install and manage Node.js on a Linux-based system. Homebrew is used to install NVM on macOS.

This guide will show you how to install NVM on your macOS machine and manage

Prerequisites
  • Install Homebrew (You must have admin privileges)
powershell
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 
Step 1 – Remove existing Node Versions

If your system already has a node installed, uninstall it first. My system already has installed node via Homebrew. So uninstalling it first. Skip if not already installed.

powershell
brew uninstall --ignore-dependencies node 
brew uninstall --force node 
Step 2 – Install NVM on macOS

Update the Homebrew package list and install NVM.

powershell
brew update 
brew install nvm 

Next, create a directory for NVM at home.

powershell
mkdir ~/.nvm 

Now, configure the required environment variables. Edit the following configuration file in your home directory

powershell
open -a TextEdit ~/.bash_profile

and, add the below lines to ~/.bash_profile

markdown
export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh

Save and close the window

Next, load the variable to the current shell environment. From the next login, it will automatically loaded.

powershell
source ~/.bash_profile

That's all. Your macOS system now has the NVM installed. Continue to the following step to install Node.js versions using nvm.

Step 3 – Install Node.js with NVM

First, let's see what Node versions are available to install. By typing:

powershell
nvm ls-remote 

Now, you can install any version listed in the above output. You can also use aliases names like node for the latest version, lts for latest LTS version, etc.

powershell
nvm install node     ## Installing Latest version 
nvm install 14       ## Installing Node.js 14.X version 

After installing you can verify what is installed with:

powershell
nvm ls 

If you have installed multiple versions on your system, you can set any version as the default version at any time. To set node 14.X as the default version, simply use:

powershell
nvm use 14 

Similarly, you can install other versions like Node 12, 15, and 18 versions and switch between them.

Tagged with:

NVM

UNIX

0

Enjoyed this article?

Leave a comment below!