Miguel Stevens
Miguel Stevens

Reputation: 9230

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.

Upvotes: 35

Views: 162210

Answers (12)

McRui
McRui

Reputation: 1955

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.

Upvotes: 1

Imran Zahoor
Imran Zahoor

Reputation: 2797

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

Upvotes: 0

brazilsunshine
brazilsunshine

Reputation: 21

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

Hope that helps.

Upvotes: 0

Myo Win
Myo Win

Reputation: 533

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

Upvotes: 3

MURTUZA BORIWALA
MURTUZA BORIWALA

Reputation: 555

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

Upvotes: 0

Muhammad Mubashir
Muhammad Mubashir

Reputation: 174

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

Upvotes: 1

agm1984
agm1984

Reputation: 17178

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

Upvotes: 0

rwilson
rwilson

Reputation: 199

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'

Upvotes: 4

gavintfn
gavintfn

Reputation: 529

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.

Upvotes: 44

bfuzze
bfuzze

Reputation: 500

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.

Upvotes: 2

gd_silva
gd_silva

Reputation: 1025

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.

Upvotes: 37

MSD
MSD

Reputation: 349

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

Upvotes: 1

Related Questions