How to install Xdebug 3 on MacOS
Xdebug is an extension for PHP, and provides a range of features to improve the PHP development experience. It allows you to break during code execution and inspect all the variables in scope during a request.
The purpose of this blog is to install Xdebug 3 on MacOS with PHP 8.1 to debug PHP code.
Let’s start installing Xdebug 3 on MacOs:
Step 1: First check whether PHP 8.1 is installed or not; if not, run the below command:
brew install php@8.1
Step 2: To Install Xdebug, run the below command:
pecl install xdebug
If found warning like PHP Warning: mkdir(): File exists in /opt/homebrew/Cellar/php/8.1.8/share/php/pear/System.php on line 294
First, create pecl directory by using the command: mkdir /opt/homebrew/lib/php/pecl
Then again, run the command:
pecl install xdebug
After running the above command, you will see the below output:
running: find "/private/tmp/pear/temp/pear-build-rootO1x6b4/install-xdebug-3.2.2" | xargs ls -dils 15982931 0 drwxr-xr-x 3 root wheel 96 Aug 9 11:25 /private/tmp/pear/temp/pear-build-rootO1x6b4/install-xdebug-3.2.2 15983635 0 drwxr-xr-x 3 root wheel 96 Aug 9 11:25 /private/tmp/pear/temp/pear-build-rootO1x6b4/install-xdebug-3.2.2/opt 15983636 0 drwxr-xr-x 3 root wheel 96 Aug 9 11:25 /private/tmp/pear/temp/pear-build-rootO1x6b4/install-xdebug-3.2.2/opt/homebrew 15983637 0 drwxr-xr-x 3 root wheel 96 Aug 9 11:25 /private/tmp/pear/temp/pear-build-rootO1x6b4/install-xdebug-3.2.2/opt/homebrew/Cellar 15983638 0 drwxr-xr-x 3 root wheel 96 Aug 9 11:25 /private/tmp/pear/temp/pear-build-rootO1x6b4/install-xdebug-3.2.2/opt/homebrew/Cellar/php 15983639 0 drwxr-xr-x 3 root wheel 96 Aug 9 11:25 /private/tmp/pear/temp/pear-build-rootO1x6b4/install-xdebug-3.2.2/opt/homebrew/Cellar/php/8.1.12 15983640 0 drwxr-xr-x 3 root wheel 96 Aug 9 11:25 /private/tmp/pear/temp/pear-build-rootO1x6b4/install-xdebug-3.2.2/opt/homebrew/Cellar/php/8.1.12/pecl 15983641 0 drwxr-xr-x 3 root wheel 96 Aug 9 11:25 /private/tmp/pear/temp/pear-build-rootO1x6b4/install-xdebug-3.2.2/opt/homebrew/Cellar/php/8.1.12/pecl/20210902 15983642 776 -rwxr-xr-x 1 root wheel 394950 Aug 9 11:25 /private/tmp/pear/temp/pear-build-rootO1x6b4/install-xdebug-3.2.2/opt/homebrew/Cellar/php/8.1.12/pecl/20210902/xdebug.so Build process completed successfully Installing '/opt/homebrew/Cellar/php/8.1.12/pecl/20210902/xdebug.so' install ok: channel://pecl.php.net/xdebug-3.2.2 Extension xdebug enabled in php.ini
Step 3: After installing Xdebug, you must enable this extension in your php.ini file. To enable it, locate the php.ini file location and open php.ini file
sudo nano /opt/homebrew/etc/php/8.1/php.ini
Paste these lines in php.ini file:
zend_extension="xdebug.so" xdebug.mode=debug xdebug.start_with_request=yes
Run sudo apachectl restart command to restart service.
Step 4: Now run php -v command to check Xdebug is installed or not:
yogendrapratapsingh@Yogendras-MacBook-Pro ~ % php -v PHP 8.1.12 (cli) (built: Oct 30 2022 12:39:49) (NTS) Copyright (c) The PHP Group Zend Engine v4.1.12, Copyright (c) Zend Technologies with Xdebug v3.2.2, Copyright (c) 2002-2023, by Derick Rethans with Zend OPcache v8.1.12, Copyright (c), by Zend Technologies
If found any warning like:
PHP Warning: Failed loading Zend extension 'xdebug.so' (tried: /opt/homebrew/lib/php/pecl/20210902/xdebug.so (dlopen(/opt/homebrew/lib/php/pecl/20210902/xdebug.so, 0x0009): tried: '/opt/homebrew/lib/php/pecl/20210902/xdebug.so' (no such file)), /opt/homebrew/lib/php/pecl/20210902/xdebug.so.so (dlopen(/opt/homebrew/lib/php/pecl/20210902/xdebug.so.so, 0x0009): tried: '/opt/homebrew/lib/php/pecl/20210902/xdebug.so.so' (no such file))) in Unknown on line 0 Warning: Failed loading Zend extension 'xdebug.so' (tried: /opt/homebrew/lib/php/pecl/20210902/xdebug.so (dlopen(/opt/homebrew/lib/php/pecl/20210902/xdebug.so, 0x0009): tried: '/opt/homebrew/lib/php/pecl/20210902/xdebug.so' (no such file)), /opt/homebrew/lib/php/pecl/20210902/xdebug.so.so (dlopen(/opt/homebrew/lib/php/pecl/20210902/xdebug.so.so, 0x0009): tried: '/opt/homebrew/lib/php/pecl/20210902/xdebug.so.so' (no such file))) in Unknown on line 0 PHP 8.1.12 (cli) (built: Oct 30 2022 12:39:49) (NTS) Copyright (c) The PHP Group Zend Engine v4.1.12, Copyright (c) Zend Technologies with Zend OPcache v8.1.12, Copyright (c), by Zend Technologies
Then update zend_extension=”xdebug.so” to zend_extension=”/opt/homebrew/Cellar/php/8.1.12/pecl/20210902/xdebug.so” in php.ini file. Here updated value will be the same as in Step 2.
Conclusion
Finally, you’ve successfully installed Xdebug on PHP 8.1.With Xdebug, you can now debug PHP applications with ease.
Let us know in case of any issues during the installation process, and please feel free to reach out via comments.