How to Install Node.js and Pm2 in Ubuntu 20.04
Posted on September 14th, 2022
NodeJS is one of the most used backend programming languages by companies in recent years. Most of the companies who are working on a project are either using MEAN which is MongoDB, ExpressJS, AngularJS, and NodeJS, or MERN which is MongoDB, Express JS, ReactJS, and NodeJS for frontend and backend.
In both of the above-given collections of powerful Technologies, NodeJS remains in common.
There is a huge demand in the IT market for engineers who know NodeJS for backend programming. Today we will learn about how you can install NodeJS on the Ubuntu Linux operating system.
There are multiple ways to install Node.js on the Ubuntu operating system but for today we will be installing Node.js by using the apt package installer along with a Nodesourse source PPA.
Commands To Install Node.js on Ubuntu Linux
Note – The apt package manager is the default package manager for ubuntu OS. In case you are using Redhat Linux or Fedora the commands may differ.
All the commands which are required to install Node.js are mentioned below in chronological order. These commands must be executed in the sequence given below.
First of all, We install curl in Ubuntu so that we can download packages from servers.
$ sudo apt install curl
Now we download and run the install.sh script for GitHub .
$ curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
Now we reload the
bashrc using the following command to apply the changes. This command will not generate any output.
$ source ~/.bashrc
We install the node by using the below command.
$ nvm install node
To install one specific version of the node we use the below command. If you want to install another version then write the version after v.
For example –
$ nvm install v14.0.0
Command to install v12.18.3 –
$ nvm install v12.18.3
I have performed the practical along with you with the above-mentioned commands and have installed node version 8.9.4. Now, let’s check if I have properly installed Node.js or not. We check by using the following command.
$ node --version
You can install multiple versions of node js at one time. I have installed node version 14.0.0 as well.
Now, If you want to delete this specific node version then you can run this command.
$ nvm uninstall v14.0.0
Similarly, if you want to switch from one specific version of Node.js to another version then you can use the list command.
$ nvm list
The nvm list command will show all the node versions available on your computer.
Command to use different versions of Node.Js is given below
$ nvm use v18.7.0
Many times developers work on different modules for which different versions of Node.js is required for the frontend and backend.
So, it is very essential to have all the requested Node.js versions on your operating system so that while compiling the code and building the application the npm error doesn’t show up.
For installing all the dependencies for a Node.js application we use the below command.
$ npm install or $ npm i
What Is pm2 And Why It Is Necessary For Node.js?
Node.js has a process manager by the name of pm2. The name process manager itself gives us the idea that it manages the processes for backend Node.js applications.
Any Node.js backend application can be started with the help of pm2.
With this process manager, it is possible to create, terminate, and even monitor a Node.js application without much effort. It’s surprising that pm2 can single-handedly do so many things at once.
Advantages of using PM2 with Node.js Over other process managers
- With pm2 we can start, stop, and terminate a Node.js application.
- For monitoring, there is no need to install other tools like Grafana and Prometheus, because all the BE applications can be monitored with pm2 which has an inbuilt monitoring system.
- If we want to make changes in the code without having any downtime for our application, then pm2 can successfully achieve that.
- Even if the application crashes, it can be relaunched just by reloading the pm2 application through its ID or name.
Commands To Install Pm2 On Ubuntu OS
Now let’s see how you can install pm2 on your Ubuntu Linux operating system.
Follow the commands in the below-given in sequence.
To install pm2, execute the following command. The -g is to install pm2 globally.
$ sudo npm install pm2 -g
Now execute the following command for updating the apt package manager.
$ sudo apt update && apt install sudo curl && curl -sL https://raw.githubusercontent.com/Unitech/pm2/master/packager/setup.deb.sh | sudo -E bash -
To check if pm2 is installed properly use the below command. Right now there are no pm2 applications running so it’s showing blank.
$ pm2 ls
In this short tutorial, we have covered what Node.js is and how to install it on Ubuntu OS. Also, we discussed what is pm2 in Node.js, its advantages and Linux commands to install it. Node.js has become very popular for backend development in recent times. It is trendy and easy to understand. And it is very easy to install and manage on an Ubuntu OS.
We hope this tutorial was helpful for you. In case of any doubt or errors, please mention them in the comment section.