Open4

MacにPHPのインストールをする

KazukiMiyazatoKazukiMiyazato

phpenvを使ってPHPのバージョン管理をする。
phpenv公式の手順でインストールを進める。
https://github.com/phpenv/phpenv

git clone git@github.com:phpenv/phpenv.git ~/.phpenv
echo 'export PATH="$HOME/.phpenv/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(phpenv init -)"' >> ~/.zshrc

phpbuildを追加して、 phpenv install を使えるようにする

git clone https://github.com/php-build/php-build.git
mv php-build $HOME/.phpenv/plugins/php-build

8系の最新版をインストールする

% phpenv install 8.1.9
[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 8.1.9 into /Users/kazuki.miyazato/.phpenv/versions/8.1.9
[Downloading]: https://www.php.net/distributions/php-8.1.9.tar.bz2
[Preparing]: /var/tmp/php-build/source/8.1.9

-----------------
|  BUILD ERROR  |
-----------------

Here are the last 10 lines from the log:

-----------------------------------------
grep: invalid option -- P
usage: grep [-abcdDEFGHhIiJLlMmnOopqRSsUVvwXxZz] [-A num] [-B num] [-C[num]]
	[-e pattern] [-f file] [--binary-files=value] [--color=when]
	[--context[=num]] [--directories=action] [--label] [--line-buffered]
	[--null] [pattern] [file ...]
configure: WARNING: unrecognized options: --with-png-dir, --with-icu-dir
configure: error: Please reinstall the BZip2 distribution
-----------------------------------------

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

ビルドエラーだそうで。

grep: invalid option -- P と怒られており、 BSD grepには無いオプションを指定している様子。
GNU grepをインストールして、grepにaliasを登録する。

brew install grep

.zshrcに以下を記述。

export PATH="/usr/local/opt/grep/libexec/gnubin:$PATH"
alias grep='/usr/local/opt/grep/libexec/gnubin/grep'
KazukiMiyazatoKazukiMiyazato

再度 phpenv install を実行

phpenv install 8.1.9
[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 8.1.9 into /Users/kazuki.miyazato/.phpenv/versions/8.1.9
[Skipping]: Already downloaded and extracted https://www.php.net/distributions/php-8.1.9.tar.bz2
[Preparing]: /var/tmp/php-build/source/8.1.9

-----------------
|  BUILD ERROR  |
-----------------

Here are the last 10 lines from the log:

-----------------------------------------
configure: error: Please reinstall the BZip2 distribution
-----------------------------------------

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

configure: error: Please reinstall the BZip2 distribution BZip2が無いとのこと。
BZip2 をインストール

brew install bzip2

もろもろのライブラリをインストール

brew install tidy-html5 libiconv zlib libzip autoconf automake

オプションを付けて、再度 phpenv install を実行

rm -rf /var/tmp/php-build
PHP_BUILD_CONFIGURE_OPTS="--with-zlib-dir=$(brew --prefix zlib) --with-bz2=$(brew --prefix bzip2) --with-iconv=$(brew --prefix libiconv)" phpenv install 8.1.9

phpenv: /Users/kazuki.miyazato/.phpenv/versions/8.1.9 already exists
continue with installation? (y/N) y
[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 8.1.9 into /Users/kazuki.miyazato/.phpenv/versions/8.1.9
[Downloading]: https://www.php.net/distributions/php-8.1.9.tar.bz2
[Preparing]: /var/tmp/php-build/source/8.1.9
[Compiling]: /var/tmp/php-build/source/8.1.9
[xdebug]: Installing version 3.1.5
[Downloading]: http://xdebug.org/files/xdebug-3.1.5.tgz
[xdebug]: Compiling xdebug in /var/tmp/php-build/source/xdebug-3.1.5
[xdebug]: Installing xdebug configuration in /Users/kazuki.miyazato/.phpenv/versions/8.1.9/etc/conf.d/xdebug.ini
[xdebug]: Cleaning up.
Makefile:243: warning: overriding commands for target `test'
Makefile:136: warning: ignoring old commands for target `test'
[Info]: Enabling Opcache...
[Info]: Done
[Info]: The Log File is not empty, but the Build did not fail. Maybe just warnings got logged. You can review the log in /tmp/php-build.8.1.9.20221203000042.log or rebuild with '--verbose' option
[Success]: Built 8.1.9 successfully.

無事ビルドできました 🎉

バージョンを指定をする

phpenv global 8.1.9

php -v
PHP 8.1.9 (cli) (built: Dec  3 2022 00:03:18) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.9, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.9, Copyright (c), by Zend Technologies
    with Xdebug v3.1.5, Copyright (c) 2002-2022, by Derick Rethans

ちゃんとインストールできていそうです。
疲れましたね。

KazukiMiyazatoKazukiMiyazato

postgresのモジュールが入ってなかった。

$ php -m | grep 'pdo'
pdo_mysql
pdo_sqlite

訂正:postgresのモジュールはpgsqlと表示されるらしいため、pdoでgrepしても確認はできない

KazukiMiyazatoKazukiMiyazato
$ brew install postgresql
$ where psql
/usr/local/bin/psql
$ readlink -f /usr/local/bin/psql
/usr/local/Cellar/postgresql@14/14.6/bin/psql

再インストール

PHP_BUILD_CONFIGURE_OPTS="--with-pgsql=$(brew --prefix postgresql) --with-zlib-dir=$(brew --prefix zlib) --with-bz2=$(brew --prefix bzip2) --with-iconv=$(brew --prefix libiconv)" phpenv install 8.1.9