In this guide, we want to teach you How To Install Node.js on Ubuntu 22.04.
Node.js is open-source and completely free, used by thousands of developers around the world. It brings plenty of advantages to the table, making it a better choice than other server-side platforms like Java or PHP.
Steps To Install Node.js on Ubuntu 22.04
In this tutorial, we will show you to install Node.js on Ubuntu 22.04, in three different ways.
- Install Nolde.js using the APT repository
- Using the PPA repository
- Using the nvm (Node Version Manager)
Before you start to complete this guide, you need to log in to your server as a non-root user with sudo privileges. To do this, you can follow our article the Initial Server Setup with Ubuntu 22.04.
Now follow the steps below to install Node.js on your server.
Install Nodejs With APT repository on Ubuntu 22.04
First, update your local package index with the following command:
sudo apt update
Then, install Node.js on Ubuntu 22.04 with the command below:
sudo apt install nodejs
Now you can verify your Node.js installation by checking its version:
Also, you can install the npm (Node.js Package Manager) with the following command:
sudo apt install npm
Install Nodejs with the PPA repository on Ubuntu 22.04
To install a different version of Node.js, you can use a PPA (personal package archive) maintained by NodeSource. These PPAs have more versions of Node.js available than the official Ubuntu repositories. Node.js v14, v16, and v17 are available as of the time of writing.
To download the installation script for your preferred version, you can use the curl command:
# cd ~ # curl -sL https://deb.nodesource.com/setup_17.x -o nodesource_setup.sh
When your download is completed, run your installation script:
sudo bash nodesource_setup.sh
The PPA will be added to your configuration and your local package cache will be updated automatically. You can now install the Node.js package in the same way you did in the previous section.
sudo apt install nodejs
Verify your Node.js installation on Ubuntu 22.04 with the command below:
Note: The NodeSource
nodejs package contains both the
node binary and
npm, so you don’t need to install
Install Nodejs using the NVM (Node Version Manager) on Ubuntu 22
Another way of installing Node.js that is particularly flexible is to use nvm, the Node Version Manager. This piece of software allows you to install and maintain many different independent versions of Node.js, and their associated Node packages, at the same time.
First, you need to visit the GitHub Project page and copy the
curl command from the README file that displays on the main page.
Then, run the command without the | bash segment at the end of the command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh
Take a look and make sure you are comfortable with the changes it is making. When you are satisfied, run the command again with
| bash appended at the end.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
Next, source your .bashrc file:
To check which versions of Node are available, run the following command:
Output . . . v16.11.1 v16.12.0 v16.13.0 (LTS: Gallium) v16.13.1 (LTS: Gallium) v16.13.2 (LTS: Gallium) v16.14.0 (Latest LTS: Gallium) v17.0.0 v17.0.1 v17.1.0 v17.2.0 v17.3.0 v17.3.1 v17.4.0 v17.5.0 v17.6.0
To get the Node.js
v16.14.0 (Latest LTS: Gallium) you can run the command below on Ubuntu 22.04:
nvm install v16.14.0
Now you can see the different versions you have installed with the following command:
Output -> v16.14.0 default -> v16.14.0 iojs -> N/A (default) unstable -> N/A (default) node -> stable (-> v16.14.0) (default) stable -> 16.14 (-> v16.14.0) (default) lts/* -> lts/gallium (-> v16.14.0) lts/argon -> v4.9.1 (-> N/A) lts/boron -> v6.17.1 (-> N/A) lts/carbon -> v8.17.0 (-> N/A) lts/dubnium -> v10.24.1 (-> N/A) lts/erbium -> v12.22.10 (-> N/A) lts/fermium -> v14.19.0 (-> N/A) lts/gallium -> v16.14.0
Note: if you also have a version of Node.js installed through
apt, you may see an
system entry here. You can always activate the system-installed version of Node using
nvm use system.
At this point, you learn to Install Node.js on Ubuntu 22.04.
Hope you enjoy it.
You may also be interested in this article about How To Install Node.js on Centos 7.