How to Install Node.js and NPM on Ubuntu 18.04, 20.04, and 22.04: 3 Methods

How to Install Node.js and NPM on Ubuntu 18.04, 20.04, and 22.04: 3 Methods

Node.js is one of the most popular web technologies available today. As a developer, you may want to use it to increase your web application functionality or create a local development environment.

In this article, we will explain how to install Node.js on a Linux system running Ubuntu 18.04, 20.04, and 22.04. This installation method works with local Ubuntu-based systems and VPS services.

We will show you how to install Node.js on Ubuntu using the default repositories and NodeSource. Lastly, you will learn how to install a specific Node.js version using NVM.

What Is Node.js

Node.js is a server-side runtime environment that allows users to execute JavaScript code on the server-side. It is an open-source cross-platform, ideal for education and business-oriented project types.

Node.js is particularly helpful for handling data-intensive tasks and developing scalable network applications. Popular use cases include real-time chatting, data streaming, and server-side proxies.

Although the latest version of Node.js is 18.9.0, we recommend using 16.17.0 as it is the long-term supported (LTS) release. Regardless, always check the Node.js download page for the latest version since it is updated rather frequently.

Important! Before we proceed, ensure you have access to the server and non-root user account to execute the Linux sudo commands. Read our guide to learn more about how to access VPS via SSH.

How to Install Node.js Using Apt and NPM From the Default Repositories

An efficient way to install Node.js on Ubuntu is to use the official distribution repository. A system running Ubuntu 18.04 or later includes Node.js and Node Package Manager (NPM) packages in the default repository.

However, this method does not let you choose which Node.js version to install. In other words, it may install an outdated version already out of support.

Pro Tip

If you don’t want to install Node.js manually, go for one of our VPS operating system templates with Node.js already pre-installed. To learn more about this option, visit Hostinger’s Node.js server hosting page.

Before installing Node.js, open the terminal from your system’s main menu or press the Ctrl + Alt + T shortcut. Follow these steps to install Node.js on your VPS using the Apt package manager:

  1. Connect to the server using ssh by entering the following:
ssh username@server_ip_address
  1. Refresh the Apt cache to update the repository:
sudo apt update
Updating apt package manager using the Terminal
  1. Enter the system’s password and press Enter.
  2. After the update is complete, install Node.js by entering the following command:
sudo apt-get install nodejs
Installing Node.js from the default repository
  1. Type Y and press Enter to start the installation process.
  2. Once finished, install the NPM Node.js package manager by running the following:
sudo apt install npm
Installing NPM from the default repository
  1. Enter your password and confirm the installation.
  2. Finally, check the Node.js and NPM version numbers to confirm it has been successfully installed. The command-line will return the installed version number after you enter these commands:
nodejs -v
npm -v
Checking installed Node.js and NPM version from Terminal

Suppose you want to remove Node.js or NPM from the Linux system. If this is the case, use the following commands:

sudo apt remove nodejs
sudo apt remove npm

How to Install Node.js With Apt Using NodeSource

Another way to install Node.js is to use a Personal Package Archive (PPA) repository like NodeSource. Compared to the default Ubuntu repository, NodeSource offers more versions.

This method is ideal if you want to install a specific or the latest Node.js release. Here’s how to install Node.js on Ubuntu using the NodeSource repository:

  1. Enter the following commands hitting Enter after each one:
sudo apt-get update
sudo apt-get upgrade
  1. Skip this step if you have cURL installed already. Otherwise, enter the following command:
sudo apt-get install curl
  1. To install the current release, execute the following command. Remember to replace the 18.x value with your preferred Node.js version:
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
Downloading NodeSource with 18.x version of Node.js
  1. Once you have the NodeSource repository, install Node.js:
sudo apt-get install nodejs
Installing Node.js from NodeSource

Aside from Node.js, this command will install NPM alongside other dependent packages.

  1. Use these two commands to query Node.js and NPM versions and confirm installation process was a success:
node -v
npm -v
Checking freshly installed Node.js and NPM versions

How to Install a Specific Version of Node.js Using NVM

Another way to install Node.js on Ubuntu is to make use of the Node Version Manager (NVM), a bash script used to manage multiple versions of Node.js.

Installing Node.js using the NVM method is ideal if you want to use a specific version. To begin, download and install NVM using wget:

  1. Open the console and use the following command:
sudo apt-get install wget
A terminal window showing the wget installation process
  1. To install a different version, replace v0.39.0 with another value or use the following command to download the latest release:
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.39.0/install.sh | bash
Downloading and installing NVM using wget
  1. Allow the NVM script to run from your user’s bash profile:
source ~/.profile
Enabling NVM script for the current user bash profile

After NVM is installed, you can start installing Node.js on your system:

  1. Check all the available versions of Node.js using NVM by running the following command:
nvm ls-remote
Listing available node.js versions using NVM

Pro Tip

Use the Ctrl + C keyboard shortcut to stop the listing process early.

  1. While you may install any Node.js release, we recommend using the latest version to ensure its support and reliability. For this example, we will install the latest LTS version at the time of writing:
nvm install 16.17.0
Installing a particular version of Node.js using NVM
  1. Check whether it has been successfully installed by querying the currently active version number:
node -v
Checking the currently active Node.js version

Conclusion

Node.js is a server-side JavaScript runtime environment that lets you build network applications quickly. Web developers frequently use it to handle data-intensive tasks and real-time analytics.

In this article, we reviewed three ways how to install Node.js on Ubuntu 22.04 or older versions. Before installing Node.js, ensure you can use the sudo command and connect to your virtual private server.

Install the packaged version of Node.js from Ubuntu’s repository using Apt. Alternatively, use NodeSource or NVM to install a particular version or the latest LTS release.

We hope this article helped you understand how to install Node.js on your VPS or local Linux system. If you have any questions, don’t hesitate to leave a comment below.

Author
The author

Edward S.

Edward is a content editor with years of experience in IT writing, marketing, and Linux system administration. His goal is to encourage readers to establish an impactful online presence. He also really loves dogs, guitars, and everything related to space.

Author
The Co-author

Aris Sentika

Aris is a Content Writer specializing in Linux and WordPress development. He has a passion for networking, front-end web development, and server administration. By combining his IT and writing experience, Aris creates content that helps people easily understand complex technical topics to start their online journey. Follow him on LinkedIn.