How to always use ignore-platform-reqs flag when running composer?
Asked Answered
S

5

68

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.

Sarisarid answered 8/2, 2016 at 15:43 Comment(3)
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!Unapproachable
@Unapproachable 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.Sarisarid
No worries I was able to fix my issue, thanks for replying you sir are good man! :-) Cheers!Unapproachable
V
51

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

Velvetvelveteen answered 8/2, 2016 at 16:15 Comment(7)
Could you please provide a full config.json example? It seems that for me the config is currently ignored.Sarisarid
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 ....Velvetvelveteen
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.Sarisarid
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?Velvetvelveteen
We are using hhvm :D Not ideal, but it seems that composer does not check PlattformSarisarid
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 fileDepend
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.Marston
P
41

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.

Paynim answered 10/4, 2021 at 11:19 Comment(0)
S
19

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.

Spiv answered 28/7, 2022 at 2:32 Comment(1)
This is should be the best answer as of 2022Whomp
B
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.

Buckeen answered 10/10, 2017 at 16:28 Comment(1)
you can bypass alias by running \composer.Metrist
I
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

Indira answered 1/4, 2021 at 6:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.