Install and use node js on ubuntu 22.04; Through this tutorial, we will learn 3 simple ways of how to install and use node js on linux ubuntu 22.04 system.
How to install the Node.JS on the Ubuntu 22.04
Follow the given 3 simple ways 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
Open command prompt and execute the following command on command prompt to install node js on ubuntu 22.04 using default repository:
$ sudo apt install nodejs -y
Once the installation is complete; will check node js version by using the following command:
$ nodejs --version
If we 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
Execute the following command on command prompt 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, we 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
Execute the following command on command prompt 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 we 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, we 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
we 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
We will validate the installation by displaying the installed version of Node.js:
$ node --version
How to use the Node.js on Ubuntu 22.04
Execute the following command on command prompt to create a text file using the nano text editor:
$ nano MyJScode.js
Now we 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, 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:
$ node MyJScode.js
Conclusion
Through this tutorial, we have learned 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.