How to Install and Manage Multiple Node.js Versions on macOS Using NVM
Tags: webdev, programming, node, macos
Published: 2024-06-27 04:02:15

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:
```sh
 curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
```
3. **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
```
4. **Apply Changes**: Run the following command to apply the changes:

```
source ~/.zshrc
```
5. **Verify Installation**: Check if NVM is installed correctly by running:

```
nvm - version
```
### 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)"
```
3. **Install NVM with Homebrew**: Run the following command to install NVM:

```
brew install nvm
```
4. **Create NVM Directory**: Create a directory for NVM:

```
 mkdir ~/.nvm
```
5. **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"
```
6. **Apply Changes**: Run the following command to apply the changes:

```
source ~/.zshrc
```
7. **Verify Installation**: Check if NVM is installed correctly by running:

```
nvm - version
```
### 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](https://www.macports.org/install.php).
3. **Install NVM with MacPorts**: Run the following command to install NVM:

```
sudo port install nvm
```
4. **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
```
5. **Apply Changes**: Run the following command to apply the changes:

```
source ~/.zshrc
```
6. **Verify Installation**: Check if NVM is installed correctly by running:

```
nvm - version
```

## 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:

```sh
nvm install <version>
```
For example, to install Node.js version 14.17.0:

```sh
nvm install 14.17.0
```
### Listing Installed Node.js Versions
To list all installed Node.js versions, run:
```sh
nvm ls
```
### Switching Between Node.js Versions
To switch to a specific version of Node.js, use the following command:

```sh
nvm use <version>
```
For example, to switch to Node.js version 14.17.0:
```sh
nvm use 14.17.0
```
### 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:
```sh
nvm alias default <version>
```
For example, to set Node.js version 14.17.0 as the default:
```sh
nvm alias default 14.17.0
```
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!