Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 902
Miguel Stevens Avatar asked Oct 17 '17 08:10

Miguel Stevens


3 Answers

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.

like image 75
gavintfn Avatar answered Oct 21 '22 11:10

gavintfn


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.

like image 34
gd_silva Avatar answered Oct 21 '22 11:10

gd_silva


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'

like image 3
rwilson Avatar answered Oct 21 '22 12:10

rwilson