Install Node Js on Ubuntu 22.04 can be done in three different ways:
- Installing Node.js with Apt from the Default Repositories
- Installing Node.js with Apt Using a NodeSource PPA
- Installing Node Using the Node Version Manager
The following sections provide a detailed explanation of all of these methods.
Installing Node.js with Apt from the Default Repositories
As part of Ubuntu’s default repositories, Node.js is included to provide a consistent experience across multiple systems.
Version 12.22.5 is in the repositories at the time of writing. I understand that this version will not be the latest, but it should allow you to experiment with the language quickly and it will be stable.
Warning: Ubuntu 22.04 comes with a version of Node.js, version 12.22.9, which is an LTS release, or a long-term support version. Technically, it is an outdated version, but it will be supported until Ubuntu 24.04 is released.
The apt package manager can be used to get this version. To get this version you will have to relaunch your local package index.
sudo apt update
Then install Node.js
sudo apt install nodejs
Confirm the installation by pressing Y. If any services need to be restarted, press ENTER to accept the defaults and continue. Query node for its version number to see if the install was successful
node -v
To get started with Node.js, you only need to install the package available in the repository. Installing Node.js requires you to also install npm, the package manager. This can be accomplished with apt:
sudo apt install npm
Using this method, you will be able to install modules and packages to use with Node.js.
Using apt and the default Ubuntu software repositories, you have successfully installed Node.js and npm. Using an alternate repository for installing different versions of Node.js is shown in the following section.
Install Node JS on Ubuntu 22 with Apt Using a NodeSource PPA
NodeSource maintains a PPA (personal package archive) that you can use to install a different version of Node.js. NodeSource PPAs offer more versions of Node.js than Ubuntu’s official repository. As of the time of writing, Node.js versions 14, 16, and 17 are available.
To access its packages, we will first need to install the PPA. If your preferred version string is different from 17. x, use curl to retrieve the installation script from your home directory.
curl -fsSL https://deb.nodesource.com/setup_17.x | sudo -E bash -
sudo apt-get install -y nodejs
With nano (or your preferred text editor), you can see the contents of the downloaded script:
nano nodesource_setup.sh
There are some cases when it is not considered a best practice to run shell scripts provided by second-party companies without guidance, but in this case, NodeSource implements its logic to ensure that the correct package manager commands are passed to you based on your distro’s requirements.
The script will run if it is safe to do so. If you are satisfied that it is safe to run, exit your editor, and then run the script with sudo:
sudo bash nodesource_setup.sh
You will be prompted to add the PPA to your configuration and your local package cache will be automatically updated.
In the same manner, as you installed Node.js earlier, you can now install the Node.js package. To install the new version of Node.js, it is possibly a good idea to fully remove all older Node.js packages, by using sudo apt to remove node js npm before installing the new version.
This will not affect your configurations, only the versions you have installed. PPAs from third parties don’t always package their software in a way that’s a direct upgrade over stock packages. If you have trouble, you can always go back to the original package if you need to.
sudo apt install nodejs
Run node with the -v version flag to verify that you have installed the new version.
node -v
Output v17.6.0
npm is included in the NodeSource node.js package, so you don’t need to install it separately.
Using apt and the NodeSource PPA, you have successfully installed Node.js and npm. The next section shows you how to install and manage multiple versions of Node.js via the Node Version Manager.
Installing Node.js Using the Node Version Manager
Node.js can also be installed using nvm, the Node Version Manager, which provides great flexibility. In conjunction with Node.js, the module manages the installation and maintenance of a multitude of different independent versions, as well as their associated Node packages at the same time.
On the GitHub page of the project, you can find instructions on how to install NVM on your Ubuntu 22.04 machine. Take a look at the README file located on the main page and copy the curl command. You will then be able to get the latest version of the installation script.
To ensure that you are not doing anything you don’t agree with within the script before you pipe it to bash, it is always a good idea to audit it before you use it. Using curl, remove the bash segment at the end
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
You should review the changes it is making to determine if they are suitable for you. Repeat the command with the addition of bash to the end when you are satisfied. You can download and execute the script using the following URL, no matter which version of nvm you are using:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
Your user account will be installed with the NVM script. It must be sourced from your .bashrc file to be used.
source ~/.bashrc
With NVM, you can now find out which versions of Node are available:
nvm list-remote
Output
There are lots of items on the list!
The first line indicates the current version (-> v16.14.0), followed by some named aliases and the versions those aliases refer to.
Note: There may also be a system entry here if you have Node.js installed via apt. The node can be activated by running the nvm use system on the system.
Based on these aliases, we can install a release as well. You can install fermium by running the following command:
nvm install node
Output
The same method from the other sections can be used to verify that the installation was successful:
node -v
Output
Our machine is equipped with the correct version of Node. We have also installed npm.
Read More: Migrate WordPress to AWS Lightsail Instance Quickly
Conclusion
Node.js is quite easy to install and run on an Ubuntu 22.04 server. Which method you choose will depend on your specific circumstances. The use of Ubuntu’s packaged version is the easiest method, though nvm and NodeSource PPAs provide additional flexibility.