简体   繁体   中英

Laravel Composer sees wrong PHP Version

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.

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 .

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.

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.

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'

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

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

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

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

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

I had the same issue, running valet isolate php@7.3 worked for me.

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM