Skip to main content

Command Palette

Search for a command to run...

How to install PM2 on Ubuntu: A Comprehensive Guide

Junior Guide to Install pm2 on ubuntu server

Published
2 min read
How to install PM2 on Ubuntu: A Comprehensive Guide
R

Full-stack developer with mastery across mobile (React Native, Ionic), front-end (React, Svelte, Vue.js), back-end (Express.js, FastAPI, Django), e-commerce (Shopify, OpenCart), and CMS (Wordpress, Laravel, Drupal, headless). Building web and mobile applications that deliver!

PM2, the powerful process manager for Node.js applications, simplifies managing and monitoring your Node.js projects on Ubuntu. This guide will walk you through the installation and utilization of PM2, empowering you to take control of your applications' runtime environment.

Prerequisites:

  • Ubuntu Operating System

  • Basic understanding of Node.js and NPM

1. Installation:

There are two primary methods for installing PM2:

a) Using NPM:

This is the recommended method for most users. Open a terminal and run the following command:

npm install pm2 -g

The -g flag ensures global installation, making PM2 accessible from any directory.

b) Using APT Package Manager:

This method works for older Ubuntu versions and might require additional dependencies. Execute the following commands in sequence:

sudo apt update && sudo apt install curl
curl -sL https://raw.githubusercontent.com/Unitech/pm2/master/packager/setup.deb.sh | sudo -E bash -
sudo apt install pm2

2. Verification:

Once installed, verify PM2 by running:

pm2 -v

This should display the installed PM2 version.

3. Getting to Know PM2:

PM2 offers a plethora of commands for managing your applications. Here's a glimpse into some essential ones:

  • pm2 list: Lists all running applications managed by PM2.

  • pm2 start <app_name>: Starts an application using the specified name or script path.

  • pm2 stop <app_name>: Stops the specified application.

  • pm2 restart <app_name>: Restarts the specified application.

  • pm2 logs <app_name>: Shows the logs of the specified application.

  • pm2 delete <app_name>: Deletes the specified application from PM2's management.

4. Basic Usage:

Let's walk through a basic example of using PM2. First, create a simple Node.js application named index.js with the following code:

JavaScript

const http = require('http');

const server = http.createServer((req, res) => {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World from PM2!');
});

server.listen(3000, () => {
  console.log('Server started on port 3000');
});

Run the application using PM2:

pm2 start index.js

This starts the application and assigns it a process ID. You can view the running application using pm2 list.

To stop the application, use:

pm2 stop index

5. Going Further:

PM2 offers numerous advanced features like cluster mode for load balancing, process monitoring dashboards, and even remote management. Explore the official documentation and online resources for in-depth information:

By mastering PM2, you can effectively manage and scale your Node.js applications on Ubuntu, ensuring smooth operation and optimal performance. Remember, experimentation and exploration are key to unlocking the full potential of this powerful tool!

We hope this guide provides a solid foundation for your PM2 journey on Ubuntu. Feel free to leave any questions or comments below, and happy Node.js development!

How to install PM2 on Ubuntu: A Comprehensive Guide