DEV Community

Matteo Veraldi
Matteo Veraldi

Posted on

How to quickly setup NodeJS with NVM

If you'd like to see the video version Click here.

I had to re-install my WSL2 Ubuntu distro for Web Development, so I decided to document how I setup things starting from NodeJS.
These steps are meant to be executed on a Debian based Linux distribution, but they are valid also for Mac OSX.

NodeJS

I often switch from one project to another and each project requires a different version of NodeJS. I am too lazy to manually install and change my PATH variables to perform the switch so I always install NodeJS through nvm.

What is NVM?

NVM is a Node Version Manager, a CLI tool for NodeJS that provides utilities to install and switch from one version of Node to another in a matter of seconds.

Install NVM

Open a new terminal and paste this command:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
Enter fullscreen mode Exit fullscreen mode

Please note: If this post is too old, please make sure to replace v0.39.7 with the latest version of nvm.

Install NodeJS via NVM

Open a new terminal and paste this command:

nvm ls-remote
Enter fullscreen mode Exit fullscreen mode

from the list that will appear, select the version you'd like to install. If you are not sure, install the latest Long Term Support (LTS) version with this command:

nvm install --lts
Enter fullscreen mode Exit fullscreen mode

Add .nvmrc to your project

One of the best features of nvm is the .nvmrc. It is a file that you can add inside the root directory of your NodeJS project to allow nvm users to install and use the correct NodeJS version without having to think about it. It is very good for CI/CD too!

To create a .nvmrc, open a new terminal on the root directory of your project and paste this command:

node -v > .nvmrc
Enter fullscreen mode Exit fullscreen mode

To test if it works, paste this command:

nvm install && nvm use
Enter fullscreen mode Exit fullscreen mode

You may create a bash script inside your .bashrc that automatically looks for an .nvmrc file in your directory and performs the install and use for you. Click here to learn how.

Conclusions

This was a very quick introduction to how I setup NodeJS on my environment using nvm. If you have any questions please refer to the official documentation or contact me via my Social Links.

Top comments (0)