Choose a different version or distribution
Introduction
Developed from the ground up for use outside of web browsers, Node.js is a cross-platform, open-source JavaScript runtime environment based on Chrome's JavaScript. It is most often used in the creation of networking and server-side applications because of its speed and scalability. The Node.js package manager, popularly known as "npm," is the biggest software registry in the world.
In this tutorial, we will explore three distinct approaches to setting up Ubuntu 22.04 with Node.js and npm:
- As available in the official Ubuntu repository. This is the recommended method for installing Node.js with npm on Ubuntu since it is the simplest and should work in most situations. The Ubuntu repositories provide
v12.22.9
. - Originating in the NodeSource code archive. Rather than using the version of Node.js that comes preinstalled with Ubuntu, you may use this repository to install an alternative one. NodeSource currently supports Node.js versions
18.x, 17.x, 16.x, and 14.x
. - Putting
nvm
to use as an example (Node Version Manager). This utility facilitates the installation of different versions of Node.js on a single computer. This is the recommended method of installing Node.js if you are a developer working with Node.js.
Pick an installation approach that works best in your setting. It is recommended that you review the application's documentation to determine which version of Node.js should be installed.
Installing Node.js and npm from the Ubuntu repository
As of this writing, the version of Node.js included in the official Ubuntu 22.04 repositories is v12.22.9
, which is incompatible with the latest TLS standards.
Easy installation is a major selling point. The package index may be updated and Node.js and npm installed by running the following commands:
sudo apt update
sudo apt install nodejs npm
When you run the line above, it will install a variety of packages, including the prerequisites for compiling and installing native addons via npm.
Once finished, launch: to check the installation.
nodejs -v
Output
v12.22.9
Installing Node.js and npm from NodeSource
The mission of NodeSource is to provide enterprise-level Node support. It keeps various versions of Node.js in an APT repository. If your project needs a certain version of Node.js, you may find it in this repository.
The current versions available from the NodeSource repository are as follows:
- v18.x - The latest stable version.
- v17.x
- v16.x - The latest LTS version.
- v14.x
Version 18.x of Node.js will be installed.
To download and run the NodeSource installation script, use the following command as a user with Sudo privileges:
curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash -
The script will generate an apt repository file, install all required packages, update the apt-repository, as well as apply the NodeSource signing key to your system.
Substitute setup_16.x
for setup_18.x
if you need to use a different version of Node.js, such as 16.x
.
Install Node.js and npm when the NodeSource repository has been enabled:
sudo apt install nodejs
The node
and npm
binaries are included in the nodejs distribution.
Print the current versions of Node.js and npm to make sure they were installed correctly.
node -v
Output
v18.2.0
npm -v
Output
8.9.0
You must set up the following development tools before you may generate native addons from npm:
sudo apt install build-essential
Installing Node.js and npm using NVM
To handle numerous Node.js versions on a per-user basis, NVM (Node Version Manager) is a bash script. You may use NVM to quickly and easily install and remove Node.js versions for development and testing purposes.
The nvm script may be downloaded and installed by going to the repository's GitHub page and copying the appropriate curl
or wget
command.
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
It's not recommended to use sudo
since it enables nvm
for the root user.
This script will clone the project's Github repository into the ~/.nvm
directory:
Output
=> Close and reopen your terminal to start using nvm or run the following to use it now:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
To add the path to the nvm
script to the current shell session, as suggested by the output above, either shut and reopen the terminal or execute the instructions provided. Whatever works best for you is the right choice.
You may check whether nvm
was installed correctly by typing once the script is on your PATH
:
nvm -v
Output
0.39.1
The following command will provide a list of all supported Node.js versions for installation using nvm
:
nvm list-remote
The program outputs a comprehensive index of all Node.js releases.
Output
...
v14.19.2 (LTS: Fermium)
v14.19.3 (Latest LTS: Fermium)
...
v16.14.2 (LTS: Gallium)
v16.15.0 (Latest LTS: Gallium)
v17.0.0
v17.0.1
...
v18.1.0
v18.2.0
Using the command line, you may install the most recent version of Node.js:
nvm install node
This is how the results should look:
Output
...
Now using node v18.2.0 (npm v8.9.0)
Creating default alias: default -> node (-> v18.2.0)
The installation is successful if the Node.js version is shown on screen once it has been installed:
node -v
Output
v18.2.0
Let's put in the newest LTS version (16.15.0) and the previous version (14.19.3)
:
nvm install --lts
nvm install 14.19.3
To see a list of the available Node.js releases, type:
nvm ls
The results ought to like the following:
Output
-> v14.19.3
v16.15.0
v18.2.0
default -> node (-> v18.2.0)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v18.2.0) (default)
stable -> 18.2 (-> v18.2.0) (default)
lts/* -> lts/gallium (-> v16.15.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.12 (-> N/A)
lts/fermium -> v14.19.3
lts/gallium -> v16.15.0
The version of Node.js being used by the currently active shell session is -> 14.19.3
, shown by the arrow on the right; the default is v18.2.0
. When a new shell is opened, it will use the default version.
The following command is what you should use to alter the current version:
nvm use 16.15.0
Output
Now using node v16.15.0 (npm v8.5.5)
To alter them installed the version of Node.js:
nvm alias default 16.15.0
For further details on how to use the nvm
script, please see the corresponding GitHub page.
Conclusion
We've covered three different approaches to getting Node.js and npm set up on Ubuntu 22.04. Which strategy you choose is ultimately determined by your own needs and preferences. The nvm approach provides more per-user control over which Node.js versions are installed and which are removed than does installing the bundled version from the Ubuntu or NodeSource repository.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.