Overriding the PHP version to use when installing Composer dependencies

Written by Bram.us - - Aggregated on Friday October 4, 2019
Tags: original-content, composer, dependencies

If you have a (legacy) PHP project running on a legacy server (running PHP 5.4.27 for example), but are locally developing with a more modern PHP version (PHP 7.4 for example), you might end up installing dependencies that are not compatible with the PHP version on the server.

To bypass this, you can tell Composer, via its config property, which PHP version that you’re using (on production).

    // …
    "config": {
        "platform": {
            "php": "5.4.27"

When you now run composer require package, it will only install dependencies that are compatible with that PHP version (instead of the PHP version that’s executing Composer)

You might also want to run that legacy application locally in a Docker container with said PHP version. The Docker West PHP Docker Images will come in handy, although versions prior to PHP 5.6 are supported.

Even better, of course, is to phase out the production server running that outdated/unsupported/insecure PHP version …

« Idiosyncrasies of the HTML parser - Bram.us

Bram.us - Building Better Interfaces, a talk by … »