Installation of Xdebug on MacOS Catalina 10.15

Guenter picture Guenter · Oct 10, 2019 · Viewed 7.2k times · Source

I tried to install Xdebug on OS X 10.15 and run into following problem:

/private/tmp/pear/install/xdebug/xdebug.c:25:10: fatal error: 'php.h' file not found

I tried to fix the problem like described here: Installing xdebug on MacOs Mojave - 'php.h' file not found

Unfortunately the header files cannot be found in this directory: /Library/Developer/CommandLineTools/Packages

Any ideas where I can get the current header files for OS X 10.15?

Answer

Louis Charette picture Louis Charette · Oct 14, 2019

tl;dr

Apple decided to remove headers file in /usr/include and the macOS_SDK_headers_for_macOS_10.14.pkg package. To install Xdebug, you'll have to manually compile Xdebug with the correct reference in both phpize and make.

For more details, I wrote a blog article about the issue and the solution


Update June 30th 2020

After some new investigation while trying to upgrade to a newer version of xdebug, I now believe most of the instructions in this answer are not necessary to enabled xdebug on the build int version of PHP. Before doing anything, you should check if xdebug.so already exists in /usr/lib/php/extensions/no-debug-non-zts-20180731/, which I believe is there by default (please confirm in comments). If so, you can skip to the Enabled support in PHP portion of this answer.

Note building xdebug from source code and actually trying to use that version of xdebug.so with the build-in PHP should end up in a "code signature" error. As described here and here, the only real solution would be to compile and use you own instance of PHP instead of the built-in one.


Long story short, Apple decided to nuke /usr/include in MacOS Catalina, which has been the default location for C header file for ever in UNIX systems. Trying to install through PEAR / PECL will return an error as the compiler will look for necessary headers file in /usr/include. So the solution is to compile Xdebug manually, manually specifying the actual location of the header files, which are still provided by Xcode, just at a different location.

First, make sure Xcode is installed, including the command line tools. The following command will display the location of the default SDK :

$ xcrun --show-sdk-path
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk

The header you'll want (php.h) will then be in /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/php/main.

Getting source

Let's compile 2.7.2, getting the source code from git. Alternatively, you can download the source from Xdebug site.

git clone https://github.com/xdebug/xdebug.git
cd xdebug
git checkout tags/2.7.2

phpize

Next we need to make a copy phpize so we can edit the include path :

cp /usr/bin/phpize .
nano ./phpize

Find this line :

includedir="`eval echo ${prefix}/include`/php"

...and replace it with this line :

includedir="/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/php"

Run phpize:

./phpize

You should now see something like this :

Configuring for:
PHP Api Version:         20180731
Zend Module Api No:      20180731
Zend Extension Api No:   320180731

Configure & build

We can now configure :

./configure --enable-xdebug

...and run make using our custom SDK location defined as compiler flags :

make CPPFLAGS='-I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/php -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/php/main -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/php/TSRM -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/php/Zend -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/php/ext -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/php/ext/date/lib'

Might see some warning, just ignore it for now. Finally, we'll need to run :

make install

Again, this command will fail because it can't move the extension to the right place. SIP will prevent it. But no worries, we'll take care of that manually at the next step. make install is still required as it will sign the *.so file.

Enabled support in PHP

Next, we move the executable somewhere safe. I use /usr/local/php/extensions.

sudo mkdir -p /usr/local/php/extensions
sudo cp /usr/lib/php/extensions/no-debug-non-zts-20180731/xdebug.so /usr/local/php/extensions

Then we edit the PHP configuration to enable Xdebug. Simply edit php.ini:

sudo nano /etc/php.ini

And we add the following at the bottom :

[xdebug]
zend_extension=/usr/local/php/extensions/xdebug.so
xdebug.remote_enable=on
xdebug.remote_log="/var/log/xdebug.log"
xdebug.remote_host=localhost
xdebug.remote_handler=dbgp
xdebug.remote_port=9000

Restart built in server to be sure :

sudo apachectl restart

And finally test everything went fine :

php -i | grep "xdebug support"

If the above command returns nothing, then Xdebug is not available on your install. Go back the steps to find out what's missing.


EDIT

A more complete fix would be to edit the result of php-config --include-dir, which returns /usr/include/php. That would make any installation find the necessary header files without having to manually edit files or compiler flags.