Log In

How to Install Node.js on Ubuntu 20.04

How to Install Node.js on Ubuntu 20.04
Reading time: 8 min
Hostman Team
Technical writer

Node.js is an environment for executing JavaScript code. With Node.js, programmers can write server-side code in JS and create full-fledged desktop programs.

At the heart of Node.js is the V8 engine developed at Google and used in Chrome. It compiles JavaScript into machine code that the processor can understand. However, the engine alone is not enough; on the server side, you need to be able to work with files, networking, etc. Therefore, Node.js developers added these and other features to V8 with the help of their own code and additional libraries. The result is a tool that turns JS into a general-purpose language.

Node.js has gained immense popularity among developers for several reasons:

  • They can write server- and client-side code in the same language.

  • Single-threading, asynchrony, and non-blocking IO provide for higher speed. The next task can be started without waiting for the previous task to finish.

  • NPM (Node Package Manager) helps developers to share tools and modules and manage their dependencies easily.

In this article, we will look at several ways to install Node.js. As a test machine, we will use a server with Ubuntu 20.04 and create a user with administrator privileges. You can rent your own server to experiment with Node.js from Hostman.

Uninstalling old versions of Node.js

Before installing Node.js on Ubuntu, you should check for and remove old versions of the program. The thing is that if you leave older versions, some actions can run through them, which can cause hard-to-track conflicts.

Let's check if Node.js is installed. To do this, list all installed packages and filter the output:

dpkg -l | awk '{print $2}' | grep node



As you can see, the packages are present. Let's remove the nodejs package along with its configuration files, as well as the dependencies:

sudo apt-get purge nodejs -y && sudo apt autoremove -y 

Check if there are any packages left:

dpkg -l | awk '{print $2}' | grep node

Installing with apt through Ubuntu repositories

Using apt for installation is the easiest method that will work for beginners. Ubuntu repository stores the stable, but not the latest version.  

Update the repositories on the machine:

sudo apt update

Next, install Node.js using the apt install nodejs command. The -y flag will automatically answer "yes" to all the program's prompts:

sudo apt install nodejs -y

This is enough to start creating your own Node.js programs. In the future, you will need npm to download additional modules. Use this command to install it:

sudo apt install npm -y

Output the version of the programs to check if the installation is correct:

node -v && npm -v



Installing from the official repository (PPA)

The NodeSource repository contains the latest Node packages. Installing via PPA is the recommended method for production. You can find more information about this method in the official documentation. For most popular distributions, including Ubuntu, a special script will automatically set everything up. If you are not a fan of curl <url> | bash -, or if you are using an unsupported distribution, you can install it manually.

Let's use the script. First, let's display it in the terminal and make sure it's safe:

curl -fsSL https://deb.nodesource.com/setup_18.x

Instead of 18.x you can specify the desired version. Now the repository contains v18 (current), v17, v16 (LTS), v14. 

After checking, let's pass the script to the shell:

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -

After customization, the script will recommend to install some packages:

sudo apt install nodejs gcc g++ make -y

See if everything is installed:

node -v && npm -v



Now we have the latest packages of Node.js and npm.

Installing with NVM

NVM is a utility for installing and managing multiple versions of Node.js. It allows you to change versions with a single command. Installing with nvm is recommended for developers who want to install Node.js locally on their machine. You can find more information about installing with nvm here.

sudo apt update && sudo apt install build-essential libssl-dev -y

NVM is installed via an installation script. To download and execute it, use curl/wget:

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

Before sending it to the shell, you can check the script's contents. After executing the script, we will be prompted to restart the terminal, but we can reconfigure the environment in the current shell.

source ~/.bashrc

Other distributions or shells may have a different file name.

Let's check the available versions of Node.js:

nvm list-remote

You will see a list of releases starting from 2010. For example, let's install v14.19.3:

nvm install 14.19.3

We can see that npm is installed together with Node.js. Let's see the list of installed versions:

nvm list


-> v16.1.0

The command outputs the installed versions and their aliases, with the -> character indicating the active one. The system entry means Node.js installed with apt. At the bottom of the list are the aliases of the different LTS releases of Node.js:

lts/fermium -> v14.19.3
lts/gallium -> v16.15.0 (-> N/A)

What is LTS? If you go to the Node.js website, you will find two versions to download: LTS and current. Node.js is growing and evolving rapidly, so they focused on two different release lines. The Long-Term Support version is a stable, tested version with a long support period. The current version is ahead of LTS in terms of functionality but may contain more bugs.

You can use an alias to install the LTS release:

nvm install lts/carbon

You can change versions as follows:

nvm use v14.19.3


Now using node v14.19.3 (npm v6.14.17)

Check the versions:

node -v && npm -v



Now, let's try to delete v14.x. First, deactivate it:

nvm deactivate 14.19.3


/home/username/.nvm/*/bin removed from ${PATH}

And uninstall:

nvm uninstall 14.19.3


Uninstalled node v14.19.3

With its flexible management, NVM makes it easy to upgrade Node.js on Ubuntu.

Non-standard installation methods

Sometimes, unique situations arise where the previous methods may not work, such as installing Node.js into an embedded system without external access, etc. In this case, you can install Node.js from an archive or build it from source. We recommend using these methods if you are an experienced user because it may be tricky to update the software and install dependencies in the future. 

Installing from archive

Go to the official Node.js site and download the archive with the version for your architecture:

wget https://nodejs.org/dist/latest-v15.x/node-v15.14.0-linux-x64.tar.gz

Since we downloaded a compressed archive, we will need tar for unpacking the archive (tar is included by default in almost all distributions). We will unpack the archive to /usr/local, so when unpacking, we need to remove the leading directory in the file names. To do this, we'll use the --strip-components 1 flag:

sudo tar -C /usr/local --strip-components 1 -xf node-v15.14.0-linux-x64.tar.gz


node -v && npm -v



Build from source code

You can find all the information about the building process on the project’s Github page. There are almost no external dependencies. The following packages may be required for building on Ubuntu: 

  • gcc and g++

  • make

  • python3

Install them:

sudo apt install python3 g++ make python3-pip

Now download and unzip the archive:

wget https://nodejs.org/dist/v16.15.0/node-v16.15.0.tar.gz
sudo tar -tvf node-v16.15.0.tar.gz
cd node-v16.15.0.tar.gz

Run the configuration script build. The -j4 flag makes the make command run 4 simultaneous tasks, which will reduce build time:

sudo ./configure
sudo make -j4

Install Node.js:

sudo make install


So, we've looked at several ways to install Node.js. Let's say you've chosen one of the methods and installed what you need. To make sure that everything works, outputting the version is not enough. Let's create a very simple application and check it works correctly. 

Create a text file with any editor you like:

vim test.js

We will try to output the string to the console. To do this, write:


Save the file. Now run the program:

node test.js




Today, Node.js is one of the leading technologies for web development. Many large companies such as PayPal, Yahoo, eBay, General Electric, Microsoft, and Uber use this platform to build their websites.

There are different ways to install Node.js. Which method to choose depends on your goals. For beginners, the version from the distribution's repository is fine. Newer versions can be obtained from NodeSource's PPA. The NVM program will help you install several versions at once and manage them conveniently. And, in case of non-standard situations, you can install Node.js from an archive with binary versions or build from source code.