How to install and configure Xdebug in linux
Xdebug
is an extension for PHP and provides a variety of features to improve the PHP development experience.
Installation
Installing Xdebug
with a package manager is usually the quickest way. Depending on your distribution, run the following command
Ubuntu (18.04 LTS/Bionic, 20.04 LTS/Focal):
sudo apt-get install php-xdebug
Ubuntu (Ondřej Surý's PPA):
sudo apt-get install php(version)-xdebug
For other linux distributions here.
Installing with PECL
You can install Xdebug
via PECL
on Linux with Homebrew.
Prerequisites:
GCC
and associated libraries.PHP
development headers.
Run:
pecl install xdebug
Configuring
First we have to find the xdebug
configuration file, on linux it is located in the following path:
cd /etc/php/your_php_version/mods-available
In this directory you will find xdebug.ini
, the xdebug
configuration file and open the file with a code editor.
sudo your_editor xdebug.ini
Enter the following settings:
zend_extension=xdebug.so
xdebug.mode=debug,develop
xdebug.start_with_request=yes
xdebug.client_port=8000
xdebug.client_host=127.0.0.1
xdebug.cli_color=1
xdebug.show_local_vars=1
In xdebug.client_port
enter the port you use in your applications.
Let's check that xdebug
has been installed and is working. To do this, use the following php code:
phpinfo();
In the php info you should find an xdebug section.
Solving a problem
In the image above, it shows that our xdebug
has the Development Helpers and Step Debugger modes active, but if in a given project we want to activate or deactivate any mode without touching the main xdebug file
.
To make this possible, we'll use the XDEBUG_MODE
environment variable, as in the example below:
XDEBUG_MODE=coverage php -S 127.0.0.1:8000
If we check again in the xdebug section we will only have coverage mode enabled
Here are the instructions for the other modes and all the possible xdebug
configurations.
This concludes our guide to installing and configuring xdebug on linux.
Don't forget to add a reaction if the post has helped.
Subscribe to my newsletter
Read articles from Antonio Silva directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Antonio Silva
Antonio Silva
Systems Development Technician and PHP Developer.