Install nodejs using nvm

2020-11-21

In this short article we will take a look at nvm (node version manager) and use it to install different node versions.

Nvm is available on github (opens new window) and the installation is very simple. For the sake of this article we are going through it anyways. Please replace the version with the latest release tag from the release page (opens new window).

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.0/install.sh | bash

This will download and execute the install.sh script. Also it will add the following to your .bashrc config file. If you are using zsh then you will have to add it to your ~/.zshrc file.

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

Source your configuration file using source ~/.bashrc or reopen your terminal to enable nvm. You can check if nvm is available by running the following.

nvm --version
# will return 
0.37.0

Now that we have verified that nvm is available, we can use it to install the latest node version.

nvm install node

The node alias referes to the latest version of node, at the time that I am writing this, this is v15.2.1.

node --version
# will return 
v15.2.1

Now let's say you need some older node version to execute some code in an older project. Then you can just install another node version as shown below.

nvm install v14
# will install v14.15.1

To list all your node versions you can run the following.

nvm ls
# will return
        v14.15.1
->      v15.2.1
default -> node (-> v15.2.1)
node -> stable (-> v15.2.1) (default)
stable -> 15.2 (-> v15.2.1) (default)
...

To temporary use a specific node version you can call the following.

nvm use v14.15.1

If you want to permanently want to change the default version to use, you can overwrite the default alias.

nvm alias default v14.15.1

And that's basically it 😁