MacのローカルPHPをPHP7にあげました

php-logo

みなさんこんにちは。
最近あまりPHPを使った案件がないのでPHPのバージョンがまだ5.6だったのでさすがになぁと思いPHP7にあげることにしました。
また、MacのローカルにPHPのバージョン管理環境がなかったため、それも整えることにしました。

anyenvをインストールする

anyenvという便利なものがあります。
これはphpenvやrbenv、pyenvなど、各言語のバージョンを管理するソフトウェアがあるのですが、各々インストールするには多少苦労がありますし、みんな作りは同じ感じだからそれらをまとめてインストール出来るソフトウェアを作ったよ!的なものだと認識してます。
つまりanyenvをインストールしておけばRubyもPHPもPythonもNodeも簡単にインストール出来るってことですね!

ということでコマンドラインでanyenvインストールします。

$ git clone https://github.com/riywo/anyenv ~/.anyenv
$ echo 'export PATH="$HOME/.anyenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(anyenv init -)"' >> ~/.bash_profile
$ exec $SHELL -l
$ anyenv
anyenv
Usage: anyenv  []

Some useful anyenv commands are:
   commands            List all available anyenv commands
   local               Show the local application-specific Any version
   global              Show the global Any version
   install             Install a **env
   uninstall           Uninstall a specific **anv
   version             Show the current Any version and its origin
   versions            List all Any versions available to **env

See `anyenv help ' for information on a specific command.
For full documentation, see: https://github.com/riywo/anyenv#readme

こういう風にhelpが出てきたらインストール成功してます。

phpenvのインストール

それではphpenvをインストールします。

$ anyenv install phpenv
$ exec $SHELL -l
$ phpenv
phpenv:
=======
   PHP multi-version installation and management utility.

Usage: phpenv  []

Some useful phpenv commands are:
   commands    List all available phpenv commands
   configure   Edit the current PHP's php.ini configuration file at location
   local       Set or show the local application-specific PHP version
   global      Set or show the global PHP version and update the Apache apxs
   shell       Set or show the shell-specific PHP version
   install     Install a PHP version using the php-build plugin
   uninstall   Uninstall a specific PHP version using the php-build plugin
   rehash      Rehash phpenv shims (run this after installing executables)
   version     Show the current PHP version and its origin
   versions    List all PHP versions available to phpenv
   which       Display the full path to an executable
   whence      List all PHP versions that contain the given executable

See `phpenv help ' for information on a specific command.
For full documentation, see: https://github.com/madumlao/phpenv#readme

phpenvと打ってヘルプページが出てきたらこちらもインストール完了です。

PHP7をインストール

それではphpenvを使ってPHP7をインストールしましょう。
phpenv install -lとするとインストール出来る一覧が出て来るので、私の場合は7.0.9が最新だったので7.0.9をインストールしました。

$ phpenv install 7.0.9
...(省略)
-----------------
|  BUILD ERROR  |
-----------------

Here are the last 10 lines from the log:

-----------------------------------------
configure: WARNING: unrecognized options: --with-mysql
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.
2016-11-14 21:54:11.913 xcodebuild[50450:7737446] [MT] PluginLoading: Required plug-in compatibility UUID DA4FDFD8-C509-4D8B-8B55-84A7B66AE701 for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/Unity4XC.xcplugin' not present in DVTPlugInCompatibilityUUIDs
2016-11-14 21:54:11.913 xcodebuild[50450:7737446] Failed to load plugin at: /Users/DAIKI/Library/Application Support/Developer/Shared/Xcode/Plug-ins/Unity4XC.xcplugin, skipping.  Reason for failure: *** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[0]
configure: error: mcrypt.h not found. Please reinstall libmcrypt.
-----------------------------------------

The full Log is available at '/tmp/php-build.7.0.9.20161114215156.log'.
[Warn]: Aborting build.

上記のようなエラーが出てしまいました。libmcryptというものが足りないっぽいのでbrewでインストールしてみます。

$ brew install libmcrypt

そしてもう一度インストール

$ phpenv install 7.0.9
...(省略)
-----------------
|  BUILD ERROR  |
-----------------

Here are the last 10 lines from the log:

-----------------------------------------
/var/tmp/php-build/source/7.0.9/ext/date/php_date.c:3757:5: note: use function 'llabs' instead
                                abs((utc_offset % 60)));
                                ^~~
                                llabs
6 warnings generated.
/var/tmp/php-build/source/7.0.9/ext/openssl/openssl.c:44:10: fatal error: 'openssl/evp.h' file not found
#include 
         ^
1 error generated.
make: *** [ext/openssl/openssl.lo] Error 1
-----------------------------------------

The full Log is available at '/tmp/php-build.7.0.9.20161114215630.log'.
[Warn]: Aborting build.

今度はOpenSSLの問題ぽいです。Mac OS X El Capitan上でphp-buildするメモという記事の中で解決方法が書いてあったのでそれを行います。OpenSSLとlibxml2のもともと入ってるバージョンとPHP7が求めているものがずれていることが問題のようです。
なので環境変数としてHomebrewでインストールしたものを指定し、それらを回避します。

$ brew install opensll
$ brew install libxml2
$ PHP_BUILD_CONFIGURE_OPTS="--with-openssl=$(brew --prefix openssl) --with-libxml-dir=$(brew --prefix libxml2)" PHP_BUILD_EXTRA_MAKE_ARGUMENTS=-j4 phpenv install 7.0.9
$ phpenv global 7.0.9
$ phpenv rehash
$ source ~/.bash_profile
$ php -v
PHP 7.0.9 (cli) (built: Nov 14 2016 22:23:02) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.9, Copyright (c) 1999-2016, by Zend Technologies
    with Xdebug v2.4.1, Copyright (c) 2002-2016, by Derick Rethans

これでPHP7がインストール出来ました。

最後に

いかがでしたか?
何個かエラーが出ましたが意外とすんなりインストールすることが出来ました。
あとこれでPHPのバージョン管理も出来るようになったのでバージョンに悩まされる日々から開放されました、やったー!