How to Install Node.js on Ubuntu 20.04
4 min read

How to Install Node.js on Ubuntu 20.04

Node.js is a JavaScript (JS) runtime for server-side programming. It is useful for developers when creating scalable back-end functionality using Javascript. In this tutorial, we will explore two different ways to install Node.js on Ubuntu 20.04 - the latest LTS version of Ubuntu.
How to Install Node.js on Ubuntu 20.04

Node.js is a JavaScript (JS) runtime for server-side programming. It is useful for developers when creating scalable back-end functionality using Javascript.

Introduction

In this tutorial, we will explore three different ways to install Node.js on Ubuntu 20.04 - the latest LTS version of Ubuntu.

The first section will go through the default installation on the distro using Apt. The second section will use NodeSource to install different versions of Node.js. Lastly, the third section will utilize Node Version Manager (nvm) to install other versions of Node.js as needed.

Installation of Node.js through default repositories using Apt

Out of the box Ubuntu 20.04 contains a stable version of Node.js. The version included in the repository will not be the most current version during installation, but it will be perfectly usable by whomever needs a super quick install.

To get started in installing this version of Node.js, begin with updating apt.

sudo apt update

Next, a simple one liner to install Node.js.

sudo apt install nodejs

After the install is complete, verify the version that was installed.

nodejs -v

This will output something similar to v10.xx.x. Note, the "xx.x" will be the version.

Additionally, most individuals who install Node.js generally will need the Node.js Package Manager. If you do, you can install this as well with the default repositories in Ubuntu 20.04 by the below command.

sudo apt install npm

After the installation of npm is done, you will have the ability to install Node.js packages and modules.

If this version of Node.js is fine, then you are done with the installation of Node.js! However, if the version of Node.js installed using apt and the default repositories is not sufficient, move onto the next section to learn how to install other versions of Node.js using a PPA (personal package archive).

Installation of Node.js through a NodeSource PPA with Apt (different Node.js versions)

Depending on your use case the version of Node.js from the default repositories of Ubuntu 20.04 may not be sufficient. This section will show you how you can install different versions (i.e. v10, v12, v13, and even v14) of Node.js using a NodeSource PPA.

To get started, we will need to add the PPA to Ubuntu 20.04 so the packages can be accessed. First, change the current directory to your home directory and use curl to fetch the NodeSource script.

cd ~

curl -sL https://deb.nodesource.com/setup_14.x -o nodesource_setup.sh

Note: In the above script, if you prefer v10, v12, or v13 of Node.js, change "setup_14.x" to your desired version string. For example, "setup_10.x". You can refer to the official documentation for NodeSource here.

Once you have inspected and verified the file (using your desired editor - nano for the win! - i.e. nano nodesource_setup.sh), then run the script.

sudo bash nodesource_setup.sh

After the script has run, you will be able to install Node.js as you did in the first section.

sudo apt install nodejs

After the install is complete, verify the version that was installed.

nodejs -v

This will output something similar to v14.xx.x. Note, the "xx.x" will be the version.

Installation of Node.js through Node Version Manager (nvm) (different Node.js versions)

If you need a very specific version of Node.js for a project or for an application that you would like to run, the Node Version Manager (nvm) is likely the route that you should be looking for. It is a fairly flexible method to installing the Node.js version that you need along with the associated packages - all in one.

To begin with, you will need to grab the installation script. At the time of writing this, v0.37.2 is the most recent version. However, you can check the official GitHub README here to find the most recent installation script.

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

Note, the above script had the "| bash" part of the script removed. This is because it is always best practice to inspect the downloaded script file before executing it. After you have verified the script, rerun the above command with the the bash portion appended to the end.

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

After the install is complete, nvm will be installed for your user. Now, you will need to source the .bashrc file.

source ~/.bashrc

To find the available versions of Node.js through nvm, you can run the below command.

nvm list-remote

Take a look through the list that will show and find the one that suits your needs! Once you have found the version that you would like to install, you can install it by running the following command.

nvm install v13.2.0

To verify that the version selected has been installed, you can do so by running this command. This will list more than one version installed if applicable.

nvm list

You will see the currently in-use version at the top of the output prepended with '->'. You will also see other aliases used by node and which versions they relate to.

You may also see the version of node that was installed using Apt here as well - referred to as the system version. This version can be used by issuing the following command.

nvm use system

You may even see LTS versions listed that are referred to by their alias - such as lts/argon -> v4.9.1 (-> N/A). Thus, we can install one using a command like below to install the latest LTS version.

nvm install lts/erbium

The Node Version Manager also makes it easy to switch between versions that are installed. This can be done using the use flag.

nvm use v13.6.0

You will see output like (which also lists the compatible npm version)

Now using node v13.6.0 (npm v6.13.4)

And, like always, you can verify the version of Node.js that is being used.

node -v

To Conclude

As you can see, there are a few different ways to install Node.js on Ubuntu 20.04. Which method you end up using will depend on your own situation and what you plan to use Node.js for. For many, the default repositories and Apt will suffice, but for those more complex tasks a NodeSource PPA or the Node Version Manager will end up saving the day.

Should you have any trouble, send me a message and I'll do my best to help out.

Enjoying these posts? Subscribe for more