Catalina(OS X 10.15.4)へ「phpenv」を導入する

2020年5月18日

Mac OSX(10.15.4)にphpenvを導入する

きっかけは,CakePHP4 の導入。

「いま,PHPの開発は,『Laravel』が主流!」

などという記事もみかけますが,むかし使い慣れた「CakePHP」から,新しいフレームワークを習得するのが面倒です。(「Laraval」も興味はあります。)

Rails」が流行りだしたころも,同じようなことがいわれていた気もします。
(しばらく見ない間に,Railsも5.1になっている・・・。)

いま,WEB開発では,どんな手法がトレンドなんでしょうね。


「CakePHP」のはなしに戻ります。

早速,公式マニュアルのクイックスタートガイドに沿って,導入しようとすると,2点エラーが出ました。
(しかし,公式サイトの日本語ドキュメントは,いつの間にこれほど充実していたのだろう。)


追記:
この時点では,2点のエラーを修正すれば,すんなりPHP7.4が導入できると思っていました。
しかし,2点どころではなかったです。

「phpenv」をOS X 10.15.4(Catalina)に導入するのは,かなり面倒です(時間がかかります)。


1) The requested package cakephp/bake ^4.0 exists as cakephp/bake [(略)] but these are rejected by your constraint.

The requested package cakephp/bake ^4.0 exists as cakephp/bake but these are rejected by your constraint.

原因がよく分からんので,早速グーグルで検索。

参考:How to Fix Cakephp 4 Composer Install Error

どうやら,インストール時に,CakePHPのバージョン指定を”4.0″にしていたのが悪かったようです。
すでに,「4.0 以上」のマイナーバージョンがリリースされているので,それが指定できるようにしなければならない・・・のかな?

失敗時のコマンドphp composer.phar create-project --prefer-dist cakephp/app:4.0 [プロジェクト名]

エラー回避のコマンドphp composer create-project --prefer-dist "cakephp/app:^4.0" [プロジェクト名]

こうすることで,まず1つめのエラーを回避できました。


2)the requested PHP extension intl is missing from your system.

いま,Mac OS Xに,デフォルトで入っているPHP(7.3.11) を使っているのですが,「ext-intl」がないようです(もしくは,有効になっていない?)。

こちらも,ググってみたところ,その多くが「Homebrew」でPHPを入れるという解決策でした。

でも,「せっかくOS標準のPHPから離れるのなら,最新版のPHPを入れたい」ので,PHP7.4をインストールしようと思います。

Homebrewでコマンドを叩いたところ,まだ用意されてないのか,見つかりません。

% brew search php
==> Formulae
brew-php-switcher   php-cs-fixer        phplint             phpstan
php                 php@7.2             phpmd               phpunit
php-code-sniffer    php@7.3             phpmyadmin
==> Casks
eclipse-php                netbeans-php               phpstorm

探し方が間違っているのかな?

brewでOSと同じバージョンのphp7.3を導入するのは,なんか微妙な気分です。

なら,phpenv はどうだろう?ということで,今回のタイトルに至ります。


phpenv の導入

まず,githubをみると,2つリポジトリが見つかります。

A)phpenv / phpenv

B)CHH / phpenv

「Star」や「Watch」の数でみると,(A)の方が優勢ですね。
どちらを使うべきか。

ここでも,ちょっとググってみます。

phpenvのリポジトリは、phpenv/phpenvCHH/phpenvの2種類あって、phpenv/phpenvのほうが如何にも公式って感じでこちらでインストールしようとしたのですが、こちらはバグが多すぎる上に、Issueに載っている解決法もバラバラで、さらにすべて試してもうまくいかないことがあったので、諦めました。そしてCHH/phpenvをインストールしたら何の問題もなくあっさり出来たので、こちらをインストールします。

参考:phpenvで最新版のPHPをインストールしてWebサイトで使用する

これを見ると,どうやら(B)の方がよいようですね。
(B)のリポジトリをクローンしてインストールします。

phpenv % bin/phpenv-install.sh

export PATH="/Users/(アカウント名)/.phpenv/bin:$PATH"
eval "$(phpenv init -)"

Add above line at the end of your ~/.bashrc and restart your shell to use phpenv.

「.bashrc」に書き込めと表示されますが,OS が Catalina になってから,ターミナルが Zsh になったらしいので,書き込む先は「.zshrc」でしょうか。

編集して,ターミナルを再起動します。

% phpenv 
rbenv 1.1.2-30-gc879cb0
Usage: rbenv <command> [<args>]

Some useful rbenv commands are:
   commands    List all available rbenv commands
   local       Set or show the local application-specific Ruby version
   global      Set or show the global Ruby version
   shell       Set or show the shell-specific Ruby version
   rehash      Rehash rbenv shims (run this after installing executables)
   version     Show the current Ruby version and its origin
   versions    List installed Ruby versions
   which       Display the full path to an executable
   whence      List all Ruby versions that contain the given executable

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

phpenv コマンドが通るようになりました。

使い方は rbenv と同じようですが(コマンドを叩いたら,いきなり「rbenv」と出ますね・・・),rbenv を使ったのもはるかむかしなので,覚えてません。

