Laravel Composer sees wrong PHP Version
Asked Answered
M

12

35

I'm trying to install an older Laravel Project.

When I run composer install I get the following error

This package requires php >=5.6.4 but your PHP version (5.5.35) does not satisfy that requirement.

When I run

php -v

I get the following result

PHP 7.1.10 (cli) (built: Oct 12 2017 14:00:12) ( ZTS )

This is the content of my composer.json

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=5.6.4",
        "doctrine/dbal": "^2.6",
        "guzzlehttp/guzzle": "^6.3",
        "intervention/image": "^2.4",
        "intervention/imagecache": "^2.3",
        "laravel/framework": "5.4.*",
        "laravel/tinker": "~1.0",
        "laravelcollective/html": "^5.4",
        "maatwebsite/excel": "^2.1",
        "sentry/sentry-laravel": "^0.8.0",
        "spatie/laravel-glide": "^3.2",
        "spatie/laravel-permission": "^2.6",
        "spatie/laravel-pjax": "^1.3"
    },
    "require-dev": {
        "fzaninotto/faker": "~1.4",
        "mockery/mockery": "0.9.*",
        "phpunit/phpunit": "~5.7"
    },
    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "scripts": {
        "post-root-package-install": [
            "php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "php artisan key:generate"
        ],
        "post-install-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postInstall",
            "php artisan optimize"
        ],
        "post-update-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postUpdate",
            "php artisan optimize"
        ]
    },
    "config": {
        "preferred-install": "dist",
        "sort-packages": true,
        "optimize-autoloader": true
    }
}

How is it possible that this project thinks I have php 5.6 running?

Thank you.

Marabou answered 17/10, 2017 at 8:41 Comment(10)
What environment are you using, Homestead? Try and remove your composer.lock file and run againRoye
You probably have more than one PHP version installed in your system and your web server is configured to use 5.5.35. Check your web server's config files.Spirituous
@JoshGriggs this is just Laravel Valet.Marabou
Don't run php -v that gives you a different loaded php module. Use phpinfo() to find the version configured on your serverTelesthesia
Genuinely curious, does composer / artisan not use the CLI version of PHP?Roye
how about php-fpm7.1 -v?Ettieettinger
that's a command not found. :)Marabou
Well guys this is weird, composer update did the trick, And I didn't get a warning about the lock file being outdated...Marabou
@Notflip What web server you're using?Yseult
Valet, from LaravelMarabou
D
37

I've had this problem too. If you don't want to update all your composer packages, you can solve this issue by manually changing the composer.lock file and writing your actual PHP version in platform > php in the JSON object.

Example

...
"platform": {
    "php": "7.1"
}
...

Although it works, the most recommended way to do this would be deleting your composer.lock file, changing the platform > php version in composer.json and then executing composer install.

Disputatious answered 17/10, 2017 at 8:59 Comment(4)
That' a good solution! Better than updating I guess. I'm always affraid something will break after an updateMarabou
This is good solution! Thanks for info gd.silva. @Notflip I think my answer is also works for you right?Civility
Yes, updating everything is not the best option in most situations.Disputatious
That's a very good solution. I just want to add that "platform" clause should be inside "config" clause.Mildew
L
44
composer clear-cache
composer self-update
composer update --ignore-platform-reqs
or
composer install --ignore-platform-reqs

additional information and response to @nicohase, Nico, you are correct when you state that composer is not using the same php executable as apache. Why would composer ensure that php-cli meets the requirements of the other required packages? It wouldn't and doesn't. The user is administering composer with php-cli, which inherently means that they are compatible. Composer is checking to ensure that the version of php that is running on the webserver and the other packages are compatible.

Now, as to why, both the method that I listed and the other post suggests, are both likely solutions. Composer caches information regarding the system, php and the packages that are installed for two reasons, 1. continuity.. 2. version history. If composer modified its own cache files when external changes occurred, it would be difficult to know which packages versions were compatible with each other, and when.

