2

So I want to upload my Laravel 8 project to a web hosting, but when I finished, there was an error message:

Fatal error: Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 8.1.0". You are running 8.0.7. in /storage/ssd4/678/18965678/laravel/vendor/composer/platform_check.php on line 24

After I checked the PHP version on the web hosting, it's only up to the 8.0 version.

I try to downgrade my PHP to 7.4 and 8.0 versions and re-upload to web hosting but the error is getting worse.

I think it is something to do with Composer (?) but I'm not sure what and how to solve it.

Does anyone have a suggestion for this? thank you very much.

*edited the completed error

Gregoire Ducharme
  • 1,095
  • 12
  • 24
Guzti Eka Putra
  • 47
  • 1
  • 10
  • Since the error complains that your PHP version is to low (it requires PHP 8.1.0 while you're using 8.0), so then where is the logic in _downgrading_ your PHP version? You should _upgrade_ it to _at least_ PHP 8.1.0 – M. Eriksson Jun 20 '22 at 08:22
  • Upgrading PHP is indeed the obvious thing to do. Is there a reason you cannot? – KIKO Software Jun 20 '22 at 08:24
  • my PHP version is 8.1.6 sir, but the web hosting only supports <8.0. I also don't know why the error looks like that. @M.Eriksson – Guzti Eka Putra Jun 20 '22 at 08:25
  • And remember that you shouldn't adapt your application after what your hosting supports, you should select a hosting after your applications needs. And I'm talking about upgrading PHP on the server, not locally. – M. Eriksson Jun 20 '22 at 08:25
  • Well, there is obviously a package with a `composer.json` specifying `"require": { "php": ^8.1.0 }`. – KIKO Software Jun 20 '22 at 08:29
  • @KIKOSoftware my PHP version is already 8.1.6 sir, I don't know why the error said that. What I know is the hosting I use only supports up to PHP 8.0 – Guzti Eka Putra Jun 20 '22 at 08:29
  • 2
    Well, last time I checked 8.0 < 8.1. What is there not to understand about this? PHP is not part of your project, it's part of the server. – KIKO Software Jun 20 '22 at 08:30
  • We're talking about upgrading PHP _on your server_ (which you said were running PHP 8.0), not on your local machine. – M. Eriksson Jun 20 '22 at 08:32
  • so the only solution is I try to use another hosting that supports PHP 8.1? @KIKOSoftware – Guzti Eka Putra Jun 20 '22 at 08:33
  • 1
    Yes, indeed. Or find out which package(s) require PHP >= 8.1 and see if you can downgrade it/them. That might be easy or difficult, I can't say. Most packages should still be able to run with PHP 7. This shows you it is important to run the same PHP version locally and on your server. – KIKO Software Jun 20 '22 at 08:34
  • Composer **tells** you that your **dependencies require PHP 8.1 but you have 8.0**. Your conclusion is *"something is wrong with Composer, I will downgrade PHP"*. How did you conclude that? Why aren't you reading the message and figuring things out? Like.. perhaps you could upgrade to PHP 8.1? Or you could lower the version requirements in your composer.json? Or you could run `composer install --ignore-platform-reqs` and force installation, then hope that it all works? StackOverflow driven development isn't the way to fix problems. – N.B. Jun 20 '22 at 10:45
  • 1
    I'd say this is a configuration issue. There are endless ways to solve it, but I tried in an answer to first plainly explain what it is about and how to solve. Additionally (and now updated), when the PHP version on the Server is different to the local one (and the local one appears incompatible), the Servers version is leading and should be configured in the project for the target platform. That can include Composer project configuration and I show how with a standard procedure in the (updated) answer (see _Locking for the Wrong Platform_). – hakre Jun 20 '22 at 11:02

2 Answers2

5

I think it is something to do with Composer (?) but I'm not sure what and how to solve it.

Yes, you're executing a Composer command, assumable composer install and you have a composer.lock file in your project.

What?

When Composer installs from the lock file (named composer.lock by default), it checks the platform so that you don't install incompatible dependencies unnoticed.

In case there is a deviation in the platform (PHP version, installed PHP extensions and the like) it gives notice:

Fatal error: Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 8.1.0". You are running 8.0.7. in /storage/ssd4/678/18965678/laravel/vendor/composer/platform_check.php on line 24

This basically means that the lock file references dependencies that - if they would be installed - would be incompatible with the PHP version Composer has detected on the system where composer install is running.

Compare with the error message: A PHP version equal or greater than 8.1.0 is required, but the detected PHP version is 8.0.7 which is below that version.

How to Solve?

The solution depends a bit on how they deal with both the pinned versions and the procurement and management of the platform.

Given the pinned versions in the lock file are leading, this requires the platform to match. This is often the case in a Composer based PHP project, so I suspect this is also their setup (the project ships with a defined dependency configuration in the composer.lock file).

Then the solution is relatively simple: As the PHP version is too low, the system where you execute composer install needs it upgraded to at least PHP version 8.1.0. Commonly you'll take the latest in the 8.1.x series.

After changing the system configuration you can run the Composer installation command again and Composers platform check should now pass successfully.

So much for the theory.

Locking for the Wrong Platform

My PHP version is already 8.1.6 sir, I don't know why the error said that. What I know is the hosting I use only supports up to PHP 8.0

The hosting is the system where you get the error. The my is the system where you create the lock file.

This now causes an issue. As you distribute the lock file, it is with the wrong platform. There are two ways to handle this, and both are about to align the PHP version between your system and the hosting system:

  1. Tell Composer which PHP version to use for the platform.
  2. Use the hosting PHP version on your system.

You can do 1., 2. and even both, they are not mutually exclusive.

Often most easy is 1. as it does not involve system configuration.

But 2. has more properties that can be beneficial for your PHP project development.

Tell Composer which PHP Version to use for the Platform

This is an option in your Composer project configuration. Configure the PHP platform version to the one of your hosting (8.0.7):

$ composer config platform.php 8.0.7

(the command does not produce any output when successful; see as well Tell Composer to use Different PHP Version)

Afterwards you have to update the project dependencies as Composer is now using that configured PHP version 8.0.7 to resolve the dependencies:

$ composer --no-plugins --no-ansi -n update --no-scripts
Loading composer repositories with package information
...
Updating dependencies
...
Generating autoload files

This updates the lock file while taking care that only dependency versions that are compatible with that PHP version are being required.

Now commit the lock file and publish the project with it.

$ composer validate --no-check-publish \
    && git add composer.json composer.lock \
    && git commit -m "configure, update and lock dependencies" \
    && git push hosting
...

Use the Hosting PHP Version on your System

You should normally develop the PHP project with the same PHP Version that you use on the system(s) where it is run. This helps to prevent issues when the PHP runtime configuration deviates, like a different PHP version as in your case.

Consult your teams development documentation on how to configure the PHP runtime on a development box. If there is no team and also the project documentation does not cover it, consult your system operations' or operating systems' manual if there is no professional system administration at hand you can consult.

This is normally done with the systems package manager. For example I have a couple of PHP versions installed on my system. Within each project development environment then, I specify which PHP version to use and develop with it. E.g. Composer runs with that PHP version.

hakre
  • 193,403
  • 52
  • 435
  • 836
0

add to composer.json > config "platform-check": false, and then run composer autoload-dump.