How to Install Node.js and npm on Ubuntu 20.04

Updated on

6 min read

Install Node.js and npm on Ubuntu 20.04

Node.js is a cross-platform JavaScript runtime environment built on Chrome’s JavaScript, designed to execute JavaScript code on the server side. It is generally used to build back-end applications but is also popular as a full-stack and front-end solution. Npm is the default package manager for Node.js and the world’s largest software registry.

In this tutorial, we will guide you through three different methods of installing Node.js and npm on Ubuntu 20.04:

  • From the NodeSource repository. Use this repository if you want to install a different Node.js version than the one provided in the Ubuntu repositories. Currently, NodeSource supports Node.js v14.x, v13.x, v12.x, and v10.x.

  • Using nvm (Node Version Manager). This tool allows you to have multiple Node.js versions installed on the same machine. If you are a Node.js developer, then this is the preferred way of installing Node.js.

  • From the standard Ubuntu repositories. This is the easiest way to install Node.js and npm on Ubuntu. The version included in the Ubuntu repositories is 10.x. However, Nodejs 10 is no longer maintained and should not be used in production.

Choose the installation method that is appropriate for your environment. If you are unsure which Node.js version to install, consult the documentation of the application you will deploy.

Installing Node.js and npm from NodeSource

NodeSource is a company focused on providing enterprise-grade Node support. It maintains an APT repository containing multiple Node.js versions. You can use this repository to install any version of Node.js you need.

The first step is to install the dependencies necessary to add a new repository . Most likely, you will already have those packages installed on your system, but some packages may be missing:

sudo apt updatesudo apt install ca-certificates curl gnupg

Next, import the Nodesource repository’s GPG key to your system:

sudo mkdir -p /etc/apt/keyringscurl -fsSL | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg

At the time of writing, NodeSource repository provides the following versions:

  • v21.x - The latest stable version.
  • v20.x - The latest LTS version.
  • v18.x - The previous LTS version.
  • v16.x - EOL-ed

We’ll install Node.js version 20.x. If you need another Node.js version, for example, 18.x, change the NODE_MAJOR=20 with NODE_MAJOR=18. Run the following command to create the NodeSource repository file:

NODE_MAJOR=20echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg]$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list

Once the repository is enabled, install Node.js and npm:

sudo apt updatesudo apt install nodejs

The nodejs package contains both the node and npm binaries.

Verify that the Node.js and npm were successfully installed by printing their versions:

node --version
npm --version

To be able to compile native addons from npm, you’ll need to install the development tools:

sudo apt install build-essential

Installing Node.js and npm using NVM

NVM (Node Version Manager) is a bash script that allows you to manage multiple Node.js versions on a per-user basis. With NVM, you can install and uninstall any Node.js version that you want to use or test.

Visit the nvm GitHub repository page and copy either the curl or wget command to download and install the nvm script:

curl -o- | bash

Do not use sudo , as it will enable nvm for the root user.

The script will clone the project’s repository from GitHub to the ~/.nvm directory:

=> Close and reopen your terminal to start using nvm, or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/" ] && \. "$NVM_DIR/"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

As the output above says, you should either close and reopen the terminal or run the commands to add the path to nvm script to the current shell session. You can do whatever is easier for you.

Once the script is in your PATH, verify that nvm was properly installed by typing:

nvm --version

To get a list of all Node.js versions that can be installed with nvm, run:

nvm list-remote

The command will print a huge list of all available Node.js versions.

To install the latest available version of Node.js, run:

nvm install node

The output should look something like this:

Checksums matched!
Now using node v21.5.0 (npm v10.2.4)
Creating default alias: default -> node (-> v21.5.0)

Once the installation is completed, verify it by printing the Node.js version:

node --version

Let’s install two more versions, the latest LTS version, and version 18.10.0:

nvm install --ltsnvm install 18.10.0

You can list the installed Node.js versions by typing:

nvm ls

The output should look something like this:

->     v18.10.0
default -> node (-> v21.5.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v21.5.0) (default)
stable -> 21.5 (-> v21.5.0) (default)
lts/* -> lts/iron (-> v20.10.0)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.12 (-> N/A)
lts/fermium -> v14.21.3 (-> N/A)
lts/gallium -> v16.20.2 (-> N/A)
lts/hydrogen -> v18.19.0 (-> N/A)
lts/iron -> v20.10.0

The entry with an arrow on the right (v18.10.0) is the Node.js version used in the current shell session, and the default version is set to v21.5.0. The default version is the version that will be active when opening new shells.

If you want to change the currently active version, enter:

nvm use 18.10.0
Now using node v18.10.0 (npm v8.19.2)

To change the default Node.js version, run the following command:

nvm alias default 18.10.0

For more detailed information about how to use the nvm script, visit the project’s GitHub page.

Install Node.js and npm from the Ubuntu repository

The Node.js version included in the Ubuntu 20.04 repositories is 10.19.0, has been EOL-ed since 30 Apr 2021. This version is no longer actively supported and will not receive security updates.

The installation is straightforward. Run the following commands to update the package index and install Node.js and npm:

sudo apt updatesudo apt install nodejs npm

The command above will install several packages, including the tools necessary to compile and install native addons from npm.

Once done, verify the installation by running:

nodejs --version


We have shown you three different ways to install Node.js and npm on your Ubuntu 20.04 server. The method you choose depends on your requirements and preferences. Even though installing the packaged version from the Ubuntu or NodeSource repository is easier, the nvm method gives you more flexibility for adding and removing different Node.js versions on a per-user basis.

If you want to use Yarn to manage your application dependencies, check our tutorial on how to install and use yarn on Ubuntu 20.04 .

Feel free to leave a comment if you have any questions.