install nvm on mac

How to Install NVM on Mac with Brew

NVM is a shell script that allows you to easily install & manage NodeJS. In this article, we will look at how to install NVM on Mac with Homebrew utility. If you have not installed Homebrew on your Mac, you can easily do so by opening the terminal and running the following command. It will download & install homebrew for you.

$ ruby -e "$(curl -fsSL" 

How to Install NVM on Mac with Brew

Here are the steps to install NVM on Mac with Homebrew.

1. Remove Existing NodeJS

If you have already installed NodeJS earlier, then open terminal and run the following commands to uninstall them first. If there is no NodeJS installed on your system, you can skip this step.

$ brew uninstall --ignore-dependencies node  
$ brew uninstall --force node 

2. Install NVM

Run the following command to update NVM package list and install NVM.

$ brew update  
$ brew install nvm 

Create directory for NVM in /home.

$ mkdir ~/.nvm 

Next, we need to configure environment variables to point to NVM. Open bash_profile file in text editor. Alternatively, you may open ~/.zshrc for macOS Catalina or later.

$ vim ~/.bash_profile 

Add the following lines to this file.

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

Save and close the file by typing Esc, then entering :wq.

Run the following command to reload bash_profile. It will also ensure that environment variable is updated every time you login.

source ~/.bash_profile

3. Install NodeJS with NVM

NVM is a very handy tool to install & manage NodeJS on your system. You can list the available NodeJS versions with the following command.

$ nvm ls-remote

Next, if you want to download and install the latest NodeJS version, just run the following command.

$ nvm install node

If you want to download & install a specific version of NodeJS like Node 12.x, run the following command.

$ nvm install 12

After installation, you can verify it with the following command.

$ nvm ls

If you have multiple versions of NodeJS installed on your system, you can set the default version with the following command. Here is an example to use Node 12.x as default version.

$ nvm use 12

You can always install different NodeJS versions on same system and manage them easily using NVM.

That’s it. In this article, we have learnt how to install and manage NodeJS using NVM, in Mac. Although you can install NodeJS separately, without NVM, it is advisable to NVM since it allows you to easily manage multiple versions of NodeJS, making it convenient to test and upgrade to new versions, or rollback to older versions, as you need.

Also read:

Shell Script to Start, Stop & Restart Tomcat Automatically
Shell Script to Backup MySQL Database
Htaccess Redirect If URL Contains String/Word
How to Change Root Password in Linux
How to Run Shell Script as Cron Job

Leave a Reply

Your email address will not be published.