Log In

How to Install Node.js on Ubuntu 22.04

How to Install Node.js on Ubuntu 22.04
23.04.2024
Reading time: 4 min
Hostman Team
Technical writer

The Node.js framework is convenient because it allows you to implement JavaScript code outside the browser. You can develop the front-end and back-end using JavaScript exclusively. Today, many companies, including Uber, Netflix, LinkedIn, and others, choose Node.js to create web applications and websites. 

To begin working with Node.js, you need to install it on your server or computer. In this guide, you will find three methods of installing Node.js on Ubuntu 22.04. 

Prerequisites

To follow the steps from this tutorial, you will need:

  • A computer or a cloud server running Ubuntu 22.04.

  • A user with sudo privileges.

Method 1: Ubuntu repository

This method is the fastest, however, it does not allow you to select a specific version. From the Ubuntu repository, you can download only one Node.js version, 12.22.9. The important distinction here is that this is not the latest version released by Node, but the latest version of Ubuntu's Node.js. It will be upgraded only with the next OS update. To install the latest version of Node.js for Ubuntu, consider the methods 2 and 3.

If installing the 12.22.9 Node.js version works for you, follow these steps.

Update the package list on your system: 

sudo apt update

Install Node.js:

sudo apt install nodejs

Now, run the command below, and the package version should be displayed on the screen, indicating successful installation:

node -v

Image3

Each Node instance has its own version of npm (node package manager). This is a standard package manager that is part of the Node.js ecosystem. It is used to install additional modules from third-party sources.

Install the npm package manager:

sudo apt install npm

Check its installation:

npm -v

If you decide to remove the installed Node.js package, run the command:

sudo apt remove nodejs

Method 2: NodeSource repository

Another way to install Node.js on Ubuntu is to download the LTS version or new packages from NodeSource.

For this you will need the curl utility. It’s installed in most Linux distributions by default. The easiest way to check if it’s already on your system is to run:

curl

If the output shows curl: try 'curl --help' or 'curl --manual' for more information, you already have it. 

Image1

Otherwise, the system will print curl command not found. In this case, install it:

sudo apt install curl

Then download the repository to your system and install Node.js, using:

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - &&\
sudo apt-get install -y nodejs

Replace 21.x with 20.x or 18.x if you need a different package.

Check the versions to see everything was installed correctly:

node -v && npm -v

Image6

Method 3: Node Version Manager

The Node Version Manager, nvm, helps you work in multitasking mode. Using this tool, you can install several versions of Node.js on Ubuntu and easily switch between them. 

First, check if curl is installed by running:

curl

If the output shows curl: try 'curl --help' or 'curl --manual' for more information, you already have it. 

Image1

Otherwise, the system will print curl command not found. In this case, install it:

sudo apt install curl

Then download the script:

curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash

Reload your shell configuration:

source ~/.bashrc

Get a list of Node versions ready to download:

nvm list-remote

Image5

The versions marked LTS have long-term support and already been tested. More recent versions have the latest functionality but can contain errors.

Now, you can install the selected version:

nvm install [version.number]

For example:

nvm install v20.12.2

View the list of the installed versions:

nvm list

The symbol shows the version currently used. 

Image2

To switch from one version to another, use the command:

nvm use [version.number]

Image4

Conclusion

In this simple guide, we described how to install Node.js on Ubuntu 22.04 using three different methods.

The first one is the fastest; however, it mostly works for testing purposes or practicing your Node.js skills. It's not very suitable for production purposes as it only allows you to install one Node.js version, and not the latest one at that.

We recommend using the installation from NodeSource to deploy a finished project on Node.js and test new versions. Advanced users will probably choose the npm method as it allows you to manage your Node.js versions more flexibly.


Share