How to always use ignore-platform-reqs flag when running composer? – Php

Photo of author
Written By M Ibrahim
composer-php configuration hhvm

Quick Fix: To consistently use the ignore-platform-reqs flag while running Composer, consider faking the PHP version instead. Add "platform": {"php": "5.5"} to your ~/. composer/config.json file or use the Composer config -g -e command to edit it.

The Problem:

A user is working on a project that has a dependency on PHP version 5.5, but their local machine is running PHP version 7.0.3. When they run composer install, they get an error because their PHP version does not meet the requirement. They know they can ignore the platform requirement by using the --ignore-platform-reqs flag, but they often forget to add the flag. They want to find a way to make their local composer always assume --ignore-platform-reqs without having to set an alias or modify the composer configuration.

The Solutions:

Solution 1: Fake PHP Version

Rather than ignoring platform requirements, it’s advisable to fake the PHP version. To do this, add the following:

"platform": { "php": "5.5" }

to your ~/.composer/config.json file.

You can also use composer config -g -e to edit the config file.

For example, if you need to fake both the PHP version and an extension, the config would look like:

{
    "config": {
        "platform": {
            "php": "8.1",
            "ext-bcmath": "8.1"
        }
    }
}

Refer to the Composer documentation for more information on the platform section of the config file.

Alternatively, you can use environment variables to set the PHP version as well as other platform requirements. This feature is available in Composer 2.3.0 onward.

Solution 2: Selectively Ignore Platform Requirements

Composer v2 allows you to selectively ignore specific platform requirements during installation. To always ignore the PHP version requirement, use the following command:

composer install --ignore-platform-req=php

This command will ignore the PHP version requirement and allow the installation to proceed, assuming your Docker container’s PHP version meets the project’s requirements.

Solution 3: Ignore Platform Reqs with Environment Variables

To automatically use the --ignore-platform-reqs flag when running Composer, you can set the following environment variables:

  • COMPOSER_IGNORE_PLATFORM_REQS=1: Ignores all platform requirements.
  • COMPOSER_IGNORE_PLATFORM_REQ=something: Ignores a specific requirement.

For example, to ignore all platform requirements, create an environment variable named COMPOSER_IGNORE_PLATFORM_REQS with a value of 1.

By setting these environment variables, Composer will always ignore platform requirements when it executes, eliminating the need to manually add the --ignore-platform-reqs flag.

Solution 4: Bash Alias

If you do not want to set an alias and have it work on composer config level, you can add the alias to your .bash_profile:

alias composer="composer --ignore-platform-reqs"

However, this can break commands that do not recognize this option, such as composer outdated.

To avoid this, you can create an additional alias for the original composer command:

alias composer_orig="/usr/local/bin/composer"

This way, you can use composer_orig whenever you encounter the error:

[Symfony\Component\Console\Exception\RuntimeException]
>
> The "--ignore-platform-reqs" option does not exist.