📒 

Node.js is a popular JavaScript runtime that enables you to run server-side JavaScript. PM2 is a process manager for Node.js that helps manage your applications, ensuring they run smoothly, automatically restart upon crashes, and allow for easy management of multiple apps. This guide will walk you through installing Node.js and PM2 on an Ubuntu system.

Prerequisites

  • Ubuntu version: This guide works with Ubuntu 20.04, 22.04, or newer.
  • User privileges: You need sudo or root access to install and configure software on your server.

Step 1: Update System Packages

Before starting, make sure that your system packages are up to date:

sudo apt update
sudo apt upgrade -y

Step 2: Install Node.js

There are multiple ways to install Node.js on Ubuntu. The most recommended methods are using NodeSource or nvm (Node Version Manager).

Method 1: Install Node.js Using NodeSource

  1. Add the NodeSource Repository:

    To install the latest LTS (Long-Term Support) version of Node.js, use the following commands:

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

    This script will add the NodeSource repository to your system and update the package list.

  2. Install Node.js:
    sudo apt install nodejs -y

    This command installs both Node.js and npm (Node Package Manager).

  3. Verify the Installation:

    Check if Node.js and npm were installed correctly by running:

    node -v
    npm -v

    These commands should output the version numbers of Node.js and npm.

Method 2: Install Node.js Using NVM

Using nvm allows you to manage multiple versions of Node.js on the same machine.

  1. Install NVM:

    Download and install nvm using the installation script:

    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash

    (Note: Replace v0.39.5 with the latest version if needed).

  2. Reload the Shell Configuration:

    After installing nvm, you need to reload your shell configuration:

    source ~/.bashrc
  3. Install Node.js with NVM:

    To install the latest LTS version of Node.js:

    nvm install --lts
  4. Verify Node.js and npm Installation:
    node -v
    npm -v

Step 3: Install PM2

PM2 is a Node.js process manager that allows you to manage your Node.js applications with ease.

  1. Install PM2 Globally:

    Use npm to install PM2 globally:

    sudo npm install -g pm2
  2. Verify PM2 Installation:

    Check that PM2 was installed correctly by running:

    pm2 -v

    This command should output the PM2 version number.

Step 4: Using PM2 to Manage Node.js Applications

Once PM2 is installed, you can use it to run and manage your Node.js applications.

  1. Start a Node.js Application:

    Navigate to the directory of your Node.js application and use PM2 to start it:

    pm2 start app.js

    Replace app.js with the entry point of your application.

  2. View Running Processes:

    To see all processes managed by PM2, run:

    pm2 list

    This will display a list of all running applications along with their statuses and other details.

  3. Restart, Stop, and Delete Applications:
    • Restart an Application:
      pm2 restart app
    • Stop an Application:
      pm2 stop app
    • Delete an Application:
      pm2 delete app

    Replace app with the name or ID of your application.

  4. Monitor Logs:

    To see the logs of all your PM2 applications, use:

    pm2 logs

    To see logs for a specific application:

    pm2 logs app

Step 5: Configure PM2 to Start on Boot

To ensure that PM2 starts automatically whenever your server restarts, use the following command:

pm2 startup

This command will display a command that you need to run with sudo to set up the startup script. Copy and paste that command into your terminal.

After running the command, save the current process list to ensure your applications are started on reboot:

pm2 save

This saves the list of applications currently managed by PM2, and it will restart them automatically when the server reboots.

Step 6: Deploying Applications with PM2

PM2 can also be used for zero-downtime deployments using a feature called ecosystem files. Here’s how to set up a simple ecosystem.config.js file:

  1. Create the Ecosystem File:
    pm2 ecosystem

    This generates a file named ecosystem.config.js with a basic configuration.

  2. Edit the Ecosystem File:

    Open the ecosystem.config.js file and configure it with your application details:

    module.exports = {
    apps: [
    {
    name: 'my-app',
    script: 'app.js',
    instances: 2,
    exec_mode: 'cluster',
    env: {
    NODE_ENV: 'development'
    },
    env_production: {
    NODE_ENV: 'production'
    }
    }
    ]
    };

    This example runs your application as a cluster with 2 instances, which can take advantage of multi-core processors.

  3. Start Applications Using the Ecosystem File:

    To start your applications using the ecosystem file:

    pm2 start ecosystem.config.js --env production

Step 7: Updating Node.js and PM2

To update Node.js, use nvm if you installed it using that method, or run the NodeSource setup script again if you used that method.

To update PM2:

sudo npm install -g pm2@latest
pm2 update

Conclusion

You have now successfully installed Node.js and PM2 on Ubuntu, and you’ve learned how to use PM2 to manage your Node.js applications. PM2 simplifies process management by automatically restarting your applications if they crash and making deployments easier. With this setup, you can ensure your Node.js applications run smoothly and are ready for production. Happy coding!