Node.js is a powerful JavaScript runtime that allows you to build scalable and high-performance applications. PM2 is a process manager for Node.js applications that enables you to keep your applications running continuously, even after a system restart. In this guide, we will show you how to install Node.js and PM2 on Ubuntu.
Before installing any software, it’s important to update your system to make sure all your packages are up-to-date. Open a terminal and run the following command:
There are several ways to install Node.js on Ubuntu, but the most common method is using the NodeSource repository. This ensures you get the latest LTS (Long-Term Support) version of Node.js.
Install the NodeSource repository:
To install Node.js, you need to first add the NodeSource repository to your system. You can do this by running the following command:
Install Node.js:
After the repository is added, install Node.js using the apt
package manager:
Verify the installation:
To check if Node.js and npm (Node Package Manager) were installed successfully, run the following commands:
This will display the installed versions of Node.js and npm.
PM2 is a popular process manager for Node.js applications that helps you keep your applications running in the background, restart them automatically if they crash, and handle log management.
Install PM2 globally:
You can install PM2 globally using npm, the package manager that comes with Node.js. Run the following command to install PM2:
Verify the installation:
After installation, verify that PM2 was installed correctly by checking its version:
If the version number is displayed, PM2 is installed successfully.
Now that you have Node.js and PM2 installed, you can use PM2 to run your Node.js applications in the background.
Start your application with PM2:
To start your Node.js application, navigate to the directory where your application is located and use the following command:
Replace app.js
with the filename of your Node.js application.
Check the status of your application:
To see the status of your running applications, use the following command:
This will display a list of all applications managed by PM2, including their status and memory usage.
To ensure that PM2 restarts your applications after a system reboot, use the following command:
This command generates a command that you need to run with sudo
in order to configure PM2 to launch on startup. After running the generated command, save the current process list:
PM2 offers many commands to help you manage your applications. Here are some useful commands:
Stop an application:
Restart an application:
View application logs:
Delete an application:
Installing Node.js and PM2 on Ubuntu is a straightforward process that enhances your ability to develop and manage Node.js applications. With Node.js installed, you can build server-side applications using JavaScript, and with PM2, you can manage your application processes efficiently, ensuring your applications stay online even after a server restart.