How to install PM2 (Process Management) on Ubuntu 22.10 / Debian 11
PM2 is a fantastic process manager designed specifically for Node.js applications.
Throughout my journey as a web developer, I've worked extensively with Node.js. I've had to schedule and manage a multitude of Node.js applications using CRON, which, let me tell you, was no walk in the park. That was until I discovered PM2! It was a game-changer, making my life significantly easier. PM2 ensures my apps are always up and running, and it automatically refreshes them whenever I make updates. Plus, it gives me the flexibility to manually set the reload time using CRON or adjust the restart delay for any application. The best part? It works seamlessly across all operating systems!
Now, you might be thinking, "Is PM2 only for Node.js apps?" The answer is a resounding no! While PM2 was indeed created with Node.js applications in mind, its utility isn't confined to just that. After using PM2 for a while, I discovered that it can manage scripts from any programming language! I gave it a whirl with Python, and guess what? It worked like a charm!
In this article, I'm going to share a practical example of how you can schedule and automate your Python scripts using PM2. So, buckle up and let's dive in!
Step 1: Update Your System
First things first, let's make sure your system is up-to-date. Open up your terminal and type in the following command:sudo apt update
This command will fetch the list of available updates and then upgrade your system. The -y flag automatically confirms all prompts, saving you from having to manually approve each update.
Step 2: Install Node.js
PM2 is a Node.js application, so we'll need to have Node.js installed on our system. Here's how to do it:sudo apt install nodejs -y
Once the installation is complete, you can verify it by checking the version of Node.js:nodejs -v
You should see a version number as the output, which means Node.js is installed correctly.
Step 3: Install NPM (Node Package Manager)
NPM is the default package manager for Node.js and it's what we'll use to install PM2. To install NPM, use the following command:sudo apt install npm -y
Just like we did with Node.js, we can check if NPM is installed correctly:npm -v
If you see a version number, you're good to go!
Step 4: Install PM2
Now that we have Node.js and NPM installed, we can finally install PM2. Here's the command to do it:sudo npm install -g pm2
The -g flag installs PM2 globally, which means you can use it from any directory on your system.
To check if PM2 is installed correctly, you can use the following command:pm2 -v
If you see a version number, congratulations! You've successfully installed PM2 on your Ubuntu 22.10 or Debian 11 system.
Step 5: Set PM2 to Start on Boot
One of the great things about PM2 is that it can automatically restart your applications if your system reboots. To set this up, you can use the following command:pm2 startup
This command will generate a command that you need to run with superuser privileges. Copy the outputted command and run it:sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u yourusername --hp /home/yourusername
Remember to replace yourusername with your actual username.
And there you have it! You've installed PM2 on your Ubuntu 22.10 or Debian 11 system. Now you can use PM2 to manage and keep your Node.js, Python or Any script or applications running in the background.
Thank you for reading.