{"id":4174,"date":"2024-11-21T22:54:00","date_gmt":"2024-11-21T21:54:00","guid":{"rendered":"https:\/\/davidperezgar.com\/en\/?p=4174"},"modified":"2024-11-21T22:55:51","modified_gmt":"2024-11-21T21:55:51","slug":"how-install-configure-xdebug-mac-debug-php-apps","status":"publish","type":"post","link":"https:\/\/davidperezgar.com\/en\/blog\/how-install-configure-xdebug-mac-debug-php-apps\/","title":{"rendered":"How to install and configure Xdebug on Mac to debug your PHP applications"},"content":{"rendered":"\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-white ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Tabla de contenidos<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a3b3e291680e\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a3b3e291680e\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/davidperezgar.com\/en\/blog\/how-install-configure-xdebug-mac-debug-php-apps\/#What_is_Xdebug_and_what_is_it_for\" >What is Xdebug and what is it for<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/davidperezgar.com\/en\/blog\/how-install-configure-xdebug-mac-debug-php-apps\/#1_Real-time_debugging\" >1. Real-time debugging<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/davidperezgar.com\/en\/blog\/how-install-configure-xdebug-mac-debug-php-apps\/#2_Stack_Trace_Stack_Trace\" >2. Stack Trace (Stack Trace)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/davidperezgar.com\/en\/blog\/how-install-configure-xdebug-mac-debug-php-apps\/#3_Performance_profiling\" >3. Performance profiling<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/davidperezgar.com\/en\/blog\/how-install-configure-xdebug-mac-debug-php-apps\/#4_Code_Coverage\" >4. Code Coverage<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/davidperezgar.com\/en\/blog\/how-install-configure-xdebug-mac-debug-php-apps\/#How_to_start_using_Xdebug\" >How to start using Xdebug<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/davidperezgar.com\/en\/blog\/how-install-configure-xdebug-mac-debug-php-apps\/#How_to_install_Xdebug_on_Mac_to_debug_PHP_applications\" >How to install Xdebug on Mac to debug PHP applications<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/davidperezgar.com\/en\/blog\/how-install-configure-xdebug-mac-debug-php-apps\/#Prerequisites_Homebrew\" >Prerequisites: Homebrew<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/davidperezgar.com\/en\/blog\/how-install-configure-xdebug-mac-debug-php-apps\/#Steps_to_install_Xdebug_on_Mac\" >Steps to install Xdebug on Mac<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/davidperezgar.com\/en\/blog\/how-install-configure-xdebug-mac-debug-php-apps\/#Configure_Xdebug\" >Configure Xdebug<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/davidperezgar.com\/en\/blog\/how-install-configure-xdebug-mac-debug-php-apps\/#Ready_for_debugging\" >Ready for debugging!<\/a><\/li><\/ul><\/nav><\/div>\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_Xdebug_and_what_is_it_for\"><\/span>What is Xdebug and what is it for<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If you work with PHP, you&#8217;ve probably encountered complex errors that seem impossible to solve with simple <code>var_dump()<\/code> or <code>echo<\/code>. This is where <strong>Xdebug<\/strong> comes in, a must-have tool for any PHP developer who wants to improve their workflow and troubleshoot more efficiently.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Xdebug<\/strong> is a PHP extension designed to provide advanced debugging and performance analysis tools for your code. Let&#8217;s take a look at some of its most prominent features and how they can help you in your day-to-day work as a programmer.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Real-time_debugging\"><\/span>1. <strong>Real-time debugging<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">With Xdebug, you can forget about rudimentary methods like printing variables everywhere. It allows you to integrate an interactive debugger into your development environment (such as PhpStorm, Visual Studio Code or NetBeans) for:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Set breakpoints and stop execution at specific lines of code<\/li>\n\n\n\n<li>Inspect variable values at different times in the program.<\/li>\n\n\n\n<li>Execute the code flow step by step to understand exactly what is going on.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This not only <strong>speeds up error resolution<\/strong>, but also helps you better understand how the different parts of your application interact. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Stack_Trace_Stack_Trace\"><\/span>2. <strong>Stack Trace (Stack Trace)<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">When an error or exception occurs, Xdebug generates a detailed stack trace. This includes information such as:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Which functions were called before the error<\/li>\n\n\n\n<li>In which files and lines did the problem occur.<\/li>\n\n\n\n<li>The arguments that were passed to each function.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This level of detail is pure gold for quickly identifying the root cause of even the most complex problems.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_Performance_profiling\"><\/span>3. <strong>Performance profiling<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Is your application running slower than it should? Xdebug includes a <strong>performance profiler<\/strong> that analyzes your code for bottlenecks. It generates files compatible with tools like <strong>Webgrind<\/strong> or <strong>QCacheGrind<\/strong>, where you can visualize how much time and resources different functions or parts of your application are consuming.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4_Code_Coverage\"><\/span>4. <strong>Code Coverage<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">If you do unit testing, you are probably interested in knowing what parts of your code are being executed during testing. Xdebug generates coverage reports that show you which lines have been tested and which haven&#8217;t, helping you improve the quality of your tests.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_start_using_Xdebug\"><\/span>How to start using Xdebug<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Installation<\/strong>: You can install Xdebug from your operating system&#8217;s official repository or by compiling it manually.<\/li>\n\n\n\n<li><strong>Configuration<\/strong>: In the <code>php.ini<\/code> file, enable the extension and set key parameters such as <code>xdebug.mode<\/code> to define which functions you want to use (<code>debug<\/code>, <code>profile<\/code>, <code>trace<\/code>, etc.).<\/li>\n\n\n\n<li><strong>Integration<\/strong>: Configure your IDE to connect to Xdebug and start debugging like a pro.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Xdebug<\/strong> is more than a tool; it is a companion that can transform your PHP development experience. By adopting advanced practices like real-time debugging and performance analysis, you&#8217;ll not only write better code, but you&#8217;ll also save hours troubleshooting problems.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"How_to_install_Xdebug_on_Mac_to_debug_PHP_applications\"><\/span>How to install Xdebug on Mac to debug PHP applications<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Prerequisites_Homebrew\"><\/span>Prerequisites: Homebrew<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Before you start, make sure you have <strong>Homebrew<\/strong> installed, as we will use it to manage installations. If you don&#8217;t have it, install it by executing this command in your terminal:<\/p>\n\n\n\n<div class=\"cbc-code-wrapper\"><div class=\"cbc-code-bar\"><button class=\"cbc-copy-button\">Copy<\/button><\/div><pre class=\"wp-block-code\"><code class=\"\">\/bin\/bash -c \"$(curl -fsSL https:\/\/raw.githubusercontent.com\/Homebrew\/install\/HEAD\/install.sh)\"\n<\/code><\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Steps_to_install_Xdebug_on_Mac\"><\/span>Steps to install Xdebug on Mac<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Install PHP with Homebrew<\/strong><\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">First, install PHP using Homebrew. This command will install the latest version of PHP available:<\/p>\n\n\n\n<div class=\"cbc-code-wrapper\"><div class=\"cbc-code-bar\"><button class=\"cbc-copy-button\">Copy<\/button><\/div><pre class=\"wp-block-code\"><code class=\"\">brew install php\n<\/code><\/pre><\/div>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Install Xdebug<\/strong><\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">To install Xdebug on systems with M1 or later processors, use the following command:<\/p>\n\n\n\n<div class=\"cbc-code-wrapper\"><div class=\"cbc-code-bar\"><button class=\"cbc-copy-button\">Copy<\/button><\/div><pre class=\"wp-block-code\"><code class=\"\">arch -arm64 sudo pecl install xdebug\n<\/code><\/pre><\/div>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Verify installation<\/strong><\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Once the installation is complete, verify that PHP and Xdebug are properly installed by running:<\/p>\n\n\n\n<div class=\"cbc-code-wrapper\"><div class=\"cbc-code-bar\"><button class=\"cbc-copy-button\">Copy<\/button><\/div><pre class=\"wp-block-code\"><code class=\"\">php -v\n<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">You should see something like this in the output:<\/p>\n\n\n\n<div class=\"cbc-code-wrapper\"><div class=\"cbc-code-bar\"><button class=\"cbc-copy-button\">Copy<\/button><\/div><pre class=\"wp-block-code\"><code class=\"\">PHP 8.x.x (cli) (built: ...)\nZend Engine v4.x.x with Xdebug v3.x.x\n<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">This confirms that Xdebug is active.<\/p>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>Locate the <code>php.ini<\/code><\/strong><\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">To find the PHP configuration file, run:<\/p>\n\n\n\n<div class=\"cbc-code-wrapper\"><div class=\"cbc-code-bar\"><button class=\"cbc-copy-button\">Copy<\/button><\/div><pre class=\"wp-block-code\"><code class=\"\">php --ini\n<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<div class=\"cbc-code-wrapper\"><div class=\"cbc-code-bar\"><button class=\"cbc-copy-button\">Copy<\/button><\/div><pre class=\"wp-block-code\"><code class=\"\">Configuration File (php.ini) Path: \/opt\/homebrew\/etc\/php\/8.0\nLoaded Configuration File: \/opt\/homebrew\/etc\/php\/8.0\/php.ini\nScan for additional .ini files in: \/opt\/homebrew\/etc\/php\/8.0\/conf.d\n<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The file you need to edit is the one listed as <code>Loaded Configuration File<\/code>. In this case:<br><code>\/opt\/homebrew\/etc\/php\/8.0\/php.ini<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Configure_Xdebug\"><\/span>Configure Xdebug<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Open the <code>php.ini<\/code> file with your favorite text editor. For example:<\/p>\n\n\n\n<div class=\"cbc-code-wrapper\"><div class=\"cbc-code-bar\"><button class=\"cbc-copy-button\">Copy<\/button><\/div><pre class=\"wp-block-code\"><code class=\"\">nano \/opt\/homebrew\/etc\/php\/8.0\/php.ini\n<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">At the end of the file, add the following lines:<\/p>\n\n\n\n<div class=\"cbc-code-wrapper\"><div class=\"cbc-code-bar\"><button class=\"cbc-copy-button\">Copy<\/button><\/div><pre class=\"wp-block-code\"><code class=\"\">zend_extension=\"xdebug.so\"\nxdebug.mode=debug\nxdebug.start_with_request=yes\n<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Save the changes and close the file<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Ready_for_debugging\"><\/span>Ready for debugging!<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Restart your local server if necessary, and Xdebug will be configured to debug your PHP applications. With this setup, you will be able to integrate Xdebug with your favorite IDE, such as Visual Studio Code, and enjoy advanced debugging with breakpoints and detailed code analysis.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you run into problems, check the official documentation or verify that the above steps have been followed correctly. Good luck debugging your code! \ud83d\ude80<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is Xdebug and what is it for? If you work with PHP, you&#8217;ve probably encountered complex errors that seem impossible to solve with simple var_dump() or echo. This is where Xdebug comes in, a must-have tool for any PHP developer who wants to improve their workflow and solve problems more efficiently. Xdebug is a PHP extension designed to provide advanced debugging and performance analysis tools for your code. Let&#8217;s take a look at some of its most important features and how they can help you in your day-to-day work as a programmer. 1. Real-time debugging With &#8230; <a title=\"How to install and configure Xdebug on Mac to debug your PHP applications\" class=\"read-more\" href=\"https:\/\/davidperezgar.com\/blog\/desarrollo-web\/como-instalar-y-configurar-xdebug-en-mac-para-depurar-tus-aplicaciones-php\/\" aria-label=\"Read more about How to install and configure Xdebug on Mac to debug your PHP applications\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":4176,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_ayudawp_aiss_exclude":false,"_ayudawp_aiss_summary":"","_ayudawp_aiss_summary_provider":"","_ayudawp_aiss_summary_hash":"","webmentions_disabled_pings":false,"webmentions_disabled":false,"editor_notices":[],"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4174","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50"],"_links":{"self":[{"href":"https:\/\/davidperezgar.com\/en\/wp-json\/wp\/v2\/posts\/4174","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/davidperezgar.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/davidperezgar.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/davidperezgar.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/davidperezgar.com\/en\/wp-json\/wp\/v2\/comments?post=4174"}],"version-history":[{"count":0,"href":"https:\/\/davidperezgar.com\/en\/wp-json\/wp\/v2\/posts\/4174\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/davidperezgar.com\/en\/wp-json\/wp\/v2\/media\/4176"}],"wp:attachment":[{"href":"https:\/\/davidperezgar.com\/en\/wp-json\/wp\/v2\/media?parent=4174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/davidperezgar.com\/en\/wp-json\/wp\/v2\/categories?post=4174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/davidperezgar.com\/en\/wp-json\/wp\/v2\/tags?post=4174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}