How to Install Node.js on Ubuntu

This tutorial will teach you how to install the Node.js runtime on the Ubuntu operating system.

Installing Node.js on Ubuntu

Node.js is an open-source runtime that allows you to execute JavaScript locally without needing a web browser.

This runtime is often used for server-side programming, allowing you to write a scalable and fast web backend in a reasonably well-known language.

This guide will teach you how to install the Node.js runtime on your Ubuntu system. These steps will work on all LTS releases of Ubuntu. For example, you can install the JavaScript runtime on Ubuntu 22.04, 20.04, 18.04, and more.

There are three different methods that you can use to install Node.js on Ubuntu. Each method has its pros and cons.

Installing Node.js from the Ubuntu Package Repository

For this first method, we will be installing Node.js to Ubuntu straight from the default package repository.

This method means you will be stuck with the version of Node.js provided with your release of Ubuntu. The older release of Ubuntu that you are using, the older version of the runtime will be installed.

For example, Ubuntu 20.04 has the no longer supported 10.19 version of Node.js in it’s repository. But Ubuntu 22.04 contains the newer, but also unsupported, 12.22 release.

If you require a version of Node.js that is kept up to date, please go to “Installing from the Official Node.js Repository on Ubuntu” or “Installing Node.js to Ubuntu using NVM“.

1. We will need to update the package list before we can install Node.JS from the default Ubuntu package repositories.

Updating the package list ensures two things for the system.

  • First, the package list cache will be updated, ensuring we are grabbing the latest release.
  • Secondly, the cache contains links to where these packages exist. Updating ensures we should only contain live links so “apt” won’t attempt to download NodeJS from an offline mirror.

Update your systems package cache using the following command on your Ubuntu systems terminal.

sudo apt update

2. After the package update completes, we can finally install Node.js to our Ubuntu system.

Run the following command to install the software using the “apt” package manager.

sudo apt install nodejs

3. You can verify that you have successfully installed Node.js on Ubuntu by querying it for its version.

To get the version of Node.js, you need to use the following command. The “-v” option tells node to output its version.

node -v

Running this on our Ubuntu 22.04 after installing Node.js revealed the following version number

v12.22.9

4. Once you have Node.js installed, you may also want to install NPM.

NPM is the package manager for Node.js and is not installed by default when installing Node.js from the Ubuntu package repository.

All you need to do to install NPM is to run the following command on your device.

sudo apt install npm

Once installed, you can install any additional packages and modules you might need for your Node.JS program.

Installing from the Official Node.js Repository on Ubuntu

Using the NodeSource package repository is one of the best ways to install Node.js on Ubuntu. In addition, installing the runtime using this method is the best way to ensure that you are using a currently supported release.

For most people, this would be our recommended way of installing Node.js on Ubuntu.

1. Before proceeding, we should ensure that the package list is up to date by using the following command.

sudo apt update

2. Once the package list has been updated, we can install the packages we need to install Node.js with the below command.

sudo apt install curl build-essential

3. Before proceeding, make a note of the version that you want to install. At the time of writing, the latest releases are as follows. You can find the latest version numbers from the

  • 18.xCurrent Release.

    It comes with the latest features but is potentially more unstable.
  • 16.xCurrent LTS Release.

    It doesn’t contain the newest features but is actively worked on still.
  • 14.xMaintenance LTS Release.

    No longer actively maintained, the Node team will fix critical bugs as they pop up.

4. Once you have decided on what release of Node.js you want to install on Ubuntu, we can add the NodeSource package repository by using one of the following commands

Each of these commands grabs the setup script from the “nodesource” website and then pipes it (| character) to the terminal. These scripts handle the process of adding the NodeSource repository to your device.

Node.js Current Release on Ubuntu

The following command will set up the package repository for the latest release on Ubuntu.

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

Node.js LTS Release on Ubuntu

You will add the repository for the current LTS release using the command below.

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

Node.js v14.x on Ubuntu

Only use the command below if you need to use an older release on your Ubuntu device. Typically, most users will want to stick to the current or LTS releases.

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

5. With the NodeSource package repository added to your device, we can install software from it.

You only need to run the following command to install Node.js on Ubuntu using the new repository. During installation, the “apt” package manager will also install NPM.

sudo apt install nodejs

6. We can verify that we installed the correct version of Node.js by using the command below.

The “-v” option tells Node.js to output its version to the terminal.

node -v

After installing the “current” release of Ubuntu, we got the following version number.

v18.4.0

7. Finally, we can verify that NPM was also installed on our Ubuntu device alongside Node.js.

We can achieve this by getting it to report its version number using the following command.

npm -v

Below is the version that was installed alongside Node.JS on our Ubuntu device.

8.12.1

Installing Node.js to Ubuntu using NVM

The final method for installing Node.js to Ubuntu is to use Node Version Manager, which often goes by the name NVM.

This software is a version manager for Node.js and allows you to have multiples version of Node running at any one time.

In this section, you will learn how to install NVM and use it to install Node.js to your Ubuntu system using it.

1. We must first update our systems package list as there are some packages we must install before proceeding.

Updating the package list is as easy as using the following command.

sudo apt update

2. With the package list updated, we can now install the curl software using the command below.

Curl allows us to make web requests, which in this case will be used to grab and run the NVM install script.

sudo apt install curl

3. Now run the following command to install NVM to your Ubuntu system.

This command will grab the install script and pipe it straight to the shell system.

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

4. After NVM is installed, you will need to close and re-open your terminal session before using it.

5. Now that we have NVM installed, we can use it to install Node.js to Ubuntu. This manager allows you to install multiple versions.

You can run the following command if you want to install the current release.

nvm install node

Alternatively, you can install the current LTS release of Node.js on Ubuntu by using the “--lts” option.

nvm install --lts

Finally, you can specify the exact version you want to install. For example, you can use “17” in place of “[VERSION]“.

nvm install [VERSION]

6. When you have multiple versions of Node.js installed, you can switch between them by using “nvm use” followed by the version number.

nvm use [VERSION]

For example, if we wanted to switch from our currently selected version to Node.js 17, we would use the following.

nvm use 17

7. You can list the currently installed versions by using the following command.

nvm ls

8. It is also possible to get NVM to list all of the remote versions of Node.js that you can install on your Ubuntu system.

nvm ls-remote

Conclusion

Throughout this tutorial, we have shown you a couple of ways to install Node.js on Ubuntu-based systems.

As mentioned in the guide, Node.js is a powerful JavaScript runtime that allows you to write scalable and efficient web backends.

Please comment below if you have issues getting this software installed on Ubuntu.

We also have a wealth of other Ubuntu tutorials that you can use to get your system running how you would like it.

Alternatively, we have a wealth of other general Linux tutorials that will help you get familiar with Linux-based systems.

Leave a Reply

Your email address will not be published.