How to Install Node.js and PM2 on an Ubuntu VPS
Node.js is a powerful runtime for running JavaScript outside the browser, perfect for scalable web apps. PM2, a process manager for Node.js, ensures uptime and simplifies management. This guide walks you through installing Node.js and PM2 on an AvaHost Ubuntu VPS (20.04/22.04/24.04), with practical examples to deploy and manage applications efficiently.
Prerequisites
AvaHost VPS with Ubuntu 20.04, 22.04, or 24.04.
Root access or
sudoprivileges (see prior SSH guide).Basic Linux command knowledge.
A static IP or domain (e.g.,
app.yourdomain.com).
Step 1: Update System Packages
Before installing Node.js and PM2, update your package list to ensure you have the latest versions available:
sudo apt update && sudo apt upgrade -yStep 2: Install Node.js
There are multiple ways to install Node.js on Ubuntu 20.04. We will use the NodeSource repository to get the latest stable version.
Install Node.js via NodeSource
- Add the NodeSource repository:
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -Replace
18.xwith the latest LTS version if necessary. - Install Node.js and npm:
sudo apt install -y nodejs - Verify the installation:
node -v npm -vThis should output the installed versions of Node.js and npm.
Step 3: Install PM2
PM2 is a process manager that helps keep your Node.js applications running.
- Install PM2 globally using npm:
sudo npm install -g pm2 - Verify the installation:
pm2 -vThis should return the installed version of PM2.
Step 4: Run a Node.js Application with PM2
To demonstrate PM2, we will create a simple Node.js application and run it.
- Create a sample application:
mkdir myapp && cd myapp echo "console.log('Hello from Node.js!');" > app.js - Start the application using PM2:
pm2 start app.js - List running processes:
pm2 list - Save the process list so that it restarts on system reboot:
pm2 save - Enable PM2 to start on boot:
pm2 startupFollow the instructions provided by the command to complete the setup.
Step 5: Monitor and Manage Applications
PM2 provides various commands to manage and monitor applications:
- Restart an application:
pm2 restart app.js - Stop an application:
pm2 stop app.js - Delete an application from PM2:
pm2 delete app.js - View logs:
pm2 logs
Conclusion
Installing Node.js and PM2 on an AvaHost Ubuntu VPS is simple and enables robust app management. The examples, like running app.js with PM2 or setting up Nginx, ensure your apps are scalable and reliable. With AvaHost’s high-performance infrastructure, you can deploy Node.js applications with confidence, ensuring uptime and easy monitoring. Explore PM2’s features (man pm2) and AvaHost’s VPS plans to power your projects.


