Is there a difference when running Composer in different versions of PHP? - 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.

Related

Does Laravel 8 require PHP 8.1?

So.. I developed a project with Laravel 9, then I had to upload it to my client's server by FTP ( which was slow and painful ) to find out only afterwards that my client's server PHP version could not go over 8.0. I tried to open the project live link ( to where I uploaded ) and the composer platform check was telling me my project had dependencies on PHP 8.1 and that my version is 8.0.
So I tried tweaking the platform check php file to disable this check, to see if it would work anyways but no, the project was throwing errors.
So I decided to downgrade to laravel 8, because after searching around I read that laravel 8 did not need php 8.1.
I guess I read some wrong information because after tweaking my project to downgrade to laravel 8 and uploading again (painfully by ftp), the platform check was again telling me that my project needed PHP 8.1.
So I disabled again this platform check by editing/tweaking the platform check php file, to see if it would work anyway, and it did work. So all good. but then today I was learning how to check which composer packages had dependencies on a specific php version, and in the process I found out (if I'm not wrong) that laravel 8 has package dependencies that depend on PHP 8.1 ?
Is there a table somewhere I can check which Laravel versions depend on which PHP versions or do I have to run some commands on each project to check these dependencies?
Like in the images below:
Thanks !
Laravel 9 does not require PHP 8.1 it requires PHP 8.0.2
If this a shared project and someone else with PHP 8.1 generated the composer.lock file (or indeed you locally have PHP 8.1 but the server has 8.0) you might end up with packages that require PHP 8.1. Composer resolves and installs packages based on the locally installed PHP version.
You can override this behaviour and ensure everyone gets package deps based on the PHP version you expect to have on production if you use the platform config option in your composer.json e.g. add this to your composer.json
"config": {
"platform": {
"php": "8.0.2"
}
}
Then run
composer update
this should try to fix your package versions to ones that work with PHP 8
Short response no, Laravel 8 requires PHP >= 7.3
From Server Requirements
But, since you downgraded it is possible that some php packages require newer php versions no matter the version chosen for Laravel.
Some hints:
Change dependencies (packages) version, probably downgrade them.
Delete the vendor folder.
Delete composer.lock
Run composer install
Checking your screenshot: with symfony the downgrade will not break anything.
But, check the other package/s ie: tojsverkoyen/css-to-inline-styles requirements.

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.

Composer reading wrong PHP version (ACQUIA DEV DESKTOP on WINDOWS)

I am trying to install a library https://github.com/thephpleague/csv via composer but getting following errors i.e
'This package requires php >= 5.5.9 but your php version <5.4.45> does not satisfy that requirement.'
My current PHP version is 5.6.30 but for some reason composer is showing <5.4.45> which is strange.
I am using ACQUIA DEV DESKTOP on WINDOWS and can switch between PHP versions easily and can check PHP version either by a) dpm(phpinfo()); or b) drush php which is showing same version as set in ACQUIA DEV DESKTOP.
This leads to an issue linked with the composer as it's reading a wrong PHP version.
Steps I have followed to install this library i.e
Copied composer.phar file from 'DevDesktop/tools' folder onto my Drupal project root and then via drush used 'composer require league/csv' which gives me this error ....
'This package requires php >= 5.5.9 but your php version <5.4.45> does not satisfy that requirement.'
'drush php' gives me this... 'Psy Shell v0.8.3 by Justin Jileman'
I have checked online resources for the similar errors but none of the solution out there matches my situation hence posting this question.
Not sure if this is linked to the issue but I have noticed 'DevDesktop\tools\drush.bat' have first line i.e
IF "%PHP_ID%"=="" (SET PHP_ID=php5_5)
which I changed to
IF "%PHP_ID%"=="" (SET PHP_ID=php5_6)
Stack stop/start and tried to download 'composer require lease/csv' but no change (same results).
Thanks in advance.
Found a solution for this error and wanted to share so as it helps anyone else having same problem and possibly improve the solution as it seems to be a quick fix while it should be a universal fix :)
If you install Acquia DevDesktop software, composer comes with this by default.
From Acquia DevDesktop software, we can easily switch between php versions and can also set a default php version but that was not helpful when I try to download library via composer which kept on reading version 5.4.
As Acquia DevDesktop supports different PHP versions i.e 5.3, 5.4, 5.5, 5.6, 7. By default composer was reading php version 5.4 and I still need to find the file which needs to be modified so as default php version can be changed for composer.
In my case what I did was...
Go to Program Files > DevDesktop > tools folder.
There are 4 files having different extensions but same name i.e 'composer'. Remove these all files will remove composer from your system.
Download composer.exe from https://getcomposer.org/ and give it a target folder i.e 'Program Files > DevDesktop > php5_6' and install.
Once installed, now go to your project and by using command prompt or drush console, type 'composer require league/csv' which should download all files in your projects 'vendor' folder.

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.

Composer: Override Transitive Dependency on PHP Version

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?

Categories