Install and Use Node.js on Ubuntu 22.04

Node.js is a real-time platform for executing JavaScript code on servers. It provides many modules that support web applications. Using Node.js, developers can design scalable backend functions and utilize them for the development of standard web applications and back-end API services. Asynchronous I/O primitives are contained by node.js, which prevents JavaScript code from being blocked.

This write-up will discuss three different methods to install Node.js on Ubuntu 22.04:

  • using the default Ubuntu 22.04 repository
  • by adding PPA repository
  • using the NVM repository

So, let’s start!

Installation of Node.js using the default Ubuntu 22.04 repository

We will now utilize the default Ubuntu repository for the installation of Node.js on our Ubuntu 22.04 system.

Step 1: Update Ubuntu 22.04 repository

Firstly, open up your Ubuntu 22.04 terminal by pressing “CTRL+ALT+T” and then update the repository:

$ sudo apt update

Step 2: Installing Node.js

In the next step, we will execute the below-given command for Node.js installation on our Ubuntu 22.04 system:

$ sudo apt install nodejs

It will ask for confirmation to continue installation after running the install command. Enter “y” to permit the process to continue:

Step 3: Verify the version of Node.js

In the third step, we need to verify the installation of Node.js by executing the following command:

$ node -v

Installation of Nodejs using PPA repository

You can also utilize the PPA (personal package archive) repository to install a specific version of Node.js according to user preference. To do so, follow the below-given step-by-step procedure.

Step 1: Get installation script to install Node.js

Firstly, use the curl command to get the script to install the preferred version. For now, we prefer to install the Node.js “16.x”:

$ curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -

You can replace “16.x” with your selected Node.js version.

Step 2: Update repository

In the second step, we will update the system repository:

$ sudo apt update

Step 3: Installation and verification of Node.js version

Next, utilize the following command to install the desired version of Node.js on Ubuntu 22.04:

$ sudo apt install nodejs

After successful installation of Node.js, verify the installed version with the help of the following command:

$ node -v

Installation of Nodejs using NVM repository

NVM is another way to install node.js. It allows us to install different versions of Node.js at one time, and the user can also switch between them at runtime. NVM displays all available versions in the form of a list, and the user can choose the latest version of Node.js to install on the machine.

Following are the steps to install Node.js using the nvm repository.

Step 1: Install curl and get nvm script

Firstly, to install curl, you can use the “apt install curl” command. Make sure curl should be installed on the machine to get the script of the nvm installer:

$ sudo apt install curl

Then, use below mentioned command to run the nvm installer:

$ curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash

Step 2: Source .bashrc file and get list of versions

After installing the nvm script on the system, source the .bashrc file and check the version list using the below-mentioned commands:

$ source ~/.bashrc
$ nvm list-remote

Step 3: Install and verify the preferred version of Node.js

Choose your desired version and install it on your machine. For instance, we will install Node.js “v17.1.0” using the following command:

$ nvm install v17.1.0

Then, verify the version of the installed Node.js:

Now, let’s check out the method to use Node.js on Ubuntu 22.04.

How to use the Node.js on Ubuntu 22.04

Web development frequently uses JavaScript computer language because it is easy to learn and understand. Developers are increasingly using Node.js to create frontend applications, REST APIs, and CRUDs.

As we have learned about Node.js installation in the previous section, we will now demonstrate to you the method to use Node.js for executing a simple JavaScript file. For this purpose, firstly, we will create a file using the “nano” editor:

$ nano MyJScode

Then, add the below-given code in the opened “MyJScode” text file and press “CTRL+O” to save the added code:

function addition(x,y){
return x+y
}
console.log(addition(3,6))

The code added in the created “MyJScode” file will perform the addition operation on the specified numbers. Now, to check out its output, execute the following command:

$ node MyJScode

We have compiled the essential information related to the installation and usage of Node.js on Ubuntu 22.04.

Uninstallation of Node.js on Ubuntu 22.04

To uninstall Node.js from Ubuntu 22.04, you have to execute the following command:

The Node.js is removed from Ubuntu 22.04.

Conclusion

To install Node.js on Ubuntu 22.04, you can use three methods, either execute the “$ sudo apt install nodejs” command for the installation through the Ubuntu repository or utilize the “curl” command to install Node.js using “NVM” or “PPA” repository. Then, use Node.js to execute a JavaScript file with the “$ node filename” command. This article discussed the method to install and use Node.js on Ubuntu 22.04.

Over 500 Million Views on Our Blogs and YouTube! We’re inviting 10 businesses to partner with us and grow together. Click here to join us! ×