「php-build」のインストール

公式リポジトリのREADMEを参考に「PHP7」を導入。

まず,「php-build」が必要らしい。

こちらも,上のリンクにある,github からクローンする。

ホームディレクトリの .phpenv ディレクトリに,pluginsフォルダを作成する。
そこに,github からクローンした「php-build」ディレクトリをコピーする。

% cp -r php-build .phpenv/plugins 

すると,phpenv コマンドで install をパラメーターで与えることができる。

% phpenv install -l
Available versions:
  5.2.17
  5.3.10
(略)
  7.4.0
  7.4.1
  7.4.2
  7.4.3
  7.4.4
  7.4.5
  7.4.6
  7.4snapshot
  master

今回の目的の,php7系がみられました。


PHP7.4 のインストールを試行:失敗

早速,コマンドを叩いて,php7.4(現時点での最新版)をインストールします。

% phpenv install 7.4.6

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

Here are the last 10 lines from the log:

-----------------------------------------
configure: error: The pkg-config script could not be found or is too old.  Make sure it
is in your PATH or set the PKG_CONFIG environment variable to the full
path to pkg-config.

Alternatively, you may set the environment variables LIBXML_CFLAGS
and LIBXML_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

To get pkg-config, see <http://pkg-config.freedesktop.org/>.
See `config.log' for more details
-----------------------------------------

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

エラーで失敗。

こういったのが,精神的にきつい。

とりあえず,ログを参照。

% cat /tmp/php-build.7.4.6.20200518013139.log
configure: WARNING: unrecognized options: --with-png-dir, --with-icu-dir
configure: error: in `/var/tmp/php-build/source/7.4.6':
configure: error: The pkg-config script could not be found or is too old.  Make sure it
is in your PATH or set the PKG_CONFIG environment variable to the full
path to pkg-config.

Alternatively, you may set the environment variables LIBXML_CFLAGS
and LIBXML_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.

