NVM stands for Node Version Manager. It is a utility that allows you to install and manage multiple Node JS versions simultaneously and switch between them on demand. Why would you need to do that? Because if you're working with multiple Node JS projects, or using tools built with Node JS, chances are you have run into version conflicts. As an example, let's say one of your projects requires Node JS version 10 whereas another project requires version 12. It is not possible to uninstall one version and install another every time you want to work on a project. This is where nvm comes handy as it allows you to have co-existing Node JS versions and switch between them with one command.

## Installing NVM on Linux

The easiest way to install nvm is to install it directly from Github. The Github repository provides an install script which you can download and run manually, or use the following command to download and run automatically -

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

This will clone the nvm repository to ~/.nvm and attempts to edit the correct profile file (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc) so that nvm is loaded whenever you start a new shell.

Note that if you're a fish shell user like me, this will not work because fish is not POSIX compliant. I recommend using Oh My Fish with the fish-nvm plugin.

Once you have installed NVM, you should restart your shell so that nvm is loaded. Or you can just source .bashrc or .zshrc, or if you're a fish user, run exec fish

You can check if NVM is loaded correctly by running nvm --version. If it runs without an error, you're good to go!

## Using NVM

Let's start using nvm by installing the latest version of Node JS. You can do that by running -

nvm install node

node is an alias for the latest version of node. This will download, compile and install the latest version of Node. This may take a while, so grab a coffee!

Once it is finished, since this is the first installed version, this will be set as your default Node version. You can verify this by running node -v. At the time of writing this article (13/05/2021), the latest version is 16.1.0

Now let's start installing multiple versions and see how we can manage them. First let's list all the versions available for download using the ls-remote command -

nvm ls-remote

This gives a long list of available versions.

You can install any one of these by using the install command. As an example, let's install v15.14.0 -

nvm install 15.14.0

Similar to the last time, it will download and install Node v15.14.0 on your computer. However if you run node -v you should still see 16.1.0 and not 15.14.0. This is because 16.1.0 is still your default version, and installing a new version does not change the default version.

You can switch to a new version by using the use command. Let's switch to 15.14.0 -

nvm use 15.14.0

Now when you run node -v, you should see the output is now 15.14.0 !

This switch however is temporary, which means if you restart your shell, or start a new shell, it will change again to the default version. If you want to permanently change the default version, you have to use the alias command -

nvm alias default 15.14.0

This will set your default version to 15.14.0 permanently.

You can list all the installed version using the command nvm ls

If you want to uninstall a version, you can use the uninstall command -

nvm uninstall 15.14.0

## Bonus: .nvmrc

If you have a project that requires a specific version of Node JS, instead of remembering that version and manually switching to that version every time you start working on the project, you can create a file named .nvmrc in the project root containing the required version number. Next time, when you run nvm use it will automatically load the specified version.

For example, the following will set the default version to 15.14.0 -

echo "15.14.0" > .nvmrc

Now when you run nvm use in that project, it will load and switch to 15.14.0.

If you want to run nvm use automatically when you cd into a directory containing .nvmrc, you can take a look here.

## Conclusion

In this tutorial, you learned how to install and set up NVM on your Linux machine, and how to install and switch multiple Node JS versions on demand. You also learned how to create a .nvmrc file to switch NVM versions automatically.