How to install Node.js in Ubuntu 20.04

How to install Node.js in Ubuntu 20.04

In order to choose a Node.js version to work with depends totally on your audience and the environment that you have at your disposition. You should always target the stable version of Node that is quite mature and dependable as it has proven stability and commitment to keep that version as it is. If you are a developer testing new stuff, deploying stuff to production that isn't meant to be used by other persons but you, LTS is then your way to go to access those features that may be not available on the latest stable release. You will probably need to use Node.js on your server along with other tools so you need to choose the best way to install Node.js in your Ubuntu server.

In this article, I'll explain to you 3 different ways to install Node.js in your Ubuntu 20.04 desktop and server version.

A. Install using the Node Version Manager (recommended)

The recommended way to proceed with the installation of Node.js is to use the NVM tool (Node Version Manager). This tool is a fully POSIX-compliant bash script to manage multiple active Node.js versions in your system. As probably, you will end up with compatibility problems with some of the projects you may want to host on your server, you should be able to switch between versions.

In order to use NVM, you should run this sh file that will do the trick. If you agree with the content of the script and what's going to do, you may simply download and run it directly in the terminal with the following command:

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

This will initialize NVM as well on the command line, however, you need first to obtain the .bashrc file with the following command:

source ~/.bashrc

And that's it, you should be able to use the NVM tool now. You may want to verify the available versions of Node.js with the following command:

nvm list-remote

This will list absolutely all the versions available of Node.js, from the 0.1.14 to the latest version till the date 15.4.0:

...

v14.6.0
v14.7.0
v14.8.0
v14.9.0
v14.10.0
v14.10.1
v14.11.0
v14.12.0
v14.13.0
v14.13.1
v14.14.0
v14.15.0   (LTS: Fermium)
v14.15.1   (Latest LTS: Fermium)
v15.0.0
v15.0.1
v15.1.0
v15.2.0
v15.2.1
v15.3.0
v15.4.0

Now your task is to choose one of the versions that you need. For example, if i want to install a specific version (the 15.4.0) i would run the following command:

nvm install v15.4.0

And wait until the installation finishes. The command should generate an output similar to:

Downloading and installing node v15.4.0...
Downloading https://nodejs.org/dist/v15.4.0/node-v15.4.0-linux-x64.tar.xz...
######################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v15.4.0 (npm v7.0.15)
Creating default alias: default -> v15.4.0

All the Node.js versions include NPM as well, so you won't need to install it manually. Now, as we mentioned, using this option you will be able to switch between versions of Node in your system. Now if I want to install another version, I would use the same command specifying the version that I want (e.g. nvm install 14.15.1). Note that installing a new version makes it the default one to use, so after installing the 14.15.1 version, printing the version of node with the following command:

node -v

Would generate the following output:

v14.15.1

Now if I want to switch to my first installed version, I would be able to list all the installed versions with the following command:

nvm list

This will print the installed versions in the system:

->     v14.15.1
        v15.4.0
default -> v15.4.0

If I want to use the v15, I just need to specify the version to the nvm use command like this:

nvm use v15.4.0

And now node -v would print v15.4.0.

B. Installing Node.js from the apt repositories

In order to install Node.js with just a couple of commands using the default apt repositories, you will need to update them first:

sudo apt update

Then, simply run the following command to obtain Node.js and install it:

sudo apt install nodejs

The installation of this package will take about 31MB of space on your disk. There shouldn't be any problem and you will be able to use now Node.js from the command line. For example, print the current version in the terminal with the following command:

nodejs -v

Till the date, the latest version of Node in the APT repositories is the following one:

v10.19.0

After installing node.js, you will need as well of course to install NPM. You can do this with the following command:

sudo apt install npm

The installation will take around 200MB of space on your disk. After the installation, you should be able to install packages as you would usually do with NPM. You can check the version of NPM with the following command:

npm -v

Which should output something like:

6.14.4

The only disadvantage of this approach is that your node.js version is linked to the one available on the APT repositories, you won't be able to switch from one version to another, and so on. If you still want to use this kind of installation but using more recent versions of Node.js, follow the C option.

C. Installing Node.js using APT with a NodeSource PPA

If you want to work with Node.js using APT but with recent versions of Node.js, you can rely on a Personal Package Archive maintained by NodeSource. The mentioned PPA's have at disposition more versions of Node.js than the one offered in the default repositories of Ubuntu.

As first, install the PPA with the following instructions in your home directory:

cd ~

Then download the preferred version of the package replacing setup_XX.X with the version of node that you want, for example, to install the latest V15, the command would be the following one:

curl -sL https://deb.nodesource.com/setup_15.4 -o nodesource_setup.sh

Then run the SH file with the following command (you may want to read the content of the file before running it):

sudo bash nodesource_setup.sh

Now, you can simply install Node.js as explained with APT:

sudo apt install nodejs

After installing, you can easily verify the installed version with the following command:

node -v

Unlike the second option using APT, the installed versions from the NodeSource PPA contain already NPM so you won't need to install it manually.

Happy coding ❤️!

This could interest you

Become a more social person