Home//

How to Fix pm2: command not found

How to Fix pm2: command not found

Minh Vu

By Minh Vu

Updated Dec 07, 2023

In this tutorial, I will help you to fix the pm2: command not found error the easiest way.

In short, the pm2: command not found error occurs because the pm2 package is not recognized by the terminal, which means it is not installed yet, or not installed globally.

So the solution is to install it using sudo npm install -g pm2 or sudo yarn global add pm2.

To understand more, you can read the content below.

What is pm2?

In case you are following some tutorials on the Internet and don't know what pm2 is, let me explain it to you.

pm2 is a process manager for Node.js applications. It helps you to:

  • Run an application and keep it alive.
  • Reload application without downtime.
  • Perform common sys-admin tasks.

For my use case, I usually use pm2 to run my NextJs application in a VPS hosted on DigitalOcean.

Why pm2: command not found Error Occurs?

The most popular reason why you encounter the pm2: command not found error is that the pm2 package is not installed, or not installed GLOBALLY.

Thus, the terminal cannot recognize the pm2 command, and it throws back the error pm2: command not found.

Fixing pm2: command not found Error

To fix the pm2: command not found error, you will need to install the pm2 package globally.

You can open your Terminal (in Linux or MacOS), and run one the following commands to install the pm2 package globally:

  • If you use npm (or no idea what you are using), run:
    shell
    sudo npm install -g pm2
  • If you use yarn, run:
    shell
    sudo yarn global add pm2
Install pm2 Globally using npm
Figure: Install pm2 Globally using npm

This command uses the npm (or yarn) package manager to install the pm2 package globally. So make sure npm is also installed. If not, it might throws back the error npm: command not found and you will need to install Node.js first.

Cool, we installed the pm2 package globally now. Move to the next section to check if it's installed successfully.

Checking if pm2 is Installed Successfully

To check if pm2 is installed successfully, run the following command:

shell
pm2 --version

If it returns the version of pm2, then you have installed pm2 successfully.

For example, at the time I write this post, the latest version of pm2 is 5.3.0, so it returns:

shell
5.3.0
Check pm2 Version
Figure: Check pm2 Version

Installing Node.js (Optional)

Node.js will provide us the npm package manager, so we need to install Node.js.

To install Node.js, there are 2 ways:

It's easier to use the installer, just download and follow the instructions. So I won't write the instructions here.

I will try to cover the nvm way right below.

Installing Node.js using nvm on Linux/MacOS

Follow these steps to install nvm on Linux/MacOS:

  1. Open Terminal.
  2. Run the following command:
    shell
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash

    I suggest you to visit the official guide to update the latest version. The version at the time I write this post is v0.39.5.

  3. Close and reopen the Terminal. Now you can install Node.js using nvm.
  4. Open Terminal.
  5. Run nvm install latest to install the latest version of Node.js.
  6. Run nvm use latest to use the latest version of Node.js.
  7. Run sudo npm install -g pm2 to install pm2 globally.

That's all, now you have installed Node.js and pm2 successfully on Linux/MacOS.

Conclusion

To recap, you can fix the pm2: command not found error by installing the pm2 package globally using npm install -g pm2 or yarn global add pm2.

In case you don't have Node.js on your machine, I also added the instructions to install Node.js using the official installer and nvm.

Thanks for reading! Please comment below if you need any more assistance.

You can search for other posts at home page.
Minh Vu

Minh Vu

Software Engineer

Hi guys, I'm the author of WiseCode Blog. I mainly work with the Elastic Stack and build AI & Python projects. I also love writing technical articles, hope you guys have good experience reading my blog!