In this tutorial, i am going to show you 3 methods to how to install and use node js on linux ubuntu 22.04 system.
How to install the Node.JS on the Ubuntu 22.04
Use the below given 3 methods to install and use node js on linux ubuntu 22.04 system:
- Method 1: Installation of Node.js using the default repository of Ubuntu 22.04
- Method 2: Installation of the Node.js using the PPA repository
- Method 3: Installation of the Node.js using the NVM
- How to use the Node.js on Ubuntu 22.04
Method 1: Installation of Node.js using the default repository of Ubuntu 22.04
Start terminal and run the following command on terminal to install node js on ubuntu 22.04 using default repository:
$ sudo apt install nodejs -y
Once the installation is done; run the following command on terminal to check node js version:
$ nodejs --version
If you found any error while installation of node js on ubuntu, can be resolve by fixing the broken packages:
$ sudo apt --fix-broken install
Method 2: Installation of the Node.js using the PPA repository
Run the following command on terminal to install node js using the ppa repository; is as follow:
$ curl -fsSL https://deb.nodesource.com/setup_17.x | sudo -E bash - sudo apt-get install -y nodejs
After adding the PPA repository of the Node.js, you will install it using the apt package manager:
$ sudo apt install nodejs
Again will confirm the installation of the Node.js by displaying its version:
$ nodejs --version
Method 3: Installation of the Node.js using the NVM
Run the following command on terminal to install the latest version or any particular version of the Node.js:
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
Now you will run the following commands:
$ export NVM_DIR="$HOME/.nvm"
$ [ -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, you will check the version of the installed NVM:
$ nvm --version
Display the list of all versions of the Node.js which are available on NVM:
$ nvm list-remote
You can either install any of the Node.js versions available in the above list or can install the latest version using the command:
$ nvm install node
You will validate the installation by displaying the installed version of Node.js:
$ node --version
How to use the Node.js on Ubuntu 22.04
Run the following command on terminal to create a text file using the nano text editor:
$ nano MyJScode.js
Now you will type the code for the simple addition of the two numbers by using the Javascript:
function add(a,b) { return a+b } console.log(add(4, 6))
In the above code, you 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:
$ node MyJScode.js
Conclusion
In this tutorial, i have shown to you how to 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.