Node Version Manager (NVM) is a powerful tool that allows you to manage multiple versions of Node.js on your system. It simplifies the process of installing, updating, and switching between different Node.js versions, making it easier to work with various projects that require different Node.js versions. In this article, we will guide you through the process of installing NVM on macOS using the Homebrew package manager.
Prerequisites
To follow this tutorial, you should have:
- A macOS computer
- Homebrew installed on your system (if you don’t have it installed, you can follow the instructions at https://brew.sh/)
- Installing NVM
To install NVM using Homebrew, open your terminal and run the following command:
brew install nvm
Homebrew will download and install NVM, as well as any required dependencies.
- Configuring NVM
After installing NVM, you need to create a new directory for it and update your shell configuration to load NVM whenever a new terminal session is started.
First, create a new directory for NVM:
mkdir ~/.nvm
Next, open your shell configuration file. If you’re using the default bash
shell, this will be ~/.bash_profile
or ~/.bashrc
. If you’re using zsh
, it will be ~/.zshrc
.
For example, to open the .zshrc
file using the nano text editor, run:
nano ~/.zshrc
Add the following lines to the end of the file:
export NVM_DIR="$HOME/.nvm"
[ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && . "/opt/homebrew/opt/nvm/nvm.sh" # This loads nvm
[ -s "/
opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" ] && . "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" # This loads nvm bash_completion
These lines set the NVM directory and load NVM and its bash completion when a new terminal session is started.
Save the changes and exit the text editor. For nano, press CTRL + X
, then Y
, and then Enter
.
To apply the changes, restart your terminal or run the following command:
```bash
source ~/.zshrc
(Replace .zshrc
with the appropriate shell configuration file if you’re using a different shell.)
- Verifying the Installation
To verify that NVM has been installed correctly, run the following command:
nvm --version
This command should display the installed NVM version, indicating that the installation was successful.
- Installing Node.js with NVM
With NVM installed, you can now easily install and manage multiple Node.js versions. To install the latest LTS (Long Term Support) version of Node.js, run:
nvm install --lts
To install a specific version of Node.js, use:
nvm install <version>
Replace <version>
with the desired Node.js version (e.g., 16.13.0
).
To switch between installed Node.js versions, use the nvm use
command:
nvm use <version>
- Updating NVM
When a new version of NVM becomes available, you can update it using Homebrew:
brew upgrade nvm
This command will update NVM to the latest version, along with any dependencies that need updating.
Conclusion
In this article, we’ve demonstrated how to install NVM on macOS using Homebrew. By using NVM, you can manage multiple Node.js versions on your system, making it easier to work with different projects that require specific Node.js versions. This will help you maintain a more organized and efficient development environment.