How To Enable PHP Debugging with XDebug and Visual Studio Code (Ubuntu)

Mark Leo Sumadero
3 min readMar 31, 2022

Assumptions — This guide assumes that you have already PHP installed in your environment.

Introduction — In this guide, we will learn how we can enable PHP debugging in Visual Studio Code using Xdebug. We will install the correct Xdebug version for the specific PHP versions. We will also install the PHP Debug extension in our Visual Studio Code.

Step 1 — Install XDebug

To install XDebug on Ubuntu, we can install it using the package manager. For Ubuntu (18.04 LTS/Bionic, 20.04 LTS/Focal), Run:

sudo apt-get install php-xdebug

After installing we can proceed now to Step 2.

Step 2 — Configure PHP

Find out which PHP ini file to modify. You can run in the command line:

php — ini

If there is a file with xdebug in the name, such as, /etc/php/7.4/cli/conf.d/99-xdebug.ini, then this is the file to use.

If that file does not exist, but there are other files in a conf.d or similar directory, you can create a new file there too. Please name it 99-xdebug.ini in that case.

Add the following line to 99-xdebug.ini

zend_extension=xdebug.so

Our next step is to add the following line to our php.ini configuration file. Edit php.ini and add this line at the end:

For XDebug v2.x.x:

[XDebug]
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_port = 9000

For XDebug v3.x.x:

[XDebug]
xdebug.mode = debug
xdebug.start_with_request = yes

Open a new command prompt and run php -v or php -m to verify the xdebug installation. Your new php info will look like this:

This means that xdebug is successfully installed.

Step 3 — Configure Visual Studio Code

Our final step is the configuration of our Visual Studio Code. Open your vscode. Go to Extensions (Ctrl+Shift+X), then search for PHP Debug and install it.

Once installed. Go to your project folder, open the Debugging options (Ctrl+Shift+D).

Click create a launch.json file and select PHP as environment, this will create a new launch.json on your project folder.

Leave it as is. You can now start debugging by adding breakpoints to your code.

Open a file you want to debug. Select lines you want to start debugging as shown in red.

To start debugging, press F5 on your keyboard this will run your code on debugging mode. Try to run your code and it will stop on the breakpoint you specified.

Watch the variables change once you navigate. To continue press F5 this will jump to the next breakpoint. To step over to the next line press F10. To stop debugging press Shift+F5.

Congratulations. You can now easily hunt persistent bugs in your code. Happy Coding.

For Windows User:

--

--