How to Install Node.js on Ubuntu 22.04
The Node.js framework is convenient because it allows you to implement JavaScript code outside the browser. You can develop the front-end and back-end using JavaScript exclusively. Today, many companies, including Uber, Netflix, LinkedIn, and others, choose Node.js to create web applications and websites.
To begin working with Node.js, you need to install it on your server or computer. In this guide, you will find three methods of installing Node.js on Ubuntu 22.04.
Prerequisites Copy link
To follow the steps from this tutorial, you will need:
-
A computer or a cloud server running Ubuntu 22.04.
-
A user with
sudoprivileges.
Method 1: Ubuntu repository Copy link
This method is the fastest, however, it does not allow you to select a specific version. From the Ubuntu repository, you can download only one Node.js version, 12.22.9. The important distinction here is that this is not the latest version released by Node, but the latest version of Ubuntu's Node.js. It will be upgraded only with the next OS update. To install the latest version of Node.js for Ubuntu, consider the methods 2 and 3.
If installing the 12.22.9 Node.js version works for you, follow these steps.
Update the package list on your system:
sudo apt updateInstall Node.js:
sudo apt install nodejsNow, run the command below, and the package version should be displayed on the screen, indicating successful installation:
node -vEach Node instance has its own version of npm (node package manager). This is a standard package manager that is part of the Node.js ecosystem. It is used to install additional modules from third-party sources.
Install the npm package manager:
sudo apt install npmCheck its installation:
npm -vIf you decide to remove the installed Node.js package, run the command:
sudo apt remove nodejsMethod 2: NodeSource repository Copy link
Another way to install Node.js on Ubuntu is to download the LTS version or new packages from NodeSource.
For this you will need the curl utility. It’s installed in most Linux distributions by default. The easiest way to check if it’s already on your system is to run:
curlIf the output shows curl: try 'curl --help' or 'curl --manual' for more information, you already have it.
Otherwise, the system will print curl command not found. In this case, install it:
sudo apt install curlThen download the repository to your system and install Node.js, using:
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - &&\
sudo apt-get install -y nodejsReplace 21.x with 20.x or 18.x if you need a different package.
Check the versions to see everything was installed correctly:
node -v && npm -vMethod 3: Node Version Manager Copy link
The Node Version Manager, nvm, helps you work in multitasking mode. Using this tool, you can install several versions of Node.js on Ubuntu and easily switch between them.
First, check if curl is installed by running:
curlIf the output shows curl: try 'curl --help' or 'curl --manual' for more information, you already have it.
Otherwise, the system will print curl command not found. In this case, install it:
sudo apt install curlThen download the script:
curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bashReload your shell configuration:
source ~/.bashrcGet a list of Node versions ready to download:
nvm list-remoteThe versions marked LTS have long-term support and already been tested. More recent versions have the latest functionality but can contain errors.
Now, you can install the selected version:
nvm install [version.number]For example:
nvm install v20.12.2View the list of the installed versions:
nvm listThe → symbol shows the version currently used.
To switch from one version to another, use the command:
nvm use [version.number]Conclusion Copy link
In this simple guide, we described how to install Node.js on Ubuntu 22.04 using three different methods.
The first one is the fastest; however, it mostly works for testing purposes or practicing your Node.js skills. It's not very suitable for production purposes as it only allows you to install one Node.js version, and not the latest one at that.
We recommend using the installation from NodeSource to deploy a finished project on Node.js and test new versions. Advanced users will probably choose the npm method as it allows you to manage your Node.js versions more flexibly.
By the way, with Hostman, you can run your workloads on an efficient Amsterdam VPS that support low latency for EU-based users. Check this out, we have plenty of budget VPS hosting options for your projects.