11

After updating PHP from 7.4 to 8.0, I ran composer update on my existing project, and got an error like this:

  • acme/some-package[1.0.0, ..., 1.4.0] requires php ^5.6.4 || ^7.0 -> your php version (8.0.3) does not satisfy that requirement.

What does this mean, and how do I fix it?

(This is a reference answer intended to cover a frequently encountered issue. The scenario is just an example. See also: "How to explain Composer's error log?")

IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • 1
    I think [this one](https://stackoverflow.com/questions/48073506/how-to-explain-composers-error-log) is already good enough as reference. It's the one I try to use as a dupe target for all these "teh composer won't install!!1!" questions. – yivi Feb 25 '21 at 12:46
  • 1
    @yivi Thanks, I hadn't seen that one before. Cross-referencing between the two would definitely be useful, but I think there's value in having an additional guide focussing on this particular case. We're currently getting a lot of questions specifically about upgrading to PHP 8.0, because it's a constraint people mostly don't think about until a major version comes out. I found a whole bunch of similar questions from when 7.0 came out which aren't currently marked as duplicate of anything, and some were never answered, so intended to point them all here. (I just haven't had time yet.) – IMSoP Feb 25 '21 at 12:54

4 Answers4

18

If you are using the PHP version 8, some of the plugins which are not yet supported can caused installation error.

composer install --ignore-platform-req=php or composer install --ignore-platform-reqs

This option can be used to set specific requirements that composer can ignore.

JMilanes
  • 221
  • 2
  • 6
  • 1
    This: `composer install --ignore-platform-req=php` save my day – Erich García Jun 22 '22 at 20:32
  • This finaly worked for me. – DANIEL Jul 20 '23 at 06:43
  • It's important to note that this will only work if the package is actually compatible with the new PHP version - it doesn't *fix* any problems, it just says "I want to run this package on an untested PHP version, at my own risk". – IMSoP Aug 18 '23 at 08:56
12

The Problem

As well as the versions of other packages they require, Composer packages can specify the versions of PHP which they support.

While resolving the versions of packages to install, Composer has to find versions which match all the constraints in place:

  • The version constraints you've specified for dependencies in your composer.json
  • The version constraints each package has specified for its dependencies
  • The PHP versions each package supports

If there is no package that satisfies all of those constraints, you will get an error.

Common Confusions

Note that the version constraint for PHP version follows the same rules as other composer constraints. So a constraint of ^7.0 means "any version of 7.x above 7.0", and does not include 8.0.

The Solution

To solve the problem, you need to relax one of those constraints:

  1. Look at the package mentioned in the error message (acme/some-package in the example) and find it on Packagist (or whatever custom package source you have configured).
  2. See if a newer version exists that supports your PHP version.
  3. If it doesn't, you'll need to find out what's needed to add that support. This might mean checking out the project directly, running its tests, and submitting a patch to mark it as compatible with the new version.
  4. If (when) support has been added, you'll need to make sure your composer.json, and other packages you depend on, don't exclude that new version. For instance, if you currently depend on acme/some-package version ^1.0, but PHP 8.0 is only supported from version 2.2.0 onwards, you'll need to change your constraint to ^2.2, and make sure your application is still compatible.

Temporary Workaround

Sometimes, you're pretty sure your application will run fine with the same versions of packages as you were previously using. In that case, you can use the platform configuration variable in your composer.json to pretend you're still using the old version. This should only be done as a temporary workaround or for testing as it means packages will be installed which may be completely broken on your new PHP version.

For example:

{
    "config": {
        "platform": {
             "php": "7.4.999"
        }
    }
}

See also "Override PHP base dependency in composer"

IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • I encounter this exact issue, but in my understanding the issue is that composer is run by default by a newer version of php (8). If I do `php7.4 $(which composer)`, it works great. I just wish the composer script was able to automatically run with the same version as in composer.json (if any). I thought config>platform>php was meant to do that, but apparently not. – sylbru Oct 01 '21 at 13:44
  • @Niavlys The composer script is entirely written in PHP, so by the time it's parsed your configuration, it's too late to choose a different PHP executable. There is also no standard name or location for different versions of PHP installed side by side - _you_ know that you have a command available called "php7.4", but Composer just runs `/usr/bin/env php` which picks up whatever "php" means in your current environment. If you want to change _that_, look up documentation for how your OS (Ubuntu / CentOS / etc) manages that symlink. – IMSoP Oct 01 '21 at 13:51
  • @Niavlys All that said, the platform configuration **should** tell Composer to ignore the currently running version of PHP, and install packages appropriate for the configured version. – IMSoP Oct 01 '21 at 13:53
  • Yep that’s what I was thinking, it can’t guess where my php executables are. I’ll take a look at `env`, maybe I can come up with a clever way to fix this. But thanks, good to know that the platform configuration should work, not sure why it doesn’t in my case… – sylbru Oct 01 '21 at 14:30
  • I just fixed my issue with a simple custom shell script called instead of `composer.phar` when doing `composer`. For now, it just checks whether I am in the main PHP 7.4 project I’m working on, and runs composer.phar with 7.4 in that case, with PHP 8 otherwise. I guess it could be generalized by making it read composer.json to know the desired PHP version, but the location of the PHP executables is likely going to stay an issue. For my personal usage though, it’s perfect! – sylbru Oct 01 '21 at 15:23
6

As an additional hint: if you want to check what you can do to make your project compatible without running composer update, Composer provides the command why-not. You can not only run it with packages and their versions: composer why-not vendor/package 2.0 will list all other package versions that block installing v2.0 of vendor/package.

This also works with PHP itself: composer why-not php 8.0 will tell you which packages block the usage of a later PHP version

Nico Haase
  • 11,420
  • 35
  • 43
  • 69
  • 1
    `why-not` is a lifesaver. Production code went live but the versions from staging to prod are slightly different and we couldn't figure out which package was throwing the php version error. This narrowed it down real fast. Thanks! – n0nag0n Jul 19 '21 at 22:55
0

Update PHP version in the composer.json manually and then run the following:

composer update --with-all-dependencies

Documentation says:

Update also dependencies of packages in the argument list, including those which are root requirements.

azurecorn
  • 395
  • 5
  • 12