To get pkg-config, see <http://pkg-config.freedesktop.org/>.
See `config.log' for more details

pkg-config」とやらをインストールする必要があるのでしょうか。


「pkg-config」を調べる

Wikipedia に pkg-config についての記事がある。
しかし,よく分からん。

コンパイルするときに使ってるのかな?

とりあえず,導入の方法を探る。
Wikipediaにある外部リンク「php-config」を踏む。

これをみると,Git で公開されているらしい。
git://anongit.freedesktop.org/pkg-config が,リンク切れ(?)。
となりの(browse)というリンクを踏むと,リストが表示された。

どうやら,GitHubではなく,GitLabというページで公開されているようだ。

早速,pkg-config を Clone しよう・・・と思ったところ,READMEをみると,結構ややこしそう。Clone したところで,Mac でビルドできないんじゃないか?


Homebrew で「pkg-config」をインストール

 % brew install pkg-config

愚直に,brew からコマンド叩いてインストールできるのではないか?

コマンド叩いて,ターミナルを再起動したところ,無事導入されたようです。

% which pkg-config
/usr/local/bin/pkg-config

Homebrew は必須ですね・・・。


PHP7.4のインストールにトライ(2度目):失敗

% phpenv install 7.4.6

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

Here are the last 10 lines from the log:

-----------------------------------------

No package 'krb5-gssapi' found
No package 'krb5' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables KERBEROS_CFLAGS
and KERBEROS_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
-----------------------------------------

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

ここまで,導入に手間取ると,疲れてきますね。


「krb5」をbrew で導入

先ほどの「pkg-config」と同様に,「krb5」を brew で叩いてみましょう。

% brew install krb5

openssl@1.1 も一緒に導入されました。
なぜでしょう。

そして,macOSは,今回インストールしたものではなく,デフォルトのプログラムを読みに行くから,.zshrc に色々と書き込む必要があるとアラート。

OpenSSL のパスも書き換える必要があるのか・・・?

とりあえず,先に krb5 のパスを書き換える。
.zshenv に下の行を追加。

# krb5
export PATH="/usr/local/opt/krb5/bin:$PATH"
export PATH="/usr/local/opt/krb5/sbin:$PATH"
export LDFLAGS="-L/usr/local/opt/krb5/lib"
export CPPFLAGS="-I/usr/local/opt/krb5/include"
export PKG_CONFIG_PATH="/usr/local/opt/krb5/lib/pkgconfig"

PHP7.4のインストールにトライ(3度目):失敗

% phpenv install 7.4.6

祈る思いで,コマンドを叩く。

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

Here are the last 10 lines from the log:

-----------------------------------------
configure: error: Package requirements (openssl >= 1.0.1) were not met:

No package 'openssl' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables OPENSSL_CFLAGS
and OPENSSL_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
-----------------------------------------

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

当然,想いは届かない。

どうやら,「OpenSSL のバージョンが 1.0.1 以上じゃないとダメ!」らしい。

だから,krb5 の導入時に OpenSSL の1.1 が入ったのですね。


.zshenv に OpenSSL のパス等を記述

先ほど krb5 を導入したときに,
.pem ファイルを使っているなら,
/usr/local/etc/openssl@1.1/certs
に,コピーして,
/usr/local/opt/openssl@1.1/bin/c_rehash
しろ,というメッセージもあった気がします。
.pem ファイルとかは,とくにもっていない,はず。

参考:[OpenSSL] pem ファイルとは?SSL証明書の中身を確認する方法

上の記事にもあるように,
むかしむかしの記憶を探ってみると,.pem ファイルは,
サーバーのSSL証明書に使ったファイルだった気がします。

SSHの公開鍵や秘密鍵は関係ない・・・はず。
なので,今回は,これは放置。

そして,また,.zshenv を修正。

# OpenSSL 1.1
export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib"
export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include"
export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig"

上の行を追加。


PHP7.4のインストールにトライ(4度目):失敗

% phpenv install 7.4.6

祈る(午前2時半)。そして,エラー。

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

Here are the last 10 lines from the log:

-----------------------------------------

No package 'krb5-gssapi' found
No package 'krb5' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables KERBEROS_CFLAGS
and KERBEROS_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
-----------------------------------------

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

また, krb5 。さっき入れたよね?

PKG_CONFIG_PATH 云々いってますので,確認。

% echo $PKG_CONFIG_PATH
/usr/local/opt/openssl@1.1/lib/pkgconfig

先ほど設定した値になってますね。

pkgconfig % ls
libcrypto.pc	libssl.pc	openssl.pc

なかにも,いくつかファイルがいらっしゃいます。
(これで,じゅうぶんなのかは分からないのですが。)

brew に krb5 なる別のものがあるのか,とも考えて検索。

% brew search krb5
==> Formulae
krb5 ✔

特にないみたいですね・・・。

ここで,再度,さきほど krb5 をインストールしたときに表示されたメッセージを確認。

PKG_CONFIG_PATHが2カ所ある!

1つは,OpenSSL,もう1つは Krb5 のところ。
また,「PKG_CONFIG_PATH」だけでなく,
「LDFLAGS」と「CPPFLAGS」もダブっている。

とりあえず,先ほど,OpenSSL で .zshenv に設定した行を,
export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"
以外,コメントアウトする。


PHP7.4のインストールにトライ(5度目):失敗

% phpenv install 7.4.6

祈る(午前2時40分頃)。そして,エラー。

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

Here are the last 10 lines from the log:

-----------------------------------------
configure: error: Package requirements (openssl >= 1.0.1) were not met:

No package 'openssl' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables OPENSSL_CFLAGS
and OPENSSL_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
-----------------------------------------

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

何か,根本的な間違いをしている気がする。

試しに,openssl の場所を確認する。

% which openssl
/usr/bin/openssl

古い(1.1じゃない)方を見に行ってますね。そりゃダメですね。


.zshenv を修正

先ほど,OpenSSL のパス以外をコメントアウトしましたが,それが間違っているのではないか,ということで .zshenv を修正。

# krb5
export PATH="/usr/local/opt/krb5/bin:$PATH"
export PATH="/usr/local/opt/krb5/sbin:$PATH"
export LDFLAGS="-L/usr/local/opt/krb5/lib"
export CPPFLAGS="-I/usr/local/opt/krb5/include"
export PKG_CONFIG_PATH="/usr/local/opt/krb5/lib/pkgconfig"

# OpenSSL 1.1
export PATH="/usr/local/opt/openssl@1.1/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/openssl@1.1/lib:$LDFLAGS"
export CPPFLAGS="-I/usr/local/opt/openssl@1.1/include:$CPPFLAGS"
export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig:$PKG_CONFIG_PATH"

OpenSSLの方で,LDFLAGSCPPFLAGSPKG_CONFIG_PATHの定義をするときに,Krb5 での定義をちゃんと引っ張ってきて上書きするように修正。


PHP7.4のインストールにトライ(6度目):失敗

% phpenv install 7.4.6

祈る(午前2時40分頃)。そして,エラー(無慈悲)。

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

Here are the last 10 lines from the log:

-----------------------------------------
configure: WARNING: unrecognized options: --with-png-dir, --with-icu-dir
configure: error: Please reinstall the BZip2 distribution
-----------------------------------------

しかし,エラー内容が変わりましたね。
「BZip2 をインストールせよ」とのこと。

ちなみに,OpenSSLのパスはとくに関係ないみたいです。

% which openssl
/usr/bin/openssl

LDFLAGSCPPFLAGSPKG_CONFIG_PATH,これらが重要だったみたい。

とりあえず, brew で bzip2 をインストールしてみましょう。


brew で BZip2 をインストール

% brew install bzip2

bzip2 is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have bzip2 first in your PATH run:
  echo 'export PATH="/usr/local/opt/bzip2/bin:$PATH"' >> ~/.zshrc

For compilers to find bzip2 you may need to set:
  export LDFLAGS="-L/usr/local/opt/bzip2/lib"
  export CPPFLAGS="-I/usr/local/opt/bzip2/include"

インストールは成功しましたが,先ほどから見慣れたメッセージが出てきましたね。

早速,.zshenv を修正します(何度目でしょうか)。

# BZip2
export PATH="/usr/local/opt/bzip2/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/bzip2/lib:$LDFLAGS"
export CPPFLAGS="-I/usr/local/opt/bzip2/include:$CPPFLAGS"

こんな感じの行を追加。


PHP7.4のインストールにトライ(7度目):失敗

% phpenv install 7.4.6

祈る(午前3時10分頃)。そして,エラー。

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

Here are the last 10 lines from the log:

-----------------------------------------
configure: WARNING: unrecognized options: --with-png-dir, --with-icu-dir
configure: error: Please reinstall the BZip2 distribution
-----------------------------------------

このエラーは,さっき見た気がします。

ググってみます。

参考:phpbrew で bzip2 や zlib が見つからないというエラー

まさに,これではないでしょうか

インストールのコマンドに引数を与えてやります。

% phpenv install 7.4.6 \
    +bz2="$(brew --prefix bzip2)" \
    +zlib="$(brew --prefix zlib)"

こんな感じですかね。


PHP7.4のインストールにトライ(8度目):失敗

% phpenv install 7.4.6 +bz2="$(brew --prefix bzip2)" +zlib="$(brew --prefix zlib)"

祈る(午前3時15分頃)。そして,エラー。

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

Here are the last 10 lines from the log:

-----------------------------------------
configure: WARNING: unrecognized options: --with-png-dir, --with-icu-dir
configure: error: Please reinstall the BZip2 distribution
-----------------------------------------

違うみたいですね。

心が折れかけていますが,探ります。

ひたすら,ググる。

参考:macOS Mojave で php BZip2 をインストールできない

上の記事では,
sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target / 
をすればよいとありましたが,私のMacでは,CommandLineTools 以下に,Packagesなるフォルダがありませんでした。

参考:macOS Catalinaでもphpenv使いたかったのでdefault_configure_optionsいじったりPHP_BUILD_CONFIGURE_OPTS指定したりした

php-buildには default_configure_options というファイルがあり、こちらに configure で利用するオプションを記載しています。

ROXX(旧SCOUTER)開発者ブログ

そうなのか!!

とりあえず,こちらの記事を参照に愚直に試行します。

先に「libiconv」「zlib」パッケージを追加

% brew install bzip2 libiconv zlib
 % brew install bzip2 libiconv zlib

(中略)

Warning: bzip2 1.0.8 is already installed and up-to-date
To reinstall 1.0.8, run `brew reinstall bzip2`

(中略)
libiconv is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have libiconv first in your PATH run:
  echo 'export PATH="/usr/local/opt/libiconv/bin:$PATH"' >> ~/.zshrc

For compilers to find libiconv you may need to set:
  export LDFLAGS="-L/usr/local/opt/libiconv/lib"
  export CPPFLAGS="-I/usr/local/opt/libiconv/include"
zlib is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

For compilers to find zlib you may need to set:
  export LDFLAGS="-L/usr/local/opt/zlib/lib"
  export CPPFLAGS="-I/usr/local/opt/zlib/include"

For pkg-config to find zlib you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/zlib/lib/pkgconfig"

あわせて,.zshenv を修正します。

# libiconv
export PATH="/usr/local/opt/libiconv/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/libiconv/lib:$LDFLAGS"
export CPPFLAGS="-I/usr/local/opt/libiconv/include:$CPPFLAGS"

# zlib
export LDFLAGS="-L/usr/local/opt/zlib/lib:$LDFLAGS"
export CPPFLAGS="-I/usr/local/opt/zlib/include:$CPPFLAGS"
export PKG_CONFIG_PATH="/usr/local/opt/zlib/lib/pkgconfig:$PKG_CONFIG_PATH"

default_configure_options の編集

カレントディレクトリをプラグインの場所に移動。

% cd .phpenv/plugins/php-build/share/php-build

「default_configure_options」を,ROXX(旧SCOUTER)開発者ブログ をたよりに,編集(先にコピーを取っておきます)。

編集箇所は,記事にあるように,

--with-zlib-dir=/usr
--with-bz2

の削除。

--with-zlib-dir=/usr/local/opt/zlib
--with-bz2=/usr/local/opt/bzip2
--with-iconv=/usr/local/opt/libiconv

の追加。


PHP7.4のインストールにトライ(9度目):失敗

% phpenv install 7.4.6

祈る(午前3時45分頃)。そして,エラー。

そろそろ,明日に響く時間帯です。

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

Here are the last 10 lines from the log:

-----------------------------------------
No package 'icu-uc' found
No package 'icu-io' found
No package 'icu-i18n' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables ICU_CFLAGS
and ICU_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
-----------------------------------------

ICU_CFLAGS とか, ICU_LIBS とか,なんのことかわかりませんが,求められているパッケージをインストールします。


brew で icu4c を再インストール

% brew install icu-uc icu-io icu-i18n

Error: No available formula with the name “icu-uc” 

そうかー。

一応,brew で確認。

% brew search icu
==> Formulae
icu4c ✔                                  ploticus
==> Casks
cricut-design-space        panic-unison               visicut

入ってるっぽいですけどね。再インストールしましょう。

% brew reinstall icu4c
(中略)
icu4c is keg-only, which means it was not symlinked into /usr/local,
because macOS provides libicucore.dylib (but nothing else).

If you need to have icu4c first in your PATH run:
  echo 'export PATH="/usr/local/opt/icu4c/bin:$PATH"' >> ~/.zshrc
  echo 'export PATH="/usr/local/opt/icu4c/sbin:$PATH"' >> ~/.zshrc

For compilers to find icu4c you may need to set:
  export LDFLAGS="-L/usr/local/opt/icu4c/lib"
  export CPPFLAGS="-I/usr/local/opt/icu4c/include"

For pkg-config to find icu4c you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/icu4c/lib/pkgconfig"

ここら辺が怪しそうですね。設定してない行です。

またも,.zshenv を修正して,祈ります。

# icu4c
export PATH="/usr/local/opt/icu4c/bin:$PATH"
export PATH="/usr/local/opt/icu4c/sbin:$PATH"
export LDFLAGS="-L/usr/local/opt/icu4c/lib:$LDFLAGS"
export CPPFLAGS="-I/usr/local/opt/icu4c/include:$CPPFLAGS"
export PKG_CONFIG_PATH="/usr/local/opt/icu4c/lib/pkgconfig:$PKG_CONFIG_PATH"

こんな感じのものを追記。


PHP7.4のインストールにトライ(10度目):失敗

試行回数が2桁にのりました。

% phpenv install 7.4.6

当然のように,エラー(3時55分)。

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

Here are the last 10 lines from the log:

-----------------------------------------
configure: error: Package requirements (oniguruma) were not met:

No package 'oniguruma' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables ONIG_CFLAGS
and ONIG_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
-----------------------------------------

oniguruma」ってなんでしょうか。

エラー見る度に毎回思うんですけど,最初から,不足しているパッケージを全部出してくれませんかね。仕方ないか。


oniguruma のインストール

愚直に brew でインストール。

 % brew search oniguruma
==> Formulae
oniguruma

いますね。

% brew install oniguruma

とくに,「.zshenv に,あれをかけ」などは出ませんでした。


PHP7.4のインストールにトライ(11度目):失敗

% phpenv install 7.4.6

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

Here are the last 10 lines from the log:

-----------------------------------------
configure: WARNING: unrecognized options: --with-png-dir, --with-icu-dir
configure: error: Cannot find libtidy
-----------------------------------------

はい。


LibTidy のインストール

早速,brew でインストールをかけようとしたが,search してもみつからない。

ググってみると,「tidy-html5」という名前で提供されているらしい。

かまわず,brew install を叩く。

% brew search tidy-html5
==> Formulae
tidy-html5
% brew install tidy-html5

PHP7.4のインストールにトライ(12度目):失敗

% phpenv install 7.4.6

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

Here are the last 10 lines from the log:

-----------------------------------------
configure: error: Package requirements (libzip >= 0.11) were not met:

No package 'libzip' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables LIBZIP_CFLAGS
and LIBZIP_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
-----------------------------------------

もう,ビルドエラーに何も感じなくなってきました。


brew で libzip のインストール

% brew install libzip

とくに,エラーなどはなし。


PHP7.4のインストールにトライ(13度目):失敗

% phpenv install 7.4.6

[Preparing]: /var/tmp/php-build/source/7.4.6

今まで,ここでコケていたのが,はじめてコンパイルまで行く。

[Compiling]: /var/tmp/php-build/source/7.4.6

期待していいのだろうか。

当然ながら,かなり時間がかかります。
clangrbenvがかなり回っていますが,ターミナル側には何も出力されないので,ちょっと不安です。
何か出力してくれた方が,安心しますね・・・。

と,期待させていたところで,エラー!

[xdebug]: Installing version 2.9.5
[Downloading]: http://xdebug.org/files/xdebug-2.9.5.tgz
[xdebug]: Compiling xdebug in /var/tmp/php-build/source/xdebug-2.9.5

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

Here are the last 10 lines from the log:

-----------------------------------------
warning: unsupported relocation in debug_info section.
note: while processing /private/var/tmp/php-build/source/7.4.6/ext/opcache/.libs/zend_accelerator_util_funcs.o
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: ext/opcache/.libs/opcache.a(shared_alloc_shm.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: ext/opcache/.libs/opcache.a(shared_alloc_shm.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: /var/tmp/php-build/source/7.4.6/modules/opcache.a(shared_alloc_shm.o) has no symbols
ld: warning: directory not found for option '-L/usr/local/opt/icu4c/lib:-L/usr/local/opt/zlib/lib:-L/usr/local/opt/libiconv/lib:-L/usr/local/opt/bzip2/lib:-L/usr/local/opt/openssl@1.1/lib:-L/usr/local/opt/krb5/lib'
ld: warning: directory not found for option '-L/usr/local/opt/icu4c/lib:-L/usr/local/opt/zlib/lib:-L/usr/local/opt/libiconv/lib:-L/usr/local/opt/bzip2/lib:-L/usr/local/opt/openssl@1.1/lib:-L/usr/local/opt/krb5/lib'
ld: warning: directory not found for option '-L/usr/local/opt/icu4c/lib:-L/usr/local/opt/zlib/lib:-L/usr/local/opt/libiconv/lib:-L/usr/local/opt/bzip2/lib:-L/usr/local/opt/openssl@1.1/lib:-L/usr/local/opt/krb5/lib'
ld: warning: directory not found for option '-L/usr/local/opt/icu4c/lib:-L/usr/local/opt/zlib/lib:-L/usr/local/opt/libiconv/lib:-L/usr/local/opt/bzip2/lib:-L/usr/local/opt/openssl@1.1/lib:-L/usr/local/opt/krb5/lib'
PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
-----------------------------------------

ちょっと,意味が分からないですね。ログファイルを見ます。

configure: WARNING: unrecognized options: --with-png-dir, --with-icu-dir
configure: WARNING: unrecognized options: --with-png-dir, --with-icu-dir
ld: warning: directory not found for option '-L/usr/local/opt/icu4c/lib:-L/usr/local/opt/zlib/lib:-L/usr/local/opt/libiconv/lib:-L/usr/local/opt/bzip2/lib:-L/usr/local/opt/openssl@1.1/lib:-L/usr/local/opt/krb5/lib'
warning: unsupported relocation in debug_info section.
note: while processing /private/var/tmp/php-build/source/7.4.6/ext/opcache/.libs/zend_accelerator_util_funcs.o
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: ext/opcache/.libs/opcache.a(shared_alloc_shm.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: ext/opcache/.libs/opcache.a(shared_alloc_shm.o) has no symbols
/Library/Developer/CommandLineTools/usr/bin/ranlib: file: /var/tmp/php-build/source/7.4.6/modules/opcache.a(shared_alloc_shm.o) has no symbols
ld: warning: directory not found for option '-L/usr/local/opt/icu4c/lib:-L/usr/local/opt/zlib/lib:-L/usr/local/opt/libiconv/lib:-L/usr/local/opt/bzip2/lib:-L/usr/local/opt/openssl@1.1/lib:-L/usr/local/opt/krb5/lib'
ld: warning: directory not found for option '-L/usr/local/opt/icu4c/lib:-L/usr/local/opt/zlib/lib:-L/usr/local/opt/libiconv/lib:-L/usr/local/opt/bzip2/lib:-L/usr/local/opt/openssl@1.1/lib:-L/usr/local/opt/krb5/lib'
ld: warning: directory not found for option '-L/usr/local/opt/icu4c/lib:-L/usr/local/opt/zlib/lib:-L/usr/local/opt/libiconv/lib:-L/usr/local/opt/bzip2/lib:-L/usr/local/opt/openssl@1.1/lib:-L/usr/local/opt/krb5/lib'
ld: warning: directory not found for option '-L/usr/local/opt/icu4c/lib:-L/usr/local/opt/zlib/lib:-L/usr/local/opt/libiconv/lib:-L/usr/local/opt/bzip2/lib:-L/usr/local/opt/openssl@1.1/lib:-L/usr/local/opt/krb5/lib'
PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.

brew で libxml2 をインストールする

いままで,エラーメッセージにはなかったけれど,もしかしてこれが原因なのだろうか。

% brew install libxml2
(中略)
readline is keg-only, which means it was not symlinked into /usr/local,
because macOS provides BSD libedit.

For compilers to find readline you may need to set:
  export LDFLAGS="-L/usr/local/opt/readline/lib"
  export CPPFLAGS="-I/usr/local/opt/readline/include"

For pkg-config to find readline you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/readline/lib/pkgconfig"
sqlite is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have sqlite first in your PATH run:
  echo 'export PATH="/usr/local/opt/sqlite/bin:$PATH"' >> ~/.zshrc

For compilers to find sqlite you may need to set:
  export LDFLAGS="-L/usr/local/opt/sqlite/lib"
  export CPPFLAGS="-I/usr/local/opt/sqlite/include"

For pkg-config to find sqlite you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/sqlite/lib/pkgconfig"
Python has been installed as
  /usr/local/opt/python@3.8/bin/python3

You can install Python packages with
  /usr/local/opt/python@3.8/bin/pip3 install <package>
They will install into the site-package directory
  /usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages

See: https://docs.brew.sh/Homebrew-and-Python
python@3.8 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 python@3.8 first in your PATH run:
  echo 'export PATH="/usr/local/opt/python@3.8/bin:$PATH"' >> ~/.zshrc

For compilers to find python@3.8 you may need to set:
  export LDFLAGS="-L/usr/local/opt/python@3.8/lib"

For pkg-config to find python@3.8 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/python@3.8/lib/pkgconfig"
libxml2 is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have libxml2 first in your PATH run:
  echo 'export PATH="/usr/local/opt/libxml2/bin:$PATH"' >> ~/.zshrc

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"

For pkg-config to find libxml2 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/libxml2/lib/pkgconfig"
readline is keg-only, which means it was not symlinked into /usr/local,
because macOS provides BSD libedit.

For compilers to find readline you may need to set:
  export LDFLAGS="-L/usr/local/opt/readline/lib"
  export CPPFLAGS="-I/usr/local/opt/readline/include"

For pkg-config to find readline you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/readline/lib/pkgconfig"
sqlite is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have sqlite first in your PATH run:
  echo 'export PATH="/usr/local/opt/sqlite/bin:$PATH"' >> ~/.zshrc

For compilers to find sqlite you may need to set:
  export LDFLAGS="-L/usr/local/opt/sqlite/lib"
  export CPPFLAGS="-I/usr/local/opt/sqlite/include"

For pkg-config to find sqlite you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/sqlite/lib/pkgconfig"
Python has been installed as
  /usr/local/opt/python@3.8/bin/python3

You can install Python packages with
  /usr/local/opt/python@3.8/bin/pip3 install <package>
They will install into the site-package directory
  /usr/local/opt/python@3.8/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages

See: https://docs.brew.sh/Homebrew-and-Python
python@3.8 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 python@3.8 first in your PATH run:
  echo 'export PATH="/usr/local/opt/python@3.8/bin:$PATH"' >> ~/.zshrc

For compilers to find python@3.8 you may need to set:
  export LDFLAGS="-L/usr/local/opt/python@3.8/lib"

For pkg-config to find python@3.8 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/python@3.8/lib/pkgconfig"
libxml2 is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have libxml2 first in your PATH run:
  echo 'export PATH="/usr/local/opt/libxml2/bin:$PATH"' >> ~/.zshrc

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"

For pkg-config to find libxml2 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/libxml2/lib/pkgconfig"

なんか,めちゃくちゃパッケージがインストールされましたね。
Python とか readline とか,sqliteとか。

当然, .zshenv を修正します。

# libxml2
export PATH="/usr/local/opt/libxml2/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/libxml2/lib:$LDFLAGS"
export CPPFLAGS="-I/usr/local/opt/libxml2/include:$CPPFLAGS"
export PKG_CONFIG_PATH="/usr/local/opt/libxml2/lib/pkgconfig:$PKG_CONFIG_PATH"

# readline
export LDFLAGS="-L/usr/local/opt/readline/lib:$LDFLAGS"
export CPPFLAGS="-I/usr/local/opt/readline/include:$CPPFLAGS"
export PKG_CONFIG_PATH="/usr/local/opt/readline/lib/pkgconfig:$PKG_CONFIG_PATH"

# sqlite
export PATH="/usr/local/opt/sqlite/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/sqlite/lib:$LDFLAGS"
export CPPFLAGS="-I/usr/local/opt/sqlite/include:$CPPFLAGS"
export PKG_CONFIG_PATH="/usr/local/opt/sqlite/lib/pkgconfig:$PKG_CONFIG_PATH"

# python3.8
export PATH="/usr/local/opt/python@3.8/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/python@3.8/lib:$LDFLAGS"
export PKG_CONFIG_PATH="/usr/local/opt/python@3.8/lib/pkgconfig:$PKG_CONFIG_PATH"

PHP7.4のインストールにトライ(14度目):失敗

% phpenv install 7.4.6

現在,4時48分。徹夜ですね。

phpenv: ~/.phpenv/versions/7.4.6 already exists
continue with installation? (y/N) 

こんなメッセージが。さっきコケてたけど成功してたのか?

いや,失敗しているはずなので,再インストール。

[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.4.6 into ~/.phpenv/versions/7.4.6
[Skipping]: Already downloaded and extracted https://secure.php.net/distributions/php-7.4.6.tar.bz2
[Preparing]: /var/tmp/php-build/source/7.4.6
[Compiling]: /var/tmp/php-build/source/7.4.6

ここまでは一瞬で来ます。

[xdebug]: Compiling xdebug in /var/tmp/php-build/source/xdebug-2.9.5

ここでコケますね(5時2分)。

うーん。phpのバージョンを落として試してみましょうか。

% phpenv install 7.4.1

※ヤングマガジンの電子版(Kindle)を読みながら待ちます。
「ザ・ファブル」がないと,読むもの少ないです。
今週号は「マイホームヒーロー」「アルキメデスの大戦」ぐらい。
休載している「喧嘩稼業」「私の少年」「アンダーニンジャ」が定期的に連載されればいいな。
「サタノファニ」「1日外出録ハンチョウ」「パラレルパラダイス」「カイジ」がギャグ枠でしょうか。
ブラインドサッカーがテーマの「ブクロキックス」が面白くなりそう。期待。

結果は,やはりエラー。
[xdebug]: Compiling xdebug in /var/tmp/php-build/source/xdebug-2.9.5
でコケます。

ちょっとここでググってみましょうか。


定義ファイルの修正

参考:phpenv+php-buildでのDebug用PHPのビルド

定義ファイルというのがあるらしいですね。

「xDebug」でコケてるのだから,とりあえず,これをインストールしないようにできるのではないか?

.phpenv/plugins/php-build/share/php-build/definitions

ここにある, 7.4.6 を確認すると下の通り。

configure_option "--enable-gd"
configure_option "--with-jpeg"
configure_option "--with-zip"

install_package "https://secure.php.net/distributions/php-7.4.6.tar.bz2"
install_xdebug "2.9.5"
enable_builtin_opcache

この行にある「install_xdebug “2.9.5”」を削除すれば,コケないのでは?(xdebugが原因でコケているなら・・・)

早速,7.4.6 を 7.4.6_debugとコピーし,「install_xdebug~~」の行を削除。

そして,インストール。


PHP7.4(XDebugなし)のインストールにトライ(14度目):成功

% phpenv install -i development  7.4.6_debug

「phpenv/php-build の小ネタ集」「CentOSにphpenvでPHP環境を構築する」を参考に,ちょっと引数変えてみました。
(うまくいっていない状態で,別のこといじるのは良くないのですが)

[Preparing]: /var/tmp/php-build/source/7.4.6_debug
[Compiling]: /var/tmp/php-build/source/7.4.6_debug
[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.7.4.6_debug.20200518053236.log or rebuild with '--verbose' option
[Success]: Built 7.4.6_debug successfully.

おお,「Success」などという素敵な言葉が出ました。

「XDebug」でコケてたんですね。
必要なら,今後インストールしましょう。

さて,「CakePHP」です。


PATHの設定は .zshrc でする

% phpenv global 7.4.6_debug
% phpenv rehash

ここで php -v を実行すると,インストールした7.6.4_debugがよみこまれるはず,なのですが相変わらず,System7.3が読み込まれます。

どうやら,PATHがおかしい。調べてみると,丁寧に,
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
これらが優先して読み込まれている(PATHの先頭に定義されている)。

調べてみると,どうやら,path_helper というのが呼び出されるため,これらのPATHが上書きされてしまうようです。

参考:
PATH設定がどこにあるか分からないときに見るべきファイル
zshの設定ファイルの読み込み順序と使い方Tipsまとめ
Mac の zsh で PATH を zshenv に設定するとハマる

そこで,PATHは,.zshrc に書くことで,これを回避することができるようです。

※今まで,色々とコケていたのも,実はこの「PathHelper」が原因がだったのでは?

PATHを .zshrc に定義したことで,どうにか,PHPもphpenvで設定したものが読み込まれるようになりました。


ようやく CakePHP4 のインストール

さっそくコマンドを実行します。

% php composer.phar create-project --prefer-dist "cakephp/app:^4.0" cakephp

無事,最後まで走り,プロジェクトが生成されました!

早速,開発サーバーを走らせてみます。
デフォルトのURLは「http://localhost:8765」です。

 % bin/cake server

無事に表示されました!ここまで長かった!
(現在,6時40分)

最初の問題2)に挙げた「ext-intl」は,とくに設定しなくとも動いているようですね(遠い昔のことのようです)。

では,CakePHP4が動いた!というところで,一旦休みます。
少し横になって,また開発に移りましょう!


追記:PATH を通したら,簡単にコンパイルできた。

先に書いた,
「/usr/local/bin」
「/usr/bin」
「/bin」
「/usr/sbin」
「/sbin」
が,他のPATHより優先されていた問題ですが,これを解消したところ,xDebugを使わないように,定義ファイルをいじらずとも,あっさりとコンパイルできました。

% phpenv install -i development 7.4.6

※ development はつけています(これのせい,ではないでしょう。おそらく)。

[Preparing]: /var/tmp/php-build/source/7.4.6
[Compiling]: /var/tmp/php-build/source/7.4.6
[xdebug]: Installing version 2.9.5
[Skipping]: Already downloaded http://xdebug.org/files/xdebug-2.9.5.tgz
[xdebug]: Compiling xdebug in /var/tmp/php-build/source/xdebug-2.9.5
[xdebug]: Installing xdebug configuration in ~/.phpenv/versions/7.4.6/etc/conf.d/xdebug.ini
[xdebug]: Cleaning up.
[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.7.4.6.20200518064609.log or rebuild with '--verbose' option
[Success]: Built 7.4.6 successfully.

% php -v

PHP 7.4.6 (cli) (built: May 18 2020 06:57:30) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.6, Copyright (c), by Zend Technologies
    with Xdebug v2.9.5, Copyright (c) 2002-2020, by Derick Rethans

落とし穴がどこにあるのか,分かりません。
分かったときに,愕然としたりします。

まさか,PATHが原因だったとは・・・。
(zsh の設定ファイルの理解不足も原因。)


投稿に関連する記事抜粋

投稿記事のカテゴリやタグと同じ記事をランダム表示します。

(2023年6月25日)
カテゴリ:

 カテゴリ一覧

 雑談(171)
アニメ(19)/ゲーム(12)/ドラマ(3)/パソコン(36)/小説(3)/広島東洋カープ(16)/映画(11)/漫画(33)/音楽(61)
 製作記(89)
AI(4)/CakePHP(3)/CentOS(4)/JavaScript(JS)(9)/Nuxt.js(4)/Vue.js(18)/WordPress(23)/料理(10)