Oct 24, 2022
Edward S. & Aris B.
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 virtual private servers (VPS).
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 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.
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.
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:
- 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:
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:
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:
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:
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:
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.