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.

Step 1: Update Your System

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:

sudo apt update && sudo apt upgrade -y

Step 2: Install Node.js

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.

  1. 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:

    curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
  2. Install Node.js:

    After the repository is added, install Node.js using the apt package manager:

    sudo apt install -y nodejs
  3. Verify the installation:

    To check if Node.js and npm (Node Package Manager) were installed successfully, run the following commands:

    node -v
    npm -v

    This will display the installed versions of Node.js and npm.

Step 3: Install PM2

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.

  1. 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:

    sudo npm install -g pm2
  2. Verify the installation:

    After installation, verify that PM2 was installed correctly by checking its version:

    pm2 -v

    If the version number is displayed, PM2 is installed successfully.

Step 4: Running Your Node.js Application with PM2

Now that you have Node.js and PM2 installed, you can use PM2 to run your Node.js applications in the background.

  1. 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:

    pm2 start app.js

    Replace app.js with the filename of your Node.js application.

  2. Check the status of your application:

    To see the status of your running applications, use the following command:

    pm2 list

    This will display a list of all applications managed by PM2, including their status and memory usage.

Step 5: Keep PM2 Running After a System Restart

To ensure that PM2 restarts your applications after a system reboot, use the following command:

pm2 startup

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 save

Step 6: Managing Your Application with PM2

PM2 offers many commands to help you manage your applications. Here are some useful commands:

  • Stop an application:

    pm2 stop app.js
  • Restart an application:

    pm2 restart app.js
  • View application logs:

    pm2 logs
  • Delete an application:

    pm2 delete app.js

Conclusion

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.