How to install NVM (Node Version Manager) with Homebrew
Node version manager is a script to manage multiple active node.js versions. And I usually install most applications using homebrew.
NOTE: install NVM with Homebrew is not officially supported.
So these are the steps to install NVM with Homebrew:
- Install NVM with Homebrew
Install NVM is very easy just with this command:
brew install nvm
- First setup
The first thing we need to do is create a folder for the current user.nvm
where the files will reside.
mkdir ~/.nvm
- Install a node version
There are different node versions that you can install; if you want to install the last version is very simple just using the command:
nvm install node
But I work with the long-term support version and the command to install the last LTS version is the command below:
nvm install --lts
- Start using the node version installed with NVM
After you installed the node version that you are looking it will be active after this command:
nvm use node
The previous command is also useful if you want to change from different Node.js versions.
To check if everything is working as expected the command bellow print the node version:
nvm run node --version
Finally, we want to execute NVM in every shell session, so let's create two user environment variables NVM_DIR
and NVM_HOMEBREW
.
Generally, environment variables are going in the file ~/.bash_profile
if you are using BASH or in the ~/.zshenv
file if you are using ZSH, but it will work also if you want to put the code below in the file ~/.bashrc
for BASH or ~/.zshrc
for ZSH.
export NVM_DIR="$HOME/.nvm"
NVM_HOMEBREW="/usr/local/opt/nvm/nvm.sh"
[ -s "$NVM_HOMEBREW" ] && \. "$NVM_HOMEBREW"
You can use "$(brew --prefix nvm)/nvm.sh"
instead of "/usr/local/opt/nvm/nvm.sh"
but it will be slower.
The NODE_PATH
variable can also be useful for some applications, for example, VScode, this is the code you need to put after the previous variables.
[ -x "$(command -v npm)" ] && export NODE_PATH=$NODE_PATH:`npm root -g`