Install and use node js on ubuntu 22.04; Through this tutorial, we will show you 3 simple ways of how to install, uninstall and use node js in linux ubuntu 22.04 system using terminal or command line.
How to Install and Uninstall Node JS in Ubuntu 22.04 using Terminal
There are 3 simple ways to install and use node js on linux ubuntu 22.04 using terminal:
- 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
- Uninstall Node js in Ubuntu 22.04
Method 1: Installation of Node.js using the default repository of Ubuntu 22.04
Open the command line or terminal and execute the following command into it 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
To install node js using the ppa repository, you can do it by using the following command:
$ 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
Uninstall Node js in Ubuntu 22.04
Press Ctrl + Alt + T
to open a terminal window. This is where you’ll enter the commands to uninstall Node.js.
Ubuntu 22.04 uses the apt
package manager to install and manage software packages. To uninstall Node.js, you’ll use apt
. Run the following command to uninstall Node.js:
sudo apt remove --purge nodejs
Conclusion
Through this tutorial, we have learned how to install and uninstall 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.