How To Fix "/Usr/Bin/Env: Php: No Such File Or Directory" Error When Using Composer?

Published September 1, 2024

Problem: Composer error with PHP directory

The "/usr/bin/env: php: No such file or directory" error happens when using Composer, a dependency management tool for PHP. This error indicates that PHP is not installed on your system or not set up in the system's PATH.

Troubleshooting the Composer Installation Issue

Checking PHP installation and version

To verify if PHP is installed on your system, open a terminal and type:

php -v

This command shows the PHP version if it's installed. If you see an error message, PHP might not be installed or not in your system's PATH.

For CentOS 7 with PHP 7.1, you can also try:

php71 -v

If this command works, it confirms that PHP 7.1 is installed on your system.

Tip: Check PHP modules

To check which PHP modules are installed, use the command:

php -m

This can help identify if required modules for Composer are present.

Verifying Composer installation

To confirm if Composer is correctly installed, run:

composer --version

This command should show the Composer version. If you get a "command not found" error, Composer might not be installed or not in your system's PATH.

Common Composer installation problems include:

  • Incorrect installation directory: Make sure Composer is installed in a directory included in your system's PATH.

  • Incorrect file permissions: Check if the Composer file has the correct executable permissions.

  • PHP not in PATH: Composer requires PHP to run. If PHP is not in your system's PATH, Composer will fail to execute.

  • Incompatible PHP version: Make sure your PHP version meets Composer's requirements.

To fix these issues, you may need to reinstall Composer or change your system's PATH settings.

Fixing the PHP Path Issue for Composer

Updating system PATH for PHP

The system PATH is a list of directories your operating system searches for executable files. To change the system PATH:

  1. Open the ".bash_profile" file in your home directory:
nano ~/.bash_profile
  1. Add this line at the end of the file:
export PATH=$PATH:/usr/bin
  1. Save the file and exit the editor.

  2. Apply the changes:

source ~/.bash_profile

To add PHP to the PATH:

  1. Find your PHP installation directory. For PHP 7.1 on CentOS, it's usually "/usr/bin/php71".

  2. Open the ".bash_profile" file again:

nano ~/.bash_profile
  1. Add the PHP directory to your PATH:
export PATH=$PATH:/usr/bin/php71
  1. Save the file and apply the changes:
source ~/.bash_profile

After making these changes, Composer should be able to find and use PHP correctly.