How I installed NodeJS 18 on my Ubuntu 22.04 machine

Endrit Qerreti

Endrit Qerreti

Node.js is a free and open source Javascript runtime environment, that you can use to build and run Javascript apps.

Node.js can be easily installed on Ubuntu 22.04 by using its official repository that allows you to install Node.js not only on Ubuntu 22.04, but to any Debian based system.

Node.js deb repository nodesource allows you to download any version of Node.js you want. However, in this tutorial, we'll be installing Node.js V18

Installing a specific version of Node.js is required in cases when you are trying to run a Node.js app that doesn't support the latest version of Node.js. So in order to be able to run that app, you must install the Node.js version that is required.

In this tutorial, you will learn how to install Node.js 18 on Ubuntu 22.04.

Install Node.js 18 on Ubuntu 22.04

Step 1 - Install required packages

Before downloading Node.js GPG key or adding its repository on your Ubuntu 22.04 machine, you need to download curl , gnupg and ca-certificates in order to be able to setup Node.js repository and verify its GPG key.

sudo apt install curl gnupg

Step 2 - Download Node.js GPG Key

Once you have installed all the required packages, proceed to download Node.js GPG key on your system.

To do so, simply run the command below.

curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg

Step 3 - Choose Node.js Version

Now you need to choose Node.js version that you want to install on your Ubuntu machine. In this tutorial, we are installing Node.js V18, so to choose this version simply run the command below.

NODE_MAJOR=18

Step 4 - Setup Node.js Repository

Now that you've chosen the version of Node.js that you will be installing, proceed to setup Node.js repository so that you can download Node.js on your system.

Setting up Node.js repository is quite simple, and it can be done easily via the command below.

echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list

Step 5 - Update your system

Node.js repsitory is now added on your Ubuntu 22.04 machine, but it's not yet active. To make it active, you need to update the package index on your system.

To update the package index, you can do so by issuing the following command.

sudo apt update

Step 6 - Install Node.js V18

Finally, proceed to install Node.js V18 on your Ubuntu 22.04 machine. To do so, simply run the command below.

sudo apt install nodejs

Step 7 - Verify Node.js Installation

Node.js should be now installed on your system. To verify that Node.js's installation process went fine, and that the correct version was installed, check the version that was installed by issuing the following.

node -v

As you can see, Node.js that we installed is Node.js v18.19.0

Uninstall Node.js 18 on Ubuntu 22.04

To uninstall Node.js from your Ubuntu 22.04 machine, you can do so by uninstalling the nodejs package that we installed via apt.

sudo apt remove nodejs -y

Conclusion

In this tutorial, you learned how to install Node.js V18 on Ubuntu 22.04, by using the official Node.js repository that allows you to install Node.js in any Debian based system.

If you need another version of Node.js installed on your Ubuntu 22.04 machine, then all you need to do is set the NODE_MAJOR parameter to receive the version you want. For example, let's say you want to install Node.js V20, then to do so you need to set NODE_MAJOR=20 before installing Node.js.