Comprehensive Guide to Resolving "PM2 Command Not Found" in Linux Systems

Dec 01, 2025 · Programming · 12 views · 7.8

Keywords: PM2 | Node.js | Linux Deployment

Abstract: This article provides an in-depth exploration of the "command not found" issue when installing and using the PM2 process manager on Linux systems, particularly CentOS 7. By analyzing Q&A data and reference documentation, it systematically explains the differences between global and local installations, the configuration mechanism of the PATH environment variable, and the core functionalities of PM2. Starting from practical problems, the article details how to resolve command recognition issues through global installation, then expands to cover advanced features such as process management, cluster mode, and monitoring logs, concluding with complete configuration examples and best practice recommendations.

Problem Background and Diagnosis

When deploying Node.js applications in Linux server environments, PM2 is widely used as a process manager. However, many developers may encounter errors similar to "pm2: command not found" during initial usage. This typically occurs when users attempt to run PM2 commands directly in the terminal after performing a local installation via npm install pm2 in the project directory.

Analysis of Installation Methods

Node.js's package manager npm supports two installation modes: local and global. Local installation places modules in the current project's node_modules directory, making them available only within that project scope. Global installation, on the other hand, links the module's executable files to directories included in the system's PATH environment variable, allowing the command to be recognized and executed in any terminal session.

According to the best answer in the Q&A data, the core solution to the "command not found" issue is to use global installation. The specific operation is as follows:

sudo npm install -g pm2

The -g parameter explicitly instructs npm to perform a global installation. For CentOS 7 systems, due to permission restrictions, the sudo prefix is typically required to obtain administrator privileges. After installation, the system automatically adds PM2's executable file path to the environment variables, allowing users to use the pm2 command from any directory.

Environment Variable Configuration Mechanism

If the command remains unrecognized after global installation, manual configuration of the PATH environment variable may be necessary. As shown in the supplementary answer, the installation path of PM2 can be located using the whereis pm2 command, and then added to the user's ~/.bashrc file:

export PATH=$PATH:/opt/node/lib/node_modules/pm2/bin

After configuration, execute source ~/.bashrc to make the changes take effect immediately, or restart the terminal session. This method ensures the system can correctly resolve the location of the PM2 command.

Detailed Explanation of PM2 Core Features

According to the reference article, PM2 is not just a simple process launcher but provides complete application lifecycle management capabilities. Below is an in-depth analysis of some key features:

Process Startup and Management

The basic command to start an application is pm2 start app.js. PM2 supports various application types, including Node.js scripts, shell scripts, Python applications, etc. More granular control can be achieved by adding parameters, for example:

pm2 start server.js --name myapp --watch --max-memory-restart 200MB

This command starts a process named "myapp," enables file watch auto-restart, and sets a memory threshold restart mechanism.

Cluster Mode and Load Balancing

For Node.js applications, PM2 includes a built-in cluster mode that automatically creates multiple worker processes and implements load balancing. Use the -i parameter to specify the number of processes:

pm2 start app.js -i max

This starts the maximum number of process instances based on CPU cores, significantly improving the application's concurrent processing capability.

Monitoring and Logging System

PM2 provides multiple monitoring methods: pm2 list displays the status of all processes; pm2 logs shows real-time log output; pm2 monit launches a terminal dashboard. For production environments, pm2 plus can be used to access a web monitoring interface.

Ecosystem Configuration Files

For complex application deployments, it is recommended to use ecosystem files (ecosystem.config.js) to centrally manage configurations. This file can define multiple applications, environment variables, and advanced options:

module.exports = {
  apps: [{
    name: "api-server",
    script: "./server.js",
    instances: 2,
    env: {
      NODE_ENV: "production"
    }
  }]
}

Configuration Examples and Best Practices

Combining the Q&A scenario, here is a complete configuration example:

  1. Install PM2 globally: sudo npm install -g pm2
  2. Navigate to the project directory: cd /path/to/chat
  3. Start the application: pm2 start server.js --name chat-app
  4. Set up auto-start on boot: pm2 startup && pm2 save
  5. Configure log rotation: pm2 install pm2-logrotate

For production environments, additional configurations are recommended:

Troubleshooting and Advanced Recommendations

If issues persist after following the above steps, troubleshoot in the following order:

  1. Verify Node.js and npm version compatibility
  2. Check if the global installation path output by npm root -g is included in PATH
  3. Confirm successful installation by checking pm2 --version
  4. Examine system firewall and SELinux settings to ensure process execution is allowed

For advanced users, explore PM2's module system by adding functional extensions via pm2 install <module>, such as log management, performance monitoring plugins, etc.

Through this systematic analysis, readers will not only resolve the specific "command not found" issue but also gain a deep understanding of PM2's complete value proposition in Node.js application deployment, laying a solid foundation for building stable and reliable production environments.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.