How to install NVM on macOS
My articles are for beginners who don’t know much about programming, I’m trying to explain things as simple as I can without using a lot of technical words.
Often, when testing and debugging Node.js programs locally, different projects require different Node.js versions. This is where NVM (Node Version Manager) comes in handy. NVM allows you to easily install and manage multiple Node.js versions on your macOS machine. One of its main benefits is the ability to set different Node.js versions for each project or globally. It’s straightforward and simple to use. In this article, I’ll show you how to install NVM and switch between different Node.js versions.
Note that you can get the latest version from GitHub at https://github.com/nvm-sh/nvm/releases. At the moment of creating this article, the latest version was v0.39.7.
Step 1: In your terminal, run the following curl command to download it
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
The script will clone the NVM repository to ~/.nvm and try to add the required source lines to the appropriate profile file, such as ~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc.
Step 2: If you don’t have any of these files, you can create one by running:
touch ~/.zshrc
Follow these instructions to show/hide hidden files on your Mac
Step 3: Open your ~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc.
open ~/.zshrc
Then, add the following line to your profile file to configure NVM
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
Step 4: To verify Installation that nvm is successfully installed, re-start your terminal and run:
command -v nvm
Congratulations! NVM is now installed on your machine.
Below are a few useful nvm commands for installing, uninstalling, and switching between Node.js versions locally
List all installed Node.js versions:
nvm ls
Install a specific Node.js version:
nvm install 21.6.0
Uninstall a specific Node.js version:
nvm uninstall 18.6.0
Set the default Node.js version:
nvm alias default 20.11.0
To specify a Node.js version for a specific project, navigate to the project directory and execute:
nvm use 20.11.0
Check the currently used Node.js version:
node --version
There are other better solutions available, but in my case, and most of the time, I don’t run Docker on my personal machine. Instead, I run personal scripts and projects without Docker. NVM comes in handy in such cases and makes local development easier.
For official documentation, please visit the GitHub link below: https://github.com/nvm-sh/nvm
I hope this helps! Comment down below if you have any questions.