Node.js is an open-source, cross-platform, Javascript runtime environment that executes Javascript outside the browser, which lets developers create scalable backend functionality using Javascript.

Since it runs on almost all platforms (including Windows, Linux, Unix, Mac OS X, etc.), Node.js allows the creation of web servers and networking tools using Javascript programming language.

In this guide, you will learn how to install Node.js on your Ubuntu 20.04. You need to have root or sudo privileges and you’re ready to go.Installing Node.js with Apt

Installing Node.js with Apt

The easiest way to install Node.js is using the default Ubuntu repositories, let’s refresh them:

$ sudo apt update

Now let’s install the latest stable version of Node.js:

$ sudo apt install nodejs -y

Awesome, the -y flag will confirm that we’re agreeing for all required dependencies for Node.js, this will take few seconds/minutes depending on your hardware.

After it’s finished, you can check if it’s indeed installed:

rockikz@rockikz:~$ node -v
v10.19.0

We have the latest stable version installed, cool!

Installing Node.js using The Node Version Manager (NVM)

But what if you’re a Node developer and want to install a specific version of Node on your machine, well NVM comes into the rescue.

NVM (Node Version Manager) is a version manager for Node.js designed to be installed per-user and invoked per-shell.

Let’s install NVM, the below command is responsible for downloading and running the BASH script that is responsible for installing NVM:

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

Now if you open another terminal and run nvm, you will face no problem. However, in the current shell, you won’t be able to access it, you need to re-execute .bashrc file:

$ source ~/.bashrc

Awesome, let’s check if NVM is installed:

$ nvm --version
0.35.3

Great, now to list all the available Node.js versions to install, you can run the following command:

$ nvm list-remote
        ...
        v14.3.0
        v14.4.0
        v14.5.0
        v14.6.0
        v14.7.0
->      v14.8.0

This will output a very long list of all Node.js versions, I’ve stripped the output to the only few last versions.

Now if you want to install a specific version, you can using the following:

$ nvm install v14.8.0

Let’s check our node version again:

$ node -v
v14.8.0

As you can see, it was changed to the version we just installed by default, to list the available versions in your machine, you can list them:

$ nvm ls

To get back to the system version (installed previously by apt), you can use:

$ nvm use system
Now using system version of node: v10.19.0
$ node -v
v10.19.0

Or if you want to use a specific installed version, you can replace system by the version, as follows:

$ nvm use v13.14.0

This requires v13.14.0 to be installed (using nvm install) and will change to this version.

Conclusion

Congratulations, you have managed to install Node.js and handle specific versions on Ubuntu 20.04 as well as on other Linux distributions such as Debian, Mint, Kali, etc.

Related: How to Install Apache Cassandra on Ubuntu 20.04

Happy Installing ♥