对一个命令使用不同的 PHP 版本 CLI 可执行文件

所以我安装了三个 PHP 版本的 Gentoo 盒子(没关系):

So I have Gentoo box with three PHP versions installed (nevermind the reasons):

  1. /usr/bin/php -> /usr/lib64/php5.4/bin/php
  2. /usr/bin/php5.5 -> /usr/lib64/php5.5/bin/php
  3. /usr/bin/php5.6 -> /usr/lib64/php5.4/bin/php
  1. /usr/bin/php -> /usr/lib64/php5.4/bin/php
  2. /usr/bin/php5.5 -> /usr/lib64/php5.5/bin/php
  3. /usr/bin/php5.6 -> /usr/lib64/php5.4/bin/php

我想使用 composer 安装 Laravel 框架:

I want to install Laravel framework using composer:

$ composer create-project laravel/laravel --prefer-dist

这会引发错误,因为 Laravel 需要 PHP > 5.5.9 并且默认的 php 解释器是 5.4.所以我发出另一个命令:

This however throws an error because Laravel requires PHP > 5.5.9 and the default php interpreter is 5.4. So I issue another command:

$ /usr/bin/php5.6 /usr/bin/composer create-project laravel/laravel --prefer-dist

这让我更进一步,但随后来自 Laravel 的 composer.json 的一些安装后命令开始发挥作用,导致安装崩溃.

This takes me one step further, but then some post-install commands from Laravel's composer.json comes into play, and installation crashes.

这是因为 composer.json 命令看起来像这样:

This is due to the fact, that composer.json commands look like this:

"post-install-cmd": [
    "php artisan clear-compiled",
    "php artisan optimize"
],

如您所见,默认"解释器又被使用了!

As you can see, the "default" interpreter is used again!

现在,正确的 PHP 文件以以下 shebang 开头:

Now, proper PHP files start with following shebang:

#!/usr/bin/env php

这是一个不错的功能,因为 PHP 解释器可以在不同系统的不同位置找到.不幸的是,在这种情况下 env 命令返回它在 $PATH 环境变量中找到的第一个可执行文件的路径.

This is nice feature as PHP interpreter can be found under different locations on different systems. Unfortunatelly, in this case env command returns path to the first executable it finds in $PATH environmental variable.

我怎么可能改变当前会话环境或执行什么样的技巧来执行整个 Laravel 安装过程 php 命令将调用 /usr/bin/php5.6 而不是 /usr/bin/php?

How could I possibly alter current session environment or what kind of trick to perform so for the execution of whole Laravel installation process php command would invoke /usr/bin/php5.6 instead of /usr/bin/php?

我不想更改 $PATH 变量或修改 composercomposer.json 或 Laravel 的 CLI 实用程序 等文件工匠.

I don't want to change $PATH variable or modify files like composer, composer.json or Laravel's CLI utility artisan.

还假设我想从普通用户帐户执行此操作(即没有 root 权限).

also assume that I want to do this from regular user account (i.e. with no root permissions).

推荐答案

也许你可以尝试修复环境!

Maybe you can try to fix the environnement!

$ php -v
PHP 5.4.x (cli) ...
$ set PATH="/usr/lib64/php5.6/bin:$PATH"
$ php -v
PHP 5.6.x (cli) ...

或者,如果您不想修改 shell 会话的 PATH,您可以仅针对当前命令进行更改:

Or, if you don't want to modify the PATH for your shell session, you can scope the change for the current command only:

$ php -v
PHP 5.4.x (cli) ...
$ env PATH="/usr/lib64/php5.6/bin:$PATH" php -v
PHP 5.6.x (cli) ...
$ php -v
PHP 5.4.x (cli) ...

相关文章