sh #使用Homebrew(MacOSX)时快速更改PHP版本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh #使用Homebrew(MacOSX)时快速更改PHP版本相关的知识,希望对你有一定的参考价值。
# Change PHP version rapidly when using Homebrew (MacOSX)
With Homebrew, sometimes you have many PHP versions installed.
I personally use EVERY php version to be sure every open-source project I have on my env works for every version.
This is why I have php 5.3, 5.4, 5.5, 5.6 and 7.0 installed with Homebrew.
Switching php versions is a pain when using homebrew, because you have to `brew unlink` the php versions used, and `brew link` the one you want.
Executing this script without argument shows all versions installed, and especially the current version.
Executing it with an argument will check a potential symlink in `/usr/local/bin/php{version}`
unlink all versions, and link the one you specified.
## Install
Execute this script to install `phpv`
```bash
sudo wget https://gist.githubusercontent.com/Pierstoval/1645a26ea5d2200642b6/raw/phpv.bash -O /usr/local/bin/phpv && sudo chmod +x /usr/local/bin/phpv && sudo chown `whoami` /usr/local/bin/phpv
```
## Usage:
* Show all versions installed:
```bash
$ phpv
Current:
PHP 5.5.27 (cli) (built: Jul 14 2015 17:04:01)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
with Xdebug v2.3.3, Copyright (c) 2002-2015, by Derick Rethans
PHP 7.0.0beta2 (cli) (built: Jul 24 2015 10:34:53)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v3.0.0-dev, Copyright (c) 1998-2015 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
PHP 5.6.11 (cli) (built: Jul 30 2015 17:06:05) (DEBUG)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
with Xdebug v2.3.3, Copyright (c) 2002-2015, by Derick Rethans
PHP 5.5.27 (cli) (built: Jul 14 2015 17:04:01)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
with Xdebug v2.3.3, Copyright (c) 2002-2015, by Derick Rethans
PHP 5.4.43 (cli) (built: Jul 14 2015 16:52:26)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
with Xdebug v2.3.3, Copyright (c) 2002-2015, by Derick Rethans
PHP 5.3.29 (cli) (built: Jul 14 2015 16:39:57)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2014 Zend Technologies
with Xdebug v2.2.7, Copyright (c) 2002-2015, by Derick Rethans
```
* Switch to another version:
```bash
$ phpv 54
phpv
Switch to /usr/local/bin/php54
Unlinking /usr/local/Cellar/php53/5.3.29_2... 0 symlinks removed
Unlinking /usr/local/Cellar/php54/5.4.43_2... 0 symlinks removed
Unlinking /usr/local/Cellar/php55/5.5.27_2... 0 symlinks removed
Unlinking /usr/local/Cellar/php56/5.6.11_2... 38 symlinks removed
Unlinking /usr/local/Cellar/php70/7.0.0-beta.2... 0 symlinks removed
Linking /usr/local/Cellar/php54/5.4.43_2... 40 symlinks created
PHP 5.4.43 (cli) (built: Jul 14 2015 16:52:26)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
with Xdebug v2.3.3, Copyright (c) 2002-2015, by Derick Rethans
```
Plus, for convenience, it's awesome to have a symlink for every PHP version pointing to `/usr/local/opt/php/{version}/bin/php`.
But you're lucky: the script can generate them automatically if it detects that a specific version of PHP is installed and if the symlink is not created.
Else, you can do it yourself by executing this script:
```bash
sudo ln -s /usr/local/opt/php53/bin/php /usr/local/bin/php53 && sudo chmod +x /usr/local/bin/php53
sudo ln -s /usr/local/opt/php54/bin/php /usr/local/bin/php54 && sudo chmod +x /usr/local/bin/php54
sudo ln -s /usr/local/opt/php55/bin/php /usr/local/bin/php55 && sudo chmod +x /usr/local/bin/php55
sudo ln -s /usr/local/opt/php56/bin/php /usr/local/bin/php56 && sudo chmod +x /usr/local/bin/php56
sudo ln -s /usr/local/opt/php70/bin/php /usr/local/bin/php70 && sudo chmod +x /usr/local/bin/php70
```
Then, you can execute any of the php version directly in command line by executing the so-called version:
```bash
$ php53 -v
PHP 5.3.29 (cli) (built: Jul 14 2015 16:39:57)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2014 Zend Technologies
with Xdebug v2.2.7, Copyright (c) 2002-2015, by Derick Rethans
```
And you can do the same for every version.
#!/bin/bash
# Use:
# $ phpv
# > Will return current global php version, and ALL php versions symlinked
#
# $ phpv {version}
# > Will "brew unlink" all versions, and "brew link php{version}"
# View README file for more info
if [[ ! -f "/usr/local/bin/php53" && -f "/usr/local/opt/php53/bin/php" ]]; then
echo "Creating symlink for php 5.3"
sudo ln -s /usr/local/opt/php53/bin/php /usr/local/bin/php53 && sudo chmod +x /usr/local/bin/php53
fi
if [[ ! -f "/usr/local/bin/php54" && -f "/usr/local/opt/php54/bin/php" ]]; then
echo "Creating symlink for php 5.4"
sudo ln -s /usr/local/opt/php54/bin/php /usr/local/bin/php54 && sudo chmod +x /usr/local/bin/php54
fi
if [[ ! -f "/usr/local/bin/php55" && -f "/usr/local/opt/php55/bin/php" ]]; then
echo "Creating symlink for php 5.5"
sudo ln -s /usr/local/opt/php55/bin/php /usr/local/bin/php55 && sudo chmod +x /usr/local/bin/php55
fi
if [[ ! -f "/usr/local/bin/php56" && -f "/usr/local/opt/php56/bin/php" ]]; then
echo "Creating symlink for php 5.6"
sudo ln -s /usr/local/opt/php56/bin/php /usr/local/bin/php56 && sudo chmod +x /usr/local/bin/php56
fi
if [[ ! -f "/usr/local/bin/php70" && -f "/usr/local/opt/php70/bin/php" ]]; then
echo "Creating symlink for php 7.0"
sudo ln -s /usr/local/opt/php70/bin/php /usr/local/bin/php70 && sudo chmod +x /usr/local/bin/php70
fi
if [ -z "$1" ]; then
echo "Current:"
php -v
echo ""
if [[ -f "/usr/local/opt/php70/bin/php" ]]; then /usr/local/opt/php70/bin/php -v ; fi
if [[ -f "/usr/local/opt/php56/bin/php" ]]; then /usr/local/opt/php56/bin/php -v ; fi
if [[ -f "/usr/local/opt/php55/bin/php" ]]; then /usr/local/opt/php55/bin/php -v ; fi
if [[ -f "/usr/local/opt/php54/bin/php" ]]; then /usr/local/opt/php54/bin/php -v ; fi
if [[ -f "/usr/local/opt/php53/bin/php" ]]; then /usr/local/opt/php53/bin/php -v ; fi
else
if [[ -f "/usr/local/opt/php$1/bin/php" ]]; then
echo "Switch to /usr/local/opt/php$1/bin/php"
if [[ -f "/usr/local/opt/php70/bin/php" ]]; then brew unlink php70 ; fi
if [[ -f "/usr/local/opt/php56/bin/php" ]]; then brew unlink php56 ; fi
if [[ -f "/usr/local/opt/php55/bin/php" ]]; then brew unlink php55 ; fi
if [[ -f "/usr/local/opt/php54/bin/php" ]]; then brew unlink php54 ; fi
if [[ -f "/usr/local/opt/php53/bin/php" ]]; then brew unlink php53 ; fi
brew link "php$1"
php -v
else
echo "Php version $1 does not exist."
fi
fi
以上是关于sh #使用Homebrew(MacOSX)时快速更改PHP版本的主要内容,如果未能解决你的问题,请参考以下文章
有人可以澄清 Mac OSX 安装中 cppcheck 中的 Homebrew 是啥吗
sh 使用Lua和Python通过Homebrew安装MacVim