Node.js is a runtime environment designed for the execution of the Javascript codes outside the browser and it also comes with many modules to build web applications. Node.js can be installed easily on Ubuntu 22.04 for testing or running different Javascript code.
In this write-up, we are going to explore the installation methods for the Node.js in Ubuntu 22.04 as well as discuss the basic usage of the Node.js on Ubuntu 22.04.
How to install the Node.JS on the Ubuntu 22.04
There are three majorly used methods to install the Node Js on Ubuntu 22.04 which are:
- Installing Node.js using the default repository of Ubuntu 22.04
- Installing Node.js using the PPA repository
- Installing Node.js using the NVM
All these methods are explained in detail in the following sections.
Method 1: Installation of Node.js using the default repository of Ubuntu 22.04
Like previous releases of the Ubuntu 22.04, the installation medium of the Node.js are included in the default repository of the Ubuntu 22.04 using the apt package manager:
To confirm the installation of the Node.js, we will check its version:
The above output shows that the Node.js has been successfully installed on Ubuntu 22.04. You might encounter a dependency error while installing the Node.js using the package from its default repository like:
This can be resolve by fixing the broken packages:
Method 2: Installation of the Node.js using the PPA repository
PPA repository includes the software repositories of different packages which are specially designed for the Ubuntu users. We will first add the PPA repository of the latest release of the Node.js:
sudo apt-get install -y nodejs
After adding the PPA repository of the Node.js, we will install it using the apt package manager:
Again will confirm the installation of the Node.js by displaying its version:
Method 3: Installation of the Node.js using the NVM
The NVM (Node Version Manager) is also used for installing the Node.js, the good thing about the NVM is that displays the list of all available versions of the Node.js, from which you can either install the latest version or any particular version of the Node.js. To download the NVM, use:
Now we will run the following commands:
$ [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
$ [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
When the above-mentioned commands are successfully executed, we will check the version of the installed NVM:
Display the list of all versions of the Node.js which are available on NVM:
You can either install any of the Node.js versions available in the above list or can install the latest version using the command:
We will validate the installation by displaying the installed version of Node.js:
How to use the Node.js on Ubuntu 22.04
Javascript is known as a programming language which has popular use in web development and it is easy to learn. We have learned in the above sections about the installation of Node.js whereas here we will simply learn the usage of a node in executing the Javascript file. We will first create a text file using the nano text editor:
Now we will type the code for the simple addition of the two numbers by using the Javascript:
return a+b
}
console.log(add(4, 6))
In the above code, we simply assign two values in variable a and b, and add them together to display the output. To run the output of the above code, we will use the command:
The output of the sum of both numbers has been displayed.
How to delete the Node.js from the Ubuntu 22.04
If there is no need for the Node.js, we can remove it from the Ubuntu 22.04 in order to free up the space for new packages by using the command:
Conclusion
Javascript is a well-known programming language used in the development of websites and for Javascript code, we have to use Node.js. In this write-up, we have installed the package of Node.js in three different ways and also learn the usage of Node.js on Ubuntu 22.04 by running a simple code of Javascript.