Composer does not detect php7 instead it uses 5.6. How can I set the CLI to use php7

南笙酒味 提交于 2019-12-12 12:24:10

问题


Here when I execute php -v , it says it has php7


but when I try to execute composer update

the response it

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - This package requires php >=7.0.0 but your PHP version (5.6.33) does not satisfy that requirement.

How can I fix this? NOTE : I'm not allowed to uninstall previous version of php

Here is the composer.json

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=7.0.0",
        "fideloper/proxy": "~3.3",
        "intervention/image": "^2.4",
        "laravel/framework": "5.5.*",
        "laravel/passport": "^v1",
        "laravel/tinker": "~1.0"
    },
    "require-dev": {
        "filp/whoops": "~2.0",
        "fzaninotto/faker": "~1.4",
        "mockery/mockery": "0.9.*",
        "phpunit/phpunit": "~6.0"
    },
    "autoload": {
        "classmap": [
            "database/seeds",
            "database/factories"
        ],
        "psr-4": {
            "App\\": "app/"
        },
        "files": [
            "app/Helpers/misc.php"
        ]
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "extra": {
        "laravel": {
            "dont-discover": [
            ]
        }
    },
    "scripts": {
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate"
        ],
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover"
        ]
    },
    "config": {
        "preferred-install": "dist",
        "sort-packages": true,
        "optimize-autoloader": true
    }
}

I already tried

composer update --ignore-platform-reqs

but I still I get another error, which is again related to the above problem.


回答1:


As stated in the question you already have both the versions of PHP on your system, As Laravel uses the cli version you need to enable 7.X and disable 5.X.

You can achieve that by below commands

$ sudo a2dismod php5.6 // disable the loaded version
$ sudo a2enmod php7.0 // enable the desired version
$ sudo service apache2 restart // restart apache to get it in action

For More information You can install different version of PHP using the below commands

For Apache

$ sudo apt install php5.6   [PHP 5.6]
$ sudo apt install php7.0   [PHP 7.0]
$ sudo apt install php7.1   [PHP 7.1]

For Ngix

$ sudo apt install php5.6-fpm   [PHP 5.6]
$ sudo apt install php7.0-fpm   [PHP 7.0]
$ sudo apt install php7.1-fpm   [PHP 7.1]

To install any PHP modules, simply specify the PHP version and use the auto-completion functionality to view all modules as follows.

------------ press Tab key for auto-completion ------------ 
$ sudo apt install php5.6 
$ sudo apt install php7.0 
$ sudo apt install php7.1 

Now you can install most required PHP modules as per your requirements.

------------ Install PHP Modules ------------
$ sudo apt install php5.6-cli php5.6-xml php5.6-mysql 
$ sudo apt install php7.0-cli php7.0-xml php7.0-mysql 
$ sudo apt install php7.1-cli php7.1-xml php7.1-mysql 



回答2:


i had same issue and solved it by changing path under system variables.

  • right click on my computer and click properties
  • click advanced system settings on the left side menu
  • make sure the advance tap menu is selected
  • click on environment variables (buttom right)
  • click on path under system variables panel
  • click on edit
  • locate php installed path (eg. C:\wamp64\bin\php\php5.x.x)
  • click edit and rename to your disired php version (eg. C:\wamp64\bin\php\php7.3.5)
  • click ok to save



回答3:


Check where your composer executable is located (using which) and check the first line (shebang). I guess this still points to old php version.




回答4:


Install via this sudo apt install php7.1 php7.1-fpm and recheck again and post the result.




回答5:


If you want to run composer with a different PHP version, try calling it using php $(which composer) update. You can exchange the path to the PHP cli now freely




回答6:


I use phpbrew for multi PHP versions. In my case this command fixed the issue with composer using wrong PHP version:

phpbrew app get composer


来源:https://stackoverflow.com/questions/48535450/composer-does-not-detect-php7-instead-it-uses-5-6-how-can-i-set-the-cli-to-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!