LoginSignup
0
0

More than 1 year has passed since last update.

Xdebug の PECL 拡張モジュールを任意の PHP バージョン用にコンパイルする

Posted at

やりたいこと

今パスが通ってるバージョンと異なるバージョンの PHP 用に Xdebug の PECL 拡張モジュールがほしい
が、インストールコマンドを叩くと、すでにインストール済みと出て失敗する。

$ pecl install xdebug
pecl/xdebug is already installed and is the same as the released version 3.2.0
install failed

前提条件

該当の PHP 本体はインストール済みの想定です。
今回は Homebrew でインストールした 8.1 用にコンパイルしました。
※パスを通しているのは 8.0 でした

Xdebug 本体をダウンロード

以下の URL からほしい Xdebug のバージョンをダウンロードします。
https://pecl.php.net/package/xdebug
今回は記事作成時点で最新の 3.2.0 を使用しました。

コンパイル

ダウンロードした圧縮ファイルを展開して、そのディレクトリ内に移動します。
そのディレクトリ内で、ほしいバージョンの phpize コマンドを実行します。
※試してないのですが、フルパスで指定しないと現在パスの通っている PHP のバージョンで実行されてしまうと思いますので注意

$ ~/Downloads/xdebug-3.2.0/xdebug-3.2.0# /opt/homebrew/Cellar/php/8.1.11/bin/phpize

--with-php-configオプションでほしいバージョンのphp-configを指定してconfigureを実行します。

$ ~/Downloads/xdebug-3.2.0/xdebug-3.2.0# ./configure --with-php-config=/opt/homebrew/Cellar/php/8.1.11/bin/php-config

※参考:https://www.php.net/manual/ja/install.pecl.phpize.php#82914

最後にmakeコマンドでコンパイルが実行されます。

$ ~/Downloads/xdebug-3.2.0/xdebug-3.2.0# make

以下のようにモジュールが作成されたディレクトリを教えてくれるので、その中にあるxdebug.soをコピーします。
今回の場合は作業していたディレクトリの中の modules のようでした。

(コンパイル中の出力が大量に出ますが省力しています)
----------------------------------------------------------------------
Libraries have been installed in:
   /Users/hoge/Downloads/xdebug-3.2.0/xdebug-3.2.0/modules <= ここ!!!

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `DYLD_LIBRARY_PATH' environment variable
     during execution

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

モジュールのコピー先

任意のバージョンのphp.iniにモジュールの参照先が記載されていますので、そのディレクトリにモジュールをコピーします。

php.ini
extension_dir = "/opt/homebrew/lib/php/pecl/20210902"

今回は/opt/homebrew/lib/php/pecl/20210902なのでそこにコピーしました。
※ PHP のバージョンによって違うようです

php.ini に使用するモジュールを追記する

php.iniの先頭に使用するモジュールを追加します。

php.ini
zend_extension="xdebug.so"

pecl installコマンドでインストールした場合、自動で先頭に追記されたのでそれに倣っています

インストール確認

インストールしたバージョンの php コマンドに以下のオプションをつけて xdebug がインストールされていればOK

$ /opt/homebrew/Cellar/php/8.1.11/bin# ./php -m | grep xdebug
xdebug <= うまくインストールできていれば出る

補足

コンパイルしたバージョンが異なる場合のエラー

無理やり別の PHP バージョンのモジュールを参照するように php.ini を書き換えたところ読み込めない旨のエラーが出ました。
なので手動でコンパイル、配置するという今回の手段に至りました。

# pecl list
PHP Warning:  Failed loading Zend extension 'xdebug.so' (tried: /opt/homebrew/lib/php/pecl/20200930/xdebug.so (dlopen(/opt/homebrew/lib/php/pecl/20200930/xdebug.so, 0x0009): tried: '/opt/homebrew/lib/php/pecl/20200930/xdebug.so' (no such file)), 

今使っている PHP のバージョンで Xdebug をインストールしたい人向け

通常は以下のコマンドでインストールできるので、おそらく一時的にパスに通す PHP のバージョンを切り替えればこのコマンドで通ったような気もします!(試してないです)

$ pecl install xdebug

参考

phpize で共有 PECL 拡張モジュールをコンパイルする方法
ほぼこちらの公式ドキュメント通りだったのでやはり公式ドキュメントは偉大

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0