Install Ubuntu Node.JS

Install Ubuntu Node.JS

October 2, 2022 0 By Tobias

Ubuntu and Ubuntu Desktop is one of the most popular Linux distributions. Node.js is a cross-platform open source JavaScript runtime that can execute JavaScript code outside of a web browser and is often used for REST-APIS, but also entire web apps can be realized with it.

There are three ways to install Node.js on Ubuntu:

  • Using apt to install the node-js package from the Ubuntu default software repository.
  • Using apt with an alternative PPA software repository to install specific versions of the nodejs package
  • Installing nvm, the Node Version Manager, and using it to install and manage different versions of Node.js

For most users the normal apt installation is sufficient. If you need a specific older or newer Node.JS version, you should use the PPA repository. If you want to switch frequently between different Node.js versions, you should choose the nvm method.

Option 1 – Install Node.js with Apt from the default repositories.

First, we update the local package index:

sudo apt update

And then install Node.js:

sudo apt install nodejs

Verify that the installation was successful by querying node for the version number:

nodejs -v
Output
v10.19.0

When you work with Node.js you mostly use Node.js packages. Like Node.js Express, which can start a simple web server. For this you need the Node.Js package manager:

sudo apt install npm

This allows modules and packages to be installed for use with Node.js.

Option 2 – Install Node.js with Apt using the NodeSource PPA.

To install a different, older or newer, version than in the Ubuntu standard software repositories, you can use a PPA (personal package archive) maintained by NodeSource. These PPAs have more versions of Node.js than the official Ubuntu repositories.

First we install the PPA to get access to the PPA. For this we use the curl command with the desired Node.js version for Ubuntu.

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

In the Official Documentation you can see the available versions. Check the contents of the downloaded script with nano (or any other text editor)

nano nodesource_setup.sh

If the script can be run safely, we also do this with the following command:

sudo bash nodesource_setup.sh

The PPA is added to your configuration and your local package cache is automatically updated. You can now install the Node.js package in the same way as we did for option 1:

sudo apt install nodejs

Verify that the installation was successful by querying node for the version number:

nodejs -v
Output
v10.19.0

The nodejs package from NodeSource includes both the node binary and npm, so there is no need to install npm separately.

Option 3 – Installing Node with Node Version Manager

Another way to install Node.js that is particularly flexible is to use nvm, the Node Version Manager. With the Node Version Manager you can install and manage different independent versions of Node.JS and other packages.

To install NVM on Ubuntu, you need to copy the curl URL from the project’s Github page and run the command:

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

Now you can view the available Node.js versions for Ubuntu.

nvm list-remote

One of the listed versions can be installed with the following command:

nvm install v13.6.0

Then you can check which Node versions are actually installed now:

nvm list

Additionally, you will see aliases for the various long-term support (or LTS) versions of Node:

Output
. . .
lts/* -> lts/erbium (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.20.1 (-> N/A)
lts/erbium -> v12.16.3 (-> N/A)

For example, to install the latest long-term support version erbium, we run the following command:

nvm install lts/erbium

With nvm use you can switch between the installed versions:

nvm use v13.6.0

Now Node.JS is installed on the Ubuntu Server or Ubuntu Desktop.