So, composer is not checking the php version when an update or install is occurring, it references its cache. Apache likely greps any references to php versions that are being disabled by the user, it would find a reference in composer's cache files. My suggestion recommends that the cache be deleted for that reason. Additionally, the

composer --self-update

tells composer to update itself, as opposed to the packages it manages ...

composer update

at that point if php had been initially installed by way of yum/apt, and then upgraded by easy apache, the --ignore-platform-reqs flag will circumvent any rpm exclude functionality that may still exist, and allow the install or update of the composer packages.

Lillie answered 12/11, 2018 at 8:27 Comment(3)
Can you add some explanation to this? Why do you think that this solves the problem?Lil
So why clear cache and update composer if you are going to circumvent it with "ignore platform reqs" anyway?Ballonet
composer install --ignore-platform-reqs is actual magic , thank you alot @LillieCarver
D
37

I've had this problem too. If you don't want to update all your composer packages, you can solve this issue by manually changing the composer.lock file and writing your actual PHP version in platform > php in the JSON object.

Example

...
"platform": {
    "php": "7.1"
}
...

Although it works, the most recommended way to do this would be deleting your composer.lock file, changing the platform > php version in composer.json and then executing composer install.

Disputatious answered 17/10, 2017 at 8:59 Comment(4)
That' a good solution! Better than updating I guess. I'm always affraid something will break after an updateMarabou
This is good solution! Thanks for info gd.silva. @Notflip I think my answer is also works for you right?Civility
Yes, updating everything is not the best option in most situations.Disputatious
That's a very good solution. I just want to add that "platform" clause should be inside "config" clause.Mildew
F
4

On my HostGator shared hosting, I was able to overcome this problem by creating Aliases in my .bashrc file for the php version I wanted to use:

alias php='/opt/php71/bin/php'
alias composer="/opt/php71/bin/php ~/bin/composer/composer.phar"

Remember to source after editing the .bashrc file: 'source ~/.bashrc'

Fromenty answered 24/2, 2019 at 8:22 Comment(1)
Had a similar issue with hosting that allowed switch of PHP version, solved with similar workaround: https://mcmap.net/q/428681/-how-to-install-laravel-on-a-server-running-different-php-versionsBoigie
A
3

For Laravel Valet users

If you are using Laravel Valet (https://laravel.com/docs/8.x/valet)

you need to run run

valet use php --force

and after that

composer global update
Avebury answered 23/12, 2021 at 4:47 Comment(0)
B
2

In case it helps someone in the future, I ran into this problem while trying to run composer update from inside PHPStorm (2017.2). I tried the above suggestions, but none ofthem worked. I have multiple versions of PHP installed (5.6, 7.0, 7.1) all added under PHPStorm settings, so I can switch based on project requirements. Regardless of selected CLI interpreter setting, it always looks to PHP 7.0 when calling composer. Running composer in a terminal outside of PHPStorm works without issue (references the path configured version, 7.1). In my case, this feels like a PHPStorm bug.

Beitris answered 6/6, 2018 at 13:39 Comment(1)
If anyone arrives here, my problem was that (on a mac) I switched to zsh shell on my Terminal app and on PhpStorm, but the system default was still set to bash. PhpStorm's composer plugin uses the system default shell instead of the one defined in the Preferences, which had an old PHP version on the PATH.Adoptive
I
1

this is a config/env issue. Ideally you can have multiple php versions to test with, in apache you can swap versions like this:

Example:
sudo a2dismod php5.6
sudo a2enmod php7.0
sudo service apache2 restart

Whats happening here is when he runs php -v he is running php-cli which is configured to run in php7, but perhaps his apache has 5.5 enabled. so

sudo a2dismod php5.5
sudo a2enmod php7.0
sudo service apache2 restart
Invagination answered 17/10, 2017 at 13:2 Comment(2)
Can you explain how changing the PHP version used by the webserver affects composer which does not use that version after all?Lil
in my case this helped me hehe, even if it is Out of topic :)Tessera
F
1

phpinfo() gives you the version of apache which is the actual version the project runs on and in case you want to change it simply follow these steps:

install php version that you wish to install:

