Composer Uses Wrong PHP Version, But php -v Shows the Correct One (Ubuntu)
When you run composer install on an Ubuntu 16.04.3 LTS machine and immediately encounter the error This package requires php >=7.0.0 but your PHP version (5.5.9), it is easy to assume a system-level misconfiguration. However, if you execute php -v directly in the terminal and see PHP 7.0.22-0ubuntu0.16.04.1, the discrepancy indicates that Composer is invoking a different binary than the one your shell is currently using. This is not a rare edge case; it is a common consequence of how Linux package managers handle multiple versions of the same executable in the /usr/bin directory.
The root cause lies in the order of directories listed in your system's PATH environment variable. When you type php -v, the shell searches for the php executable starting from the first directory in $PATH. On Ubuntu, the PHP 7.x package is typically installed to /usr/bin/php, while older versions like PHP 5.5 might reside in /usr/bin/php5 or simply share the name if not managed correctly via symlinks. If your PATH prioritizes a location containing an older binaryâperhaps from a previous installation, a development environment tool, or a misconfigured symlinkâComposer will execute that version instead of the one you verified with php -v.
To diagnose this, you must inspect exactly which binary Composer is invoking. Run the following command:
which php
If this returns /usr/bin/php but the version output matches PHP 7.0.22, proceed to check how Composer determines its own environment. By default, modern versions of Composer automatically detect the PHP executable based on the system's PATH. However, older versions or specific deployment setups might rely on an explicit path stored in configuration files or a hardcoded variable if the auto-detection fails silently.
You can force Composer to use a specific PHP binary by setting the vendor.bin or simply overriding the path via the command line:
composer -d /path/to/php7 bin/require your/package
Or, more commonly, you can update the php symlink to point explicitly to the correct version if multiple versions exist side-by-side. On Ubuntu 16.04, it is crucial to ensure that /usr/bin/php points to the PHP 7 binary and not a legacy link to PHP 5. If you have both installed via apt, check for conflicting symlinks:
ls -l /usr/bin/php*
If you find /usr/bin/php pointing to the old version, remove the symlink or update it:
sudo ln -s /usr/bin/php7.0 /usr/bin/php
Another frequent pitfall involves the phpize tool or extensions compiled against a specific PHP version. If your system has both PHP 5 and PHP 7 installed but extensions are only loaded for one, Composer might inadvertently pick up the wrong runtime environment depending on how it resolves dependencies. Ensure that the cli binary matches the fpm or apache2 module if you are running a web server, as mismatches here can lead to runtime failures even if installation succeeds.
In some cases, the issue stems from the Composer version itself being outdated. Older versions of Composer (prior to 1.0) did not always handle multi-version PHP environments gracefully and might fall back to default system paths that differ from interactive shells. Upgrading Composer ensures that it respects the current PATH configuration:
composer self-update
Additionally, verify that your shell environment variables are consistent between your interactive terminal and any scripts or deployment tools you use. If you are deploying via a CI/CD pipeline or a cron job, those environments often have stripped-down PATH values that might exclude the directory where PHP 7 resides. Always test your setup in the exact same environment where Composer runs to catch these discrepancies early.
If you are managing a Laravel project specifically, ensuring consistency between the CLI and the web server is non-negotiable. A mismatch here can result in "PHP version required" errors appearing intermittently during deployment or development. For robust setups, consider isolating PHP versions using tools like phpenv or managing them strictly through Docker containers to eliminate ambiguity entirely.