DEV Community

Sonu kumar
Sonu kumar

Posted on

How to Install and Manage Multiple Node.js Versions on macOS Using NVM

As a developer working with Node.js, we might need to work on various projects and each project has its configuration. So maybe some of the projects have different versions of Node.js, and then we need to switch between different versions of Node.js for multiple projects. The Node Version Manager (NVM) is a fantastic tool that makes this process easier. In this guide, we'll cover three ways to install NVM on macOS and show you how to manage multiple Node.js versions with ease.

What is NVM?

NVM is a version manager for Node.js, designed to simplify the installation and management of multiple Node.js versions on a single machine. With NVM, you can easily switch between different versions of Node.js as per your project's requirements.

Prerequisites

Before we start, ensure you have the following:

  • A macOS machine ( This method support for all versions of macOS including M1, M2 and Intel)
  • Command Line Tools (you can install them by running xcode-select - install in the Terminal) Now, let's explore the three ways to install NVM on macOS:

Method 1: Installing NVM Using the Curl Command

This is the recommended method from the official NVM GitHub repository.

  1. Open Terminal: Launch the Terminal application on your macOS.
  2. Download and Install NVM: Run the following command to download and install NVM:
 curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
Enter fullscreen mode Exit fullscreen mode
  1. Load NVM: After installation, you need to load NVM into your current terminal session. Add the following lines to your shell profile file (e.g., .zshrc for Zsh or .bash_profile for Bash):
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
Enter fullscreen mode Exit fullscreen mode
  1. Apply Changes: Run the following command to apply the changes:
source ~/.zshrc
Enter fullscreen mode Exit fullscreen mode
  1. Verify Installation: Check if NVM is installed correctly by running:
nvm - version
Enter fullscreen mode Exit fullscreen mode

Method 2: Installing NVM Using Homebrew

Homebrew is a popular package manager for macOS.

  1. Open Terminal: Launch the Terminal application.
  2. Install Homebrew (if not already installed):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Enter fullscreen mode Exit fullscreen mode
  1. Install NVM with Homebrew: Run the following command to install NVM:
brew install nvm
Enter fullscreen mode Exit fullscreen mode
  1. Create NVM Directory: Create a directory for NVM:
 mkdir ~/.nvm
Enter fullscreen mode Exit fullscreen mode
  1. Load NVM: Add the following lines to your shell profile file (e.g., .zshrc or .bash_profile):
export NVM_DIR="$HOME/.nvm"
 . "$(brew - prefix nvm)/nvm.sh"
Enter fullscreen mode Exit fullscreen mode
  1. Apply Changes: Run the following command to apply the changes:
source ~/.zshrc
Enter fullscreen mode Exit fullscreen mode
  1. Verify Installation: Check if NVM is installed correctly by running:
nvm - version
Enter fullscreen mode Exit fullscreen mode

Method 3: Installing NVM Using MacPorts

MacPorts is another package management system for macOS.

  1. Open Terminal: Launch the Terminal application.
  2. Install MacPorts (if not already installed):  - Download and install MacPorts from the official MacPorts website.
  3. Install NVM with MacPorts: Run the following command to install NVM:
sudo port install nvm
Enter fullscreen mode Exit fullscreen mode
  1. Load NVM: Add the following lines to your shell profile file (e.g., .zshrc or .bash_profile):
export NVM_DIR="$HOME/.nvm"
 . /opt/local/share/nvm/init-nvm.sh
Enter fullscreen mode Exit fullscreen mode
  1. Apply Changes: Run the following command to apply the changes:
source ~/.zshrc
Enter fullscreen mode Exit fullscreen mode
  1. Verify Installation: Check if NVM is installed correctly by running:
nvm - version
Enter fullscreen mode Exit fullscreen mode

Managing Multiple Node.js Versions with NVM

Now that you have NVM installed, let's see how you can manage multiple Node.js versions.

Installing a Specific Node.js Version

To install a specific version of Node.js, use the following command:

nvm install <version>
Enter fullscreen mode Exit fullscreen mode

For example, to install Node.js version 14.17.0:

nvm install 14.17.0
Enter fullscreen mode Exit fullscreen mode

Listing Installed Node.js Versions

To list all installed Node.js versions, run:

nvm ls
Enter fullscreen mode Exit fullscreen mode

Switching Between Node.js Versions

To switch to a specific version of Node.js, use the following command:

nvm use <version>
Enter fullscreen mode Exit fullscreen mode

For example, to switch to Node.js version 14.17.0:

nvm use 14.17.0
Enter fullscreen mode Exit fullscreen mode

Setting a Default Node.js Version

To set a default Node.js version, which will be used whenever you open a new terminal session, run:

nvm alias default <version>
Enter fullscreen mode Exit fullscreen mode

For example, to set Node.js version 14.17.0 as the default:

nvm alias default 14.17.0
Enter fullscreen mode Exit fullscreen mode

Conclusion
With NVM, managing multiple Node.js versions on macOS is a breeze. Whether you choose to install NVM using the curl command, Homebrew, or MacPorts, you now have the tools to easily switch between Node.js versions and ensure your projects run smoothly. Happy coding!

Top comments (0)