Mac / OSx - 更改在 bash 中调用的 PHP 版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13297965/
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
Mac / OSx - Change PHP version which is call in bash
提问by frosty22
I have a problem with PHP which is call through bash command "php":
我有一个通过 bash 命令“php”调用的 PHP 问题:
In bash:
在 bash 中:
php -v # PHP 5.3.17
/usr/bin/php # PHP 5.3.15
php -v # PHP 5.3.17
/usr/bin/php # PHP 5.3.15
I want secondary PHP version 5.3.15 as mainly, because in this version I have correctly installed extensions etc.
我主要想要次要的 PHP 版本 5.3.15,因为在这个版本中我已经正确安装了扩展等。
How can I set /usr/bin/phpas main -> I want when call "php -v" return my 5.3.15 version.
如何将/usr/bin/php 设置为 main -> 我想要调用“ php -v”时返回我的 5.3.15 版本。
Execuse my bad English, I hope that you understand.
执行我糟糕的英语,我希望你明白。
Thank you so much. I have Mac OSx Lion
非常感谢。我有 Mac OSx Lion
采纳答案by slash28cu
I would recommend you to install php through "Mac Ports".
我建议您通过“Mac Ports”安装 php。
You have multiple versions of php installed.
您安装了多个版本的 php。
With the cli command "which" you can:
使用 cli 命令“which”,您可以:
DESCRIPTION The which utility takes a list of command names and searches the path for each executable file that would be run had these commands actually been invoked.
描述 which 实用程序获取命令名称列表,并搜索实际调用这些命令时将运行的每个可执行文件的路径。
So read the man pages of the cli command "which".
因此,请阅读 cli 命令“which”的手册页。
DO this from your cli:
从您的 cli 执行此操作:
which php
哪个php
and then you will see where is located your php executable. What i would recommend you is to replace your php executable for a symbolic link to your /usr/bin/php.
然后您将看到您的 php 可执行文件的位置。我建议您将您的 php 可执行文件替换为指向您的 /usr/bin/php.ini 的符号链接。
回答by ioCron
Another way without changing the original php file / link is if you edit your .bashrc file as follows (works with other commands as well):
另一种不更改原始 php 文件/链接的方法是,按如下方式编辑 .bashrc 文件(也适用于其他命令):
Open your Terminal / Commandline and type in:
vim ~/.bashrc- Then press "i" to get into edit-mode (if there shows up a problem before that, because of a swap file, then press "E" to edit the file anyway)
Add the following line (change the path as needed):
alias php="/Applications/MAMP/bin/php/php5.4.10/bin/php"- Press "ESC" and type in ":wq"
If you need the php-alias to work right out of your current shell-session, then you have to use the following command (that's optional, so only if u r continuing to work in your current shell):
source ~/.bashrc
打开终端/命令行并输入:
vim ~/.bashrc- 然后按“i”进入编辑模式(如果在此之前出现问题,因为交换文件,然后按“E”编辑文件)
添加以下行(根据需要更改路径):
alias php="/Applications/MAMP/bin/php/php5.4.10/bin/php"- 按“ESC”并输入“:wq”
如果您需要 php-alias 在当前的 shell 会话中正常工作,那么您必须使用以下命令(这是可选的,因此只有当您继续在当前的 shell 中工作时):
source ~/.bashrc
That's it, no change in the /usr/bin folder needed, and u can export your bashrc file / settings to any other linux / bsd like system if needed without even touching your OS /usr/bin files. You can also add new aliases for different php versions, aliases like "php52", "php53", "php54", etc. for testing purposes or whatsoever.
就是这样,不需要更改 /usr/bin 文件夹,如果需要,您可以将您的 bashrc 文件/设置导出到任何其他类似 linux/bsd 的系统,甚至无需触及您的操作系统 /usr/bin 文件。您还可以为不同的 php 版本添加新别名,例如“php52”、“php53”、“php54”等别名,用于测试目的或其他任何目的。
Have fun, ioCron
玩得开心,ioCron
回答by Tien Lin
OS: OSX Yosemite
Just sort out a similar issue by vim ~/.bash_profile
操作系统:OSX Yosemite
只需通过以下方式解决类似的问题vim ~/.bash_profile
export PATH=/usr/local/bin:$PATH
export PATH=/Applications/MAMP/bin/php/php5.5.10/bin:$PATH
导出路径=/usr/local/bin:$PATH
导出路径=/Applications/MAMP/bin/php/php5.5.10/bin:$PATH
Make sure the order is correct. Put export PATH=/usr/local/bin:$PATHbefore export PATH=/Applications/MAMP/bin/php/php5.5.10/bin:$PATH
确保顺序正确。放在export PATH=/usr/local/bin:$PATH前面export PATH=/Applications/MAMP/bin/php/php5.5.10/bin:$PATH
/usr/local/bin contains the mac default php.
/usr/local/bin 包含 mac默认 php。
Hope this helps.
希望这可以帮助。
回答by Greg
For brew users, you might not need php55 if you already have 54 - but regardless you should probably run these commands
对于 brew 用户,如果您已经有 54 个,则可能不需要 php55 - 但无论如何您应该运行这些命令
$ brew install php54 php54-xdebug php54-mcrypt
you can substitute 54 with the latest number if you want. last i checked it was on 56
如果需要,您可以用最新的数字替换 54。最后我检查它是在 56
then based on some of the comments above you should run:
然后根据上面的一些评论,您应该运行:
$ php --version
it will probably display:
它可能会显示:
PHP 5.4.24 (cli) (built: Jan 19 2014 21:32:15)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
if it does, then change your .bash_profile like some of the other commenters my brew instance (i think its default) lives in
如果是这样,那么像我的 brew 实例(我认为它的默认值)所在的其他一些评论者一样更改您的 .bash_profile
/usr/local/Cellar
/usr/local/bin
as things get installed using brew, they get put in Cellar and linked through the bin. but in this case we need to override the php
当使用 brew 安装东西时,它们会被放入 Cellar 并通过 bin 链接。但在这种情况下,我们需要覆盖 php
$ sudo nano ~/.bash_profile
mine kinda looks like:
我的有点像:
PATH=$PATH:~/bin
PATH=$PATH:/usr/local/bin
PATH=$PATH:/usr/local/sbin
PATH=$PATH:/usr/local/Cellar/r/2.15.1/R.framework/Versions/Current/Resources/li$
export PATH
which means that it already has access to the /usr/local/bin - but i use the alias to specify by adding this line, near the bottom:
这意味着它已经可以访问 /usr/local/bin - 但我使用别名通过在底部附近添加这一行来指定:
alias php="/usr/local/bin/php"
save and close the terminal window, open up another one, and it should show something like:
保存并关闭终端窗口,打开另一个窗口,它应该显示如下内容:
$ php --version
PHP 5.4.27 (cli) (built: Apr 24 2014 17:16:35)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
with Xdebug v2.2.4, Copyright (c) 2002-2014, by Derick Rethans
回答by jeroen
You should look up the path and check all directories in the path for a symlink to the wrong php version.
您应该查找路径并检查路径中的所有目录是否有指向错误 php 版本的符号链接。
Then you can remove that symlink and replace it with a link to the correct version.
然后您可以删除该符号链接并将其替换为指向正确版本的链接。
You can also use findto see all instances:
您还可以使用find查看所有实例:
$ sudo find / -name "php"

