Composer: Override Transitive Dependency on PHP Version - php

I'm working on a project that uses guzzlehttp/guzzle.
While our production servers use PHP 5.4, our build box is still on 5.3, so when it runs a composer install, we see the following error:
guzzlehttp/guzzle 4.2.x-dev requires php >=5.4.0 -> no matching package found.
Is there any way to override this? I've requiring php 5.3 "as" 5.4.0, but it then complained that the package 'php' couldn't be found.
It seems that requiring a PHP version is a special case of a package and doesn't allow the same overrides provided for normal packages. Is there a way around this?

I don't know of a way around this.
The problem is: How should Composer know about the PHP that is supposed to execute the code in contrast to the PHP that is just grapping it's dependencies. Currently there is no way to override either PHP versions or installed extensions.
And where should this override be put? It's wrong to put it into the composer.json, because that would change the detected PHP version Composer assumes, and will lead to conflicts. It should probably be some sort of configuration of the local Composer instance that is being used to override the assumed PHP version.
I'd suggest upgrading your build box to 5.4 or install an additional version of PHP 5.4 that is only used for Composer.
How would you detect version conflicts on that build box during test execution if you do not use the PHP version that is supposed to run the code?

Related

Composer issues when running mPDF in PHP 7.4

I get the following error when running mPDF in my browser. I have just installed the latest version of mPDF on my cloud server running PHP 7.4. The requirements page for mPDF say I should be able to run this version with PHP 7.3+
I have tried to resolve the issue in composer.json but have no success. I am not able to install PHP 8.0 without upgrading the hosting which I do not want to do.
Any direction is appreciated.
Error message:
Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 8.0.0".
If you would like to learn why the php package is required (this may sound stupid, but think about it as a requirement with a specific version), you can make use of composer-depends(1), a.k.a. why?:
$ composer why php
...
composer/semver 3.2.6 requires php (^5.3.2 || ^7.0 || ^8.0)
doctrine/annotations 1.13.2 requires php (^7.1 || ^8.0)
...
It gives an alphabetical listing of packages in your configuration and which php version they require. This should help you to spot which package requires ^8.0.0 / >= 8.0.0 to get a better understanding.
When your target platform has PHP 7.4, you can configure your project to always depend on that version. This works by specifying the platform php version as a configuration option.
Get the exact php version on your target platform, e.g. 7.4.30 (Aug 2022).
Note: PHP 7.4.x is going to end of life this year[1], consider upgrading the hosting within the next two months to 8.0 at least (if changing hosting for the PHP version is hard, consider to already go to 8.1, test your project with each version first thought, best done locally).
Then within your project configure that version as the projects target platform version:
$ composer config platform.php 7.4.30 # (1.)
# (no output)
1. replace the exemplary version 7.4.30 with your target platform version
After changing the Composer project configuration this way, you need to update the projects dependencies:
$ composer update
# ...
Now Composer will no longer infer the available version of the php platform package (the PHP version) from the environment but instead take the configured platform version. Your local PHP version is not interfering any longer to resolve the installable set of packages. Instead, the configured platform PHP version is used for the resolution.
This effectively prevents that you pull in packages that require a higher PHP version.
Commit all changes to the configuration (composer.json) and pinned versions (composer.lock) and use those to do the packaging for the deployment to the target platform.

Package kofoworola/laravel-subdomain has requirements incompatible with Php 8.1.2 how to update this [duplicate]

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?")
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:
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).
See if a newer version exists that supports your PHP version.
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.
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"
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.
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

Reference - Composer error "Your PHP version does not satisfy requirements" after upgrading PHP

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?")
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:
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).
See if a newer version exists that supports your PHP version.
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.
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"
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.
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

Skip composer PHP requirement

We are using PHPCI and composer. The server which runs PHPCI is on PHP 5.3.
For a project we added the Facebook PHP SDK, using composer. It requires PHP 5.4.
Composer gets triggered by PHPCI and get executed. But because the CI server just got PHP 5.3 composer failed with the error message:
facebook/php-sdk-v4 4.0.9 requires php >=5.4.0 -> no matching package found.
This let fail my build in PHPCI, of course.
Is there a possibility to skip this requirement? Maybe by adding an option to composer.json? Or a parameter to composer.phar call?
I've found the option:
composer install --ignore-platform-reqs
Ignore platform requirements (php & ext- packages).
Alternative: Specify your projects' PHP version
You can skip the platform checks by configuring composer.json#/config/platform/php with the PHP version to use.
Composer will fetch packages based on that configured PHP version then.
So when you need to tell Composer the PHP version for your projects dependencies, you can (and should) specify the PHP version if different than the PHP version you execute composer with in your composer.json project configuration file (AKA root package):
{
"config": {
"platform": {
"php": "5.6.6"
}
}
}
Here PHP 5.6.6 version is exemplary, it could be 8.0.4 or any other PHP version.
This also documents the target (platform) PHP configuration. Additionally installed PHP extensions and library versions can be specified.
Compare: Config: platform - Composer documentation
For many commands, you can tell composer to bypass php version check, with parameter "--ignore-platform-reqs":
composer COMMAND --ignore-platform-reqs
this will bypass php version specification.
Be aware that the software may work or not: php version specification is there because somewhere in the code is needed at least the specified php version, so if you use that code the software will break.
If anything requires a specific version of PHP, it won't run in a lower version of PHP. You will properbly still recieve errors when bypassing the PHP requirement.
Btw, PHP 5.3 is no longer maintained, I would strongly recommend updating the PHPCI server.

Is there a difference when running Composer in different versions of PHP?

I have a Vagrant machine with the actual PHP version I want my server to run on, but locally I use a newer version of PHP.
Updating Composer locally just saves so much time as opposed to updating it on the virtual machine via SSH.
So my question is: Does it affect the vendor files when composer install or update is called from different versions of PHP?
The PHP version used when updating the dependencies affects the packages being used. Packages can define a requirement on a certain PHP version.
A common requirement found is requiring PHP 5.4 or 5.5 when a package is using the features of said versions, or PHP 5.3.3 or PHP 5.3.27 because the package needs certain bugfixes.
Composer will complain about not being able to execute composer install if the PHP version used when running this command is not able to fulfill all PHP version requirements that are mentioned in the lock file.
Running composer update with an older PHP version than composer install probably will work in most cases. Using the same PHP version should be the recommended setting, though.
Also: Using the same required extensions in all PHP versions also is needed for requirements checking.
Note that there is some demand for Composer to assume a given PHP version or extension is present on the target platform even if the command line PHP running the Composer command does not fulfill them, but this feature is not implemented yet. So there is no way to override the local PHP version with the one present on the target environment.

Categories