How To Install NVM on macOS with Homebrew
Check NVM on macOS
To check if NVM (Node Version Manager) is installed on your macOS system, you can use the following command in your terminal:
nvm --versionIf NVM is installed, this command will return the version number of NVM installed on your system. If it’s not installed, you’ll likely see a message indicating that the command nvm is not found, suggesting that NVM is not installed.
Additionally, you can check if the NVM directory exists in your home folder by running:
ls ~/.nvmIf NVM is installed, you should see the .nvm directory listed. If it's not installed, this command will return an error indicating that the directory doesn't exist.
Install NVM on macOS
Before installing any packages, it’s recommended to update Homebrew to the latest version and refresh the available formulae.
brew updateAfter updating, you can install NVM using the following command:
brew install nvmNext, create a .nvm directory in your home folder:
mkdir ~/.nvmNow, you need to configure the required environment variables. Open the configuration file in your home directory:
vim ~/.bash_profileAdd the following lines to your .bash_profile (or use .zshrc if you're on macOS Catalina or a newer version):
export NVM_DIR="$HOME/.nvm"
[ -s "/usr/local/opt/nvm/nvm.sh" ] && \. "/usr/local/opt/nvm/nvm.sh"
[ -s "/usr/local/opt/nvm/etc/bash_completion" ] && \. "/usr/local/opt/nvm/etc/bash_completion"Save and close the file (ESC + :wq).
Next, load the updated environment variables into the current shell. From your next login, these settings will load automatically.
source ~/.bash_profile # or source ~/.zshrcYou have successfully installed NVM on your macOS system. Proceed to the next step to install various versions of Node.js with NVM.
Using NVM
To view the Node.js versions available for installation, type:
nvm ls-remoteYou can install any version from the list. Additionally, you can use aliases such as node for the latest version or lts for the latest LTS version.
nvm install node # Install the latest version
nvm install 20 # Install Node.js 20.X versionAfter installation, verify the installed versions with:
nvm lsIf you’ve installed multiple Node.js versions, you can set a specific version as the default at any time. For example, to set Node.js 20 as the default version, use:
nvm use 20Similarly, you can install and switch between other Node.js versions like 12, 16, 18, and 21.
Conclusion
Being able to manage and switch between different Node.js versions is crucial for modern web development, and NVM offers this flexibility. Homebrew simplifies the installation process on macOS, allowing you to get NVM up and running efficiently. Once installed, you can seamlessly work on various Node.js projects regardless of their version requirements.
This guide provides you with the information needed to install NVM on macOS using Homebrew. Now, you’re prepared to handle any Node.js project, with the ability to manage and switch between Node.js versions with ease.
