PHPのバージョン違いによる挙動を確認する必要があったので、複数のPHPバージョンを管理するphpenvを導入しました。
導入までにつまづく箇所が多々あるので、その手順と使い方について紹介します。
環境
- macOS High Sierra 10.13.6)
導入手順
phpenvをインストール
次のコマンドでphpenv
をインストールします。
$ git clone https://github.com/CHH/phpenv.git
$ phpenv/bin/phpenv-install.sh
:
Success.
export PATH="/Users/yusuke/.phpenv/bin:$PATH"
eval "$(phpenv init -)"
Add above line at the end of your ~/.bashrc and restart your shell to use phpenv.
パスを通すため.bashrc
に追記します。
$ vim ~/.bashrc
# 下記2行を追記
PATH="/Users/yusuke/.phpenv/bin:$PATH"
eval "$(phpenv init -)"
$ source .bash_profile
php-buildをインストール
次のコマンドでphp-build
をインストールします。
$ git clone https://github.com/CHH/php-build.git $HOME/.phpenv/plugins/php-build
これで準備が整いました。
インストール可能なバージョンを確認する
下記コマンドでインストール可能なバージョンを確認しましょう。
$ phpenv install --list
Available versions:
5.2.17
5.3.10
5.3.11
:
:
7.2.7
7.2.8
7.2.9
7.2snapshot
7.3.0alpha1
7.3.0alpha2
7.3.0alpha3
7.3.0alpha4
7.3.0beta1
7.3.0beta2
7.3snapshot
master
利用するバージョンをインストール
今回、php7.1.9をインストールしたいので次のようにコマンドを実行します。
$ phpenv install 7.1.9
[Info]: Appending build output to /tmp/php-build.7.1.9.20180822215100.log
[Info]: Loaded extension plugin
[Info]: Loaded apc Plugin.
[Info]: Loaded composer Plugin.
[Info]: Loaded github Plugin.
[Info]: Loaded uprofiler Plugin.
[Info]: Loaded xdebug Plugin.
[Info]: Loaded xhprof Plugin.
[Info]: Loaded zendopcache Plugin.
[Info]: php.ini-production gets used as php.ini
[Info]: Building 7.1.9 into /Users/yusuke/.phpenv/versions/7.1.9
[Downloading]: https://secure.php.net/distributions/php-7.1.9.tar.bz2
[Preparing]: /var/tmp/php-build/source/7.1.9
-----------------
| BUILD ERROR |
-----------------
Here are the last 10 lines from the log:
-----------------------------------------
configure: WARNING: This bison version is not supported for regeneration of the Zend/PHP parsers (found: 2.3, min: 204, excluded: ).
configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.
configure: error: Cannot find OpenSSL's #### evp.h>
-----------------------------------------
The full Log is available at '/tmp/php-build.7.1.9.20180822215100.log'.
[Warn]: Aborting build.
エラーが出ますね。。。
依存パッケージをインストールする
依存パッケージをインストールします。
re2c
$ brew install re2c
bision(バージョン2.7)
$ brew install bison@2.7
:
bison@2.7 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.
If you need to have bison@2.7 first in your PATH run:
echo 'export PATH="/usr/local/opt/bison@2.7/bin:$PATH"' >> ~/.bash_profile
For compilers to find bison@2.7 you may need to set:
export LDFLAGS="-L/usr/local/opt/bison@2.7/lib"
パスを通します。
$ echo 'export PATH="/usr/local/opt/bison@2.7/bin:$PATH"' >> ~/.bashrc
libxml2
$ brew install libxml2
:
If you need to have libxml2 first in your PATH run:
echo 'export PATH="/usr/local/opt/libxml2/bin:$PATH"' >> ~/.bash_profile
For compilers to find libxml2 you may need to set:
export LDFLAGS="-L/usr/local/opt/libxml2/lib"
export CPPFLAGS="-I/usr/local/opt/libxml2/include"
パスを通します。
$ echo 'export PATH="/usr/local/opt/libxml2/bin:$PATH"' >> ~/.bashrc
$ source .bash_profile
再チャレンジ
依存パッケージをインストールしたので再度インストールします。
$ phpenv install 7.1.9
[Info]: Appending build output to /tmp/php-build.7.1.9.20180822220815.log
[Info]: Loaded extension plugin
[Info]: Loaded apc Plugin.
[Info]: Loaded composer Plugin.
[Info]: Loaded github Plugin.
[Info]: Loaded uprofiler Plugin.
[Info]: Loaded xdebug Plugin.
[Info]: Loaded xhprof Plugin.
[Info]: Loaded zendopcache Plugin.
[Info]: php.ini-production gets used as php.ini
[Info]: Building 7.1.9 into /Users/yusuke/.phpenv/versions/7.1.9
[Skipping]: Already downloaded and extracted https://secure.php.net/distributions/php-7.1.9.tar.bz2
[Preparing]: /var/tmp/php-build/source/7.1.9
-----------------
| BUILD ERROR |
-----------------
Here are the last 10 lines from the log:
-----------------------------------------
configure: error: mcrypt.h not found. Please reinstall libmcrypt.
-----------------------------------------
The full Log is available at '/tmp/php-build.7.1.9.20180822220815.log'.
[Warn]: Aborting build.
また別のエラーが出ました。。。
mcrypt
mcrypt
をインストールします。
$ brew install mcrypt
再々チャレンジ
~ $ phpenv install 7.1.9
:
-----------------
| BUILD ERROR |
-----------------
Here are the last 10 lines from the log:
-----------------------------------------
Configuring for:
PHP Api Version: 20160303
Zend Module Api No: 20160303
Zend Extension Api No: 320160303
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.
-----------------------------------------
The full Log is available at '/tmp/php-build.7.1.9.20180822221107.log'.
[Warn]: Aborting build.
またエラーとなったので、必要なパッケージをインストールします。
autoconf・automake
$ brew install autoconf
$ brew install automake
再々々チャレンジ
$ phpenv install 7.1.9
phpenv: /Users/yusuke/.phpenv/versions/7.1.9 already exists
:
[Success]: Built 7.1.9 successfully.
ようやく成功です!試してみましょう。
使い方
インストール済みのバージョンを確認するには
$ phpenv versions
* system
7.0.9
7.1.9 (set by /Users/yusuke/.phpenv/version)
*
が先頭についているのが、現在適用されているバージョンです。
systemは元々MacにインストールされているPHPです。
phpのバージョンを切り替えるときは
$ phpenv global 7.1.9
$ php -v
PHP 7.1.9 (cli) (built: Aug 22 2018 22:39:12) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.1.9, Copyright (c) 1999-2017, by Zend Technologies
with Xdebug v2.6.1, Copyright (c) 2002-2018, by Derick Rethans
簡単ですね。
他にも便利な使い方があるので興味がある方はこちらをご覧ください。
phpenv/phpenv: Simple PHP version management - GitHub