Laravel Composer 看到错误的 PHP 版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46785969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:15:21  来源:igfitidea点击:

Laravel Composer sees wrong PHP Version

phplaravelcomposer-php

提问by Miguel Stevens

I'm trying to install an older Laravel Project.

我正在尝试安装较旧的 Laravel 项目。

When I run composer install I get the following error

当我运行 composer install 时出现以下错误

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

这是我的 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?

这个项目怎么可能认为我正在运行 php 5.6?

Thank you.

谢谢你。

回答by gd.silva

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.lockfile and writing your actual PHP version in platform > phpin the JSON object.

我也遇到过这个问题。如果您不想更新所有的 composer 包,您可以通过手动更改composer.lock文件并platform > php在 JSON 对象中写入您的实际 PHP 版本来解决此问题。

Example

例子

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

Although it works, the most recommended way to do this would be deleting your composer.lockfile, changing the platform > phpversion in composer.jsonand then executing composer install.

虽然它有效,但最推荐的方法是删除您的composer.lock文件,更改platform > php版本composer.json,然后执行composer install.

回答by gavintfn

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.

附加信息和对@nicohase 的回应,Nico,当您声明 composer 没有使用与 apache 相同的 php 可执行文件时,您是正确的。为什么作曲家要确保 php-cli 满足其他所需包的要求?它不会也不会。用户使用 php-cli 管理 composer,这本质上意味着它们是兼容的。Composer 正在检查以确保在网络服务器上运行的 php 版本与其他软件包兼容。

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.

现在,至于为什么,我列出的方法和另一篇文章建议的方法都是可能的解决方案。Composer 缓存有关系统、php 和安装包的信息,原因有两个:1. 连续性.. 2. 版本历史。如果 composer 在发生外部更改时修改了自己的缓存文件,则很难知道哪些软件包版本彼此兼容,以及何时兼容。

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 不会检查 php 版本,它会引用其缓存。Apache 可能会搜索任何对用户禁用的 php 版本的引用,它会在 Composer 的缓存文件中找到引用。我的建议建议出于这个原因删除缓存。此外,该

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.

此时,如果 php 最初是通过 yum/apt 安装的,然后通过 easy apache 升级,则 --ignore-platform-reqs 标志将绕过任何可能仍然存在的 rpm 排除功能,并允许安装或更新作曲家包。

回答by bfuzze

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.

以防将来对某人有帮助,我在尝试从 PHPStorm (2017.2) 内部运行 composer update 时遇到了这个问题。我尝试了上述建议,但没有一个奏效。我安装了多个版本的 PHP(5.6、7.0、7.1),都添加在 PHPStorm 设置下,所以我可以根据项目需求进行切换。无论选择的 CLI 解释器设置如何,在调用 Composer 时它始终会查找 PHP 7.0。在 PHPStorm 之外的终端中运行 Composer 可以正常工作(参考路径配置版本,7.1)。就我而言,这感觉就像一个 PHPStorm 错误。

回答by MSD

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

这是一个配置/环境问题。理想情况下,您可以使用多个 php 版本进行测试,在 apache 中,您可以像这样交换版本:

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

这里发生的事情是,当他运行 php -v 时,他正在运行配置为在 php7 中运行的 php-cli,但也许他的 apache 启用了 5.5。所以

sudo a2dismod php5.5
sudo a2enmod php7.0
sudo service apache2 restart

回答by rwilson

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:

在我的 HostGator 共享主机上,我能够通过在我想要使用的 php 版本的 .bashrc 文件中创建别名来克服这个问题:

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'

编辑 .bashrc 文件后记得要 source: 'source ~/.bashrc'