How to Install Node.js on Ubuntu 20.04

Node.js is a JavaScript runtime. In this tutorial, we’ll go over the different ways to install Node.js on Ubuntu 20.04 and their benefits.

Installing Node.js with apt

This is the easiest and most sufficient installation method for many purposes. In addition to Node.js itself, we will immediately install its package manager — npm. Node.js packages are libraries or tools that can be downloaded using npm.

sudo apt update && sudo apt install nodejs npm

You can check the success of the installation, and at the same time, you can find out the version using the command:

nodejs -v

Installing Node.js with PPA

Installing via the PPA gives you a choice of version. Check out the available versions in the documentation. We’ll also need curl to continue.

sudo apt install curl

Now we will download and run the script of the corresponding version, which will add the PPA to the system repositories.

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

And install Node.js from the added repositories.

sudo apt install nodejs

Let’s check the installed version.

node -v

In this case, npm is already installed.

Installing Node.js with NVM

NVM is Node.js Version Manager. It allows you to install different versions of Node.js with npm packages at the same time and switch between them at runtime.
To install the latest version of the NVM installation script, go to the Installing and Updating part of the project page on GitHub, copy the link and run it on your Ubuntu 20.04.

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

Now you need to log out of your account and log back in. The second option is to execute the following commands to ensure the operation of NVM:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && . "$NVM_DIR/bash_completion" bash_completion

At this point, the NVM is ready to use. To get a list of versions available for installation:

nvm list-remote

Choose a version and install:

nvm install 14.16.0

View the list of installed versions:

nvm list


-> v14.16.0
default -> 14.16.0 (-> v14.16.0)

To select a Node.js version, use the command indicating the required version. Thus, you can switch between different versions if there are several installed.

nvm use v14.16.0

If, in addition to the versions installed using nvm, the system also contains the version installed using apt, it will be marked as system. To switch to it, use:

nvm use system

In addition, the nvm list output shows LTS releases of Node.js. Not installed ones look like this:

lts/dubnium -> v10.24.0 (-> N/A)

To install them, specify the release alias:

nvm install lts/dubnium

To check the currently used version, use the command:

node -v