Install Node.js and npm on Ubuntu 20.04

How to Install Node.js and npm on Ubuntu 20.04

Prateek Dagur Ubuntu

Not using Ubuntu 20.04? Choose a different version.

How to Install Node.js and npm on Ubuntu 18.04
How to Install Node.js and npm on Ubuntu 22.04

Not quite sure? Check your Linux version.

Table of Contents

Introduction

Node.js is an open source, cross-platform JavaScript runtime built on Chrome's V8 JavaScript engine. It is designed to execute JavaScript code outside of the web browsers. It allows developers to build fast and scalable back-end and network applications.

NPM stands for 'Node Package Manager'. It is the default package manager for Node.js and also the world's largest ecosystem of open source libraries. It is used to manage and install the node packages and their dependencies.

In this post, we will show you three different ways to install Node.js and npm on an Ubuntu 20.04 server:

  • From the official Ubuntu repositories. The first and easiest way to install Node.js and npm on an Ubuntu server is through the official distribution repository. The version of Node.js included in Ubuntu 20.04 is v10.19.0.
  • Using NodeSource PPA. You can also use NodeSource repository to install specific versions of Node.js.
  • Using nvm (Node Version Manager). You can use this tool to install and manage multiple independent versions of Node.js.

Select the installation method that is best suited to your environment. For many use cases, using default Ubuntu repositories should be sufficient. If you require specific newer (or legacy) Node versions, you should use the NodeSource PPA repository. Select the nvm method if you are actively developing Node apps and need to switch between node versions frequently.

Prerequisites

This guide assumes you're running Ubuntu 20.04. Before you begin, make sure your system has a non-root user account with sudo privileges. Follow the Ubuntu 20.04 initial server setup tutorial to learn how to do this.

Installing Node.js and npm from the Ubuntu repositories

At the time of writing, the Node.js version included with Ubuntu 20.04 repository is v10.19.0 which is an "End-of-Life release" now. This is technically outdated version, but it should be stable and sufficient for quick experimentation with the Node.js environment.

Warning: Node.js version v10.19.0 included with Ubuntu 20.04 is no longer supported or maintained. This version should not be used in production, and you should install a more recent version of Node.js using one of the other methods in this guide.

To install this version, you can use apt package manager. Update the package index first using following command:

sudo apt update

Then install Node.js and npm:

sudo apt install nodejs npm

Press Y when prompted to confirm installation. If prompted to restart any services, press ENTER to accept the defaults and proceed. The above command will install several packages, including the tools required to compile and install native addons downloaded from npm.

Once done, verify that you've installed node and npm:

node -v
npm -v
v10.19.0
6.13.4

Installing Node.js and npm using NodeSource PPA

NodeSource maintains PPAs (Personal Package Archives) containing multiple Node.js versions. These PPAs have more Node.js versions than the official Ubuntu repository.

As of the time of writing, NodeSource repo provides the following versions:

  • v18.x - Latest stable release
  • v16.x - Active LTS release
  • v14.x - On maintenance

01. First, we'll install the PPA to gain access to its packages. Use curl to download the NodeSource installation script for your preferred version. We'll install v18.x but if you want another Node.js version, replace 18.x with selective version string (16.x for instance).

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

You can always refer to the NodeSource documentation for detailed information on the available versions.

Above command will add the NodeSource package signing key, add an apt repository file, install the required packages, and update the local package cache automatically.

02. Once done, install Node.js and npm as you did in previous section. It may be better that you remove any previous Node.js and npm packages before installing the new version using sudo apt remove nodejs npm. This command will not affect your configurations.

sudo apt install nodejs

The NodeSource nodejs package includes both the node and npm binaries, so you don't have to install npm manually.

03. Verify the installation:

node -v
npm -v
v18.9.1
8.19.1

Optional: Install build tools

To compile and install native addons from npm, you'll need to install development tools:

sudo apt install build-essential

How to Remove NodeSource PPA

To fully remove Node.js installed from the NodeSource PPA method above:

sudo apt purge nodejs
rm -r /etc/apt/sources.list.d/nodesource.list

Installing Node.js and npm using NVM

NVM (Node Version Manager) is a bash script to maintain multiple independent Node.js versions. NVM is very flexible and allows to install any Node.js version that you want to use.

Visit the nvm's Github repo and copy either the curl or wget command from the README file. This will download and install the latest version of the nvm script.

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

Warning: Do not use sudo for this command as it will enable nvm for the root user.

The script clones the nvm repository from Github to ~/.nvm directory. To use it, you should either close and reopen the terminal or source your .bashrc file.

source ~/.bashrc

Once the script added in your PATH, verify the nvm installation:

nvm -v
0.39.1

You can list available Node.js versions using:

nvm list-remote

This command will print a long list of available versions.

...
        v14.20.0    (LTS: Fermium)
        v14.20.1    (Latest LTS: Fermium)
...
        v16.17.0    (LTS: Gallium)
        v16.17.1    (Latest LTS: Gallium)
         v17.0.0
         v17.0.1
...
         v18.1.0
         v18.2.0

To install latest available Node.js version:

nvm install node
...
Now using node v18.9.1 (npm v8.19.1)
Creating default alias: default -> node (-> v18.9.1)

Verify that the install was successful:

node -v
v18.9.1

Let's also install LTS version:

nvm install --lts

You can list versions you have installed by typing:

nvm list
->        v16.17.1
           v18.9.1
default -> node (-> v18.9.1)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v18.9.1) (default)
stable -> 18.9 (-> v18.9.1) (default)
lts/* -> lts/gallium (-> v16.17.1)
...

Currently active Node.js version is indicated by an arrow on the right (-> v16.17.1), and the default version is set to v18.9.1. The default version will be used when opening new shell session.

Note: If you also installed Node.js through apt, you may see a system entry in the output. You can activate the system-installed version using nvm use system.

To change currently active version, type:

nvm use 18.9.1
Now using node v18.9.1 (npm v8.19.1)

To change default Node.js version, enter:

nvm alias default 16.17.1

Good to know: Install Node.js release using aliases

You can also install a version based on the aliases. When you run nvm list, you can see some named aliases and the corresponding versions. For instance to install fermium, type:

nvm install lts/fermium

Conclusion

There are many ways to install Node.js and npm on your Ubuntu 20.04 server. The method best for your needs depends on your project configuration. While installing the packaged version from Ubuntu's repository is easier, using NodeSource PPA or nvm offers more flexibility.

Comments