Logo

How to Install PM2 in Ubuntu 23.04

Dec 02, 2023

To install PM2 on Ubuntu 23.04, follow the steps below.

Step 1 : Install PM2

Begin by installing PM2 using npm (Node Package Manager). Open a terminal and run the following command:

npm install -g pm2

This command installs PM2 globally on your system.

Step 2 : Run PM2 at Startup

To ensure PM2 starts automatically at boot, run the following command:

pm2 startup

Step 3 : Build a Simple Node.js App

Create a basic Node.js application. For example, create a file named app.js with the following content:

const http = require('http');
const server = http.createServer((req, res) => {
  res.end('Hello, PM2!');
});

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

Step 4 : Start the App with PM2

Start your Node.js app using PM2 with the following command:

pm2 start app.js --name my-app

Replace app.js with your application file and my-app with the desired name.

Step 5 : Manage the App with PM2

You can manage your app using PM2 commands. Here are some examples:

- Restart the app:

pm2 restart my-app

- Reload the app:

pm2 reload my-app

- Stop the app:

pm2 stop my-app

- Delete the app from PM2:

pm2 delete my-app

Congratulations! You have successfully installed PM2 on Ubuntu 23.04, created and managed a simple Node.js app using PM2.

Recommended