59

On my local machine, I have php v7.0.3. A project of mine has a dependency on php v5.5.

So as expected, a simple run of composer install crashes:

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - This package requires php ~5.5 but your PHP version (7.0.3) does not satisfy that requirement.

I know I can ignore the platform via:

composer install --ignore-platform-reqs

yet I often forget to add the flag. Yet since the application runs inside a docker container, a mismatching php can install the dependencies just as fine.

So I am wondering if there is a way to make my local composer always assume --ignore-platform-reqs in order to not having to type it.

I like to avoid setting an alias and have it work on composer config level.

k0pernikus
  • 60,309
  • 67
  • 216
  • 347
  • Could you show me how you do that please? I only have 1 laravel package where I can't install it for the life of me. Yeah, I know i can ignore the platform reqs but it seems like you know how to! – Eduardo Feb 02 '21 at 23:59
  • @Eduardo Without context it's hard to know what you want to achieve. You can ask as follow-up question linking to this one. It's best you have a reproducable and minimal example of your problematic `composer.json` and local php version. – k0pernikus Feb 04 '21 at 16:34
  • No worries I was able to fix my issue, thanks for replying you sir are good man! :-) Cheers! – Eduardo Feb 04 '21 at 17:50

5 Answers5

45

It's recommended to fake the PHP version, rather than to ignore platform requirements. Add:

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

to your ~/.composer/config.json or use composer config -g -e to edit it.

An example of a config to fake the PHP version and an extension:

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

More options about the config's platform section can be found in the Composer docs.

UPDATE: starting from v2.3.0 you can use environment variables. Please see Yakatz' answer

Rvanlaak
  • 2,971
  • 20
  • 40
Alex Blex
  • 34,704
  • 7
  • 48
  • 75
  • Could you please provide a full `config.json` example? It seems that for me the config is currently ignored. – k0pernikus Feb 08 '16 at 16:42
  • hmm, just tested it on ubuntu. `composer show --platform` confirms *Package overridden via config.platform (actual:...*, and when I fake php as 3.2, `composer update` yelds *- This package requires php >=5.2 but your PHP version (3.2) does not ....* – Alex Blex Feb 08 '16 at 17:09
  • I just realized that I have both a requirement on php `~5.5` and `5.4`, so I guess that I am in a deadlock when choosing between the two. Yet what I found particular interesting is that `composer config -e` open the `composer.json` file of the project instead of the `config.json` of composer's home. – k0pernikus Feb 08 '16 at 17:30
  • Sorry, typo, as always =(. It is global config of course: `composer config -g -e`. The one which is user-specific. How do you resolve this deadlock on other environments? – Alex Blex Feb 08 '16 at 17:38
  • We are using hhvm :D Not ideal, but it seems that composer does not check Plattform – k0pernikus Feb 08 '16 at 17:41
  • If you want this tied to your specific project or to work without having composer installed globally (like me), you can place the config block in your composer.json file – MrGlass Jun 05 '17 at 16:55
  • Awesome, thanks! It also allows configuring extensions that are missing. For example when making a quick demo project that uses moneyphp (which requires ext-bcmath), and you just quickly use the composer Docker image to setup some dependencies. Just add "ext-bcmath": "8.2" to the platform config. – Rvanlaak Jul 07 '23 at 10:25
38

A new feature in Composer v2 allows you to selectively ignore platform requirements.

composer install --ignore-platform-req=php

Composer already has a --ignore-platform-reqs option (notice the s in reqs), but it ignores all platform requirements, including PHP version, extensions (ext-*), and composer-plugin-api.

The new --ignore-platform-req option can be used to set specific requirements that Composer can ignore.

Ismoil Shifoev
  • 5,512
  • 3
  • 24
  • 32
18

Composer now supports (as of version 2.3.0) checking an environment variable to set --ignore-platform-reqs.

Create an environment variable COMPOSER_IGNORE_PLATFORM_REQS=1 to ignore all or COMPOSER_IGNORE_PLATFORM_REQ=something to ignore something as a requirement.

yakatz
  • 2,142
  • 1
  • 18
  • 47
11

You can add alias composer="composer --ignore-platform-reqs" to your .bash_profile but it will break commands that don't recognize this option (eg. composer outdated).

Personally I have:

alias composer="composer --ignore-platform-reqs"
alias composer_orig="/usr/local/bin/composer"

Because most of the time I want --ignore-platform-reqs, but still I can use composer_orig each time I see

[Symfony\Component\Console\Exception\RuntimeException]

The "--ignore-platform-reqs" option does not exist.

ptkoz
  • 2,388
  • 1
  • 20
  • 28
2

On Windows, update composer.bat (under C:\ProgramData\ComposerSetup\bin) and add --ignore-platform-reqs to the composer command.

To update, you may open text-editor as administrator > Ctrl + O > Open composer.bat

If you don't want the change it globally for all your projects, create a new .bat file and use it in PHPStorm > Settings > composer

Aranya Sen
  • 974
  • 8
  • 13