Art, Programming, Linux

Andrew's notes

Home Posts Tags

Setup Node Version Manager

Installing NVM on Linux and managing different Node versions

Published on May 18, 2023 631 words 3 min read Linux

Image by Lautaro Andreani

Today i somehow managed to bork my installation of npm. Cleaning up the current installation and getting a new one did not help. Long dumb story. I then found out about NVM.

NVM or Node Version Manager is a valuable tool for developers working with NodeJS. It allows us to install and work with different Nodejs versions side-by-side and switch between them effortlessly. It provides us with flexibility and helps us ensure compatibility with different projects. Using a manager like NVM is even the recommended workflow1 by the folks over at npmjs.

Install #

Installing and/or updating NVM on linux is easy. All you need is one command.

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

# or

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

By the time you read this the link above might be out of date, be sure to check out their github for the latest version of nvm.

curl or wget are packages we use to download stuff from the internet. The -o- and -q0- flags are used display the results in the terminal rather than saved to a file and the | pipe symbol is used to pipe the results into the bash shell2.

After installing either close the currently open terminal and open a new one or source ~/.bashrc. Then we can simply ask for the version we want and nvm will automatically download the version of nodejs we asked for alongside a compatible npm package.

nvm install --lts # Long Term Support

# or 

nvm install vX.Y.Z

If you are following this tutorial you will notice that the first nodejs version we will download on our machine will also become the default version for nvm. All nodejs versions that we download will be located at ~/.nvm/versions.

Some of the information presented in this blog post can also be found over FreeCodeCamp’s blog. If you are looking for how to install NVM on a Windows system check out the article there.3

Basic Usage #

After you install a version of node with nvm install you can tell NVM to currently use it with the nvm use vX.Y.Z command.

If you would like to set a specific node version to be the working default you can nvm alias default vX.Y.Z.

To uninstall a version of node you use nvm uninstall vX.Y.Z.

.nvmrc #

By creating a .nvmrc file in your project you can set a node version you want the project to use. Then when you nvm use when inside the project directory nvm will default to using the node version we specified inside the file.

echo "5.9" > .nvmrc

echo "lts/*" > .nvmrc # to default to the latest LTS version

echo "node" > .nvmrc # to default to the latest version

The commands above simply create a .nvmrc file that contains the characters inside the brackets.

Uninstalling NVM #

To remove the ~/.nvm directory and perform a clean uninstall, you can follow these steps:

rm -rf ~/.nvm

This command will delete the ~/.nvm directory and all its contents, including the installed Node.js versions.

Then use your preferred text editor and remove any lines related to NVM near the bottom of the file. Save the file and exit the editor. We will use nano :

nano ~/.bashrc

Close and reopen the terminal to ensure the changes take effect.

Verify that NVM and the associated Node.js versions are no longer installed:

nvm --version
node --version
npm --version

Running these commands should result in “command not found” errors, indicating that NVM and the associated Node.js versions have been successfully uninstalled.

By following these steps, you can perform a clean uninstallation of NVM and remove all the Node.js versions that were installed through NVM.

Resources #

---

If you found this post useful please consider donating a small amount to support my effort in creating more content like this.