sudo add-apt-repository ppa:ondrej/php -y
sudo apt-get update

//replace X with the version you want
sudo apt-get install php7.X-fpm php7.X-curl php7.X-mbstring php7.X-mysql -y

Now restart your apache:

sudo service apache2 restart

disable current php version(the one that phpinfo() gives you):

sudo a2dismod php7.2

And now enable php version that you just installed:

sudo a2enmod php7.X
Feverroot answered 25/4, 2021 at 20:8 Comment(0)
R
1

I've run into the same issue after upgrading Vagrant and Homestead which installed PHP 8.2.

After doing composer update the same error appeared, but it's simple to tweak and overcome this issue.

SSH into your project root and do php -v if it's different from what you have in your composer.json setting for PHP (in mine was PHP 8.2), simply run in the command line the PHP version required as below.

Example: your requirement is "php": "^8.1", then run php -v and check if you're running on the required PHP version, if not, run in the terminal (for this case) php81. This should set the PHP version for this specific project.

After the above, running composer update should work like charm with no issues.

Reuter answered 20/11, 2023 at 11:26 Comment(0)
D
0

I just had this problem running composer update from inside VS Code.

The problem was that VS Code's integrated terminal thought I was using PHP 7.1 while my MacOS system does also have PHP 8.0.

I tried running composer update from my other terminal, Hyper Terminal and it worked.

When I type:

$ /usr/bin/php -v

from inside VS Code's terminal, it shows 7.1.

When I type:

$ which php
/usr/local/bin/php

$ /usr/local/bin/php -v

It shows PHP 8.

The solution was to run composer update from HyperTerm not VS Code.

This comment helped a lot:

If anyone arrives here, my problem was that (on a mac) I switched to zsh shell on my Terminal app and on PhpStorm, but the system default was still set to bash. PhpStorm's composer plugin uses the system default shell instead of the one defined in the Preferences, which had an old PHP version on the PATH. – mjsarfatti

Dravidian answered 19/1, 2021 at 19:12 Comment(0)
H
0

So I am able to solve the problem by changing the version of PHP in my main 'config.php'. Before '$required_php_version = 7.2' and I had upgraded to 8.0.9 and so it was not taking the right version and so I changed it to '$required_php_version = 8.0.9'. Or here one can simply put greater than a particular version.

After that change the required version of php in 'composer.json' and 'composer.lock' file to reflect the same.

//detect enviroment
$required_php_version = '8.0.9';
$detect_compentent_list = array('mysqli_connect', 'mod_rewrite', 'ZipArchive', 'gd', 'curl', 'bcmath');
$detect_directory_list = array('upload', 'backup', 'application/config', 'application/logs', 'application/cache/ci_session', 'application/libraries');

I will keep that in mind for the future answers I post. Elikill58

Henriques answered 28/8, 2021 at 10:6 Comment(1)
Thanks for the answer, but try to don't show code or config as image, because the picture can expire and we can't copy/paste itTesch
G
0

I had the same issue, running valet isolate [email protected] worked for me.

Hope that helps.

Glenine answered 4/9, 2022 at 14:43 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewWhiny
D
0

This is the complete and correct solution.

Downgrade PHP-Cli version

sudo update-alternatives --config php

Displays output like below:

sudo update-alternatives --config php
There are 4 choices for the alternative php (providing /usr/bin/php).

  Selection    Path             Priority   Status
------------------------------------------------------------
* 0            /usr/bin/php8.1   81        auto mode
  1            /usr/bin/php5.6   56        manual mode
  2            /usr/bin/php7.2   72        manual mode
  3            /usr/bin/php7.4   74        manual mode
  4            /usr/bin/php8.1   81        manual mode

Press <enter> to keep the current choice[*], or type selection number: 3
update-alternatives: using /usr/bin/php7.4 to provide /usr/bin/php (php) in manual mode

Downgrade it for Apache

sudo a2dismod php8.1
sudo a2enmod php7.4
sudo systemctl restart apache2

Reference: link

Duvetyn answered 10/11, 2022 at 13:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.