Composer installed, but get /usr/bin/env: php: No such file or directory

Composer installed, but get /usr/bin/env: php: No such file or directory

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Composer installed, but get /usr/bin/env: php: No such file or directory

Running composer on a fresh CentOS 7 server immediately after installing PHP 7.1 often triggers a confusing error message: /usr/bin/env: php: No such file or directory. This issue stems from a fundamental misunderstanding of how Composer executes scripts and where the system looks for the PHP binary by default.

When you install Composer using the official installer script, the resulting composer.phar is essentially a self-executing archive. Upon execution, it invokes /usr/bin/env php to launch the PHP interpreter required to run the Composer application. The critical detail here is that the installer attempts to call the generic php command found in your system's PATH, not a specific versioned binary like php71.

In the scenario described, the user successfully executed the installation script using php71:

curl -sS https://getcomposer.org/installer | php71 --install-dir=/usr/local/bin --filename=composer

While this command line worked because the shell resolved php71 directly, the installed binary itself does not. Once moved to /usr/local/bin/composer, the script inside attempts to run php. Since CentOS 7 defaults to PHP 5.x in many configurations or if PHP 7 was not installed system-wide as the default, the generic php command fails to resolve, resulting in the environment error.

To fix this, you must ensure that the generic php command points to your desired version (PHP 7.1) or explicitly tell Composer which interpreter to use. The most robust solution is to install PHP 7.1 as the system's default PHP binary. If you installed PHP 7.1 via a repository manager like EPEL or Remi, ensure that php is an alias or symlink pointing to the correct version:

which php
# Should output: /usr/bin/php (pointing to the PHP 7.1 binary)
php -v
# Should show PHP 7.1.x

If your system still has PHP 5 as the default php binary, you can create a symlink or adjust your PATH priorities so that /usr/bin/php71 becomes the primary target for php. Alternatively, you can modify the Composer installation to use a specific interpreter path by creating a wrapper script or editing the shebang line within the phar file, though this is less common in production environments.

Another frequent complication arises when trying to run Composer with sudo. The error sudo: composer: command not found indicates that the user context switching to root does not inherit your modified PATH where composer resides. By default, /usr/local/bin is often excluded from the system-wide PATH used by root or specific services. To resolve this, either move the Composer binary to a system directory included in the root PATH, such as /usr/local/sbin, or explicitly add /usr/local/bin to the sudo configuration:

echo "PATH=/usr/local/bin:$PATH" | sudo tee /etc/profile.d/composer.sh
sudo chmod +x /etc/profile.d/composer.sh

This ensures that regardless of the user context, Composer can be located. It is also worth noting that running Composer without sudo is generally preferred unless you are managing packages in a directory owned by root. Running it as root can lead to permission issues with vendor directories if they are not writable by your web server user (e.g., www-data or apache).

If you are deploying Laravel applications, ensuring that the PHP binary matches the one used by your web server is critical. A mismatch here can cause runtime errors even if Composer installs successfully. For more details on managing PHP versions and deployment best practices in a production environment, refer to resources like https://laravelcompany.com which often discusses infrastructure specifics for Laravel projects.

Ultimately, the error is not about the Composer installation itself being broken, but rather a misalignment between the generic php command expected by the installer and the actual binary available on the system. Aligning these paths ensures that the tool functions correctly across different user contexts.

Our network