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.
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:
- Connect to the server using ssh by entering the following:
ssh username@server_ip_address
- Refresh the Apt cache to update the repository:
sudo apt update
- Enter the system’s password and press Enter.
- After the update is complete, install Node.js by entering the following command:
sudo apt-get install nodejs
- Type Y and press Enter to start the installation process.
- Once finished, install the NPM Node.js package manager by running the following:
sudo apt install npm
- Enter your password and confirm the installation.
- 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
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:
- Enter the following commands hitting Enter after each one:
sudo apt-get update
sudo apt-get upgrade
- Skip this step if you have cURL installed already. Otherwise, enter the following command:
sudo apt-get install curl
- 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 -
- Once you have the NodeSource repository, install Node.js:
sudo apt-get install nodejs
Aside from Node.js, this command will install NPM alongside other dependent packages.
- Use these two commands to query Node.js and NPM versions and confirm installation process was a success:
node -v
npm -v
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:
- Open the console and use the following command:
sudo apt-get install wget
- 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
- Allow the NVM script to run from your user’s bash profile:
source ~/.profile
After NVM is installed, you can start installing Node.js on your system:
- Check all the available versions of Node.js using NVM by running the following command:
nvm ls-remote
Pro Tip
Use the Ctrl + C keyboard shortcut to stop the listing process early.
- 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
- Check whether it has been successfully installed by querying the currently active version number:
node -v
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.
Comments
May 05 2019
it helps me clarify a lot thanks.
June 28 2019
merci beaucoup pour ce tutoriel sa ma beaucoup aider
March 10 2020
So helpful! Thanks!
April 13 2021
we have starter startup cloud hosting of hostinger. can we install node.js ?
May 19 2021
Hiya harshk, Node.js is not supported on Shared and Cloud hosting packages. If you want to use Node.js, you can install this useful feature on our VPS plans. You can check them at hostinger.com/vps-hosting
August 31 2021
I have installed node js in vps, but when executed the command node --version or npm --version, I am getting error as "cannot execute binary file"
September 20 2021
Hi Alana, it looks like there might be some misconfiguration inside of your VPS. I would suggest to check with our Customer Success team if they can find anything off with your server.