Set up PHP debugging in Visual Studio Code and Ubuntu 20.04

Ubuntu, Visual Studio Code, and PHP logos.
Ubuntu, Visual Studio Code, and PHP logos.

In Ubuntu:

  1. Install PHP xDebug with sudo apt install php-xdebug.
  2. Add the following at the end of /etc/php/7.4/cli/php.ini and/or /etc/php/7.4/apache2/php.ini:
    [XDebug]
    xdebug.remote_enable = 1
    xdebug.remote_autostart = 1
  3. Restart the PHP built-in server and/or Apache HTTP Server.

In Visual Studio Code:

  1. In Visual Studio Code: Install the PHP Debug extension.
  2. Click on the activity bar debug icon.
  3. In the sidebar top, the gear icon will have a red indicator if there is no debug configuration for the project yet. If so, click on it, select the PHP environment if asked and it will automatically create one.
  4. Add a breakpoint by clicking on the left of a line number in your code. A red indicator will appear next to the line.
  5. Click on the green play button at the top of the sidebar. The bottom status bar will turn red indicating PHP Debug is listening for XDebug.
  6. Run the script or load a page in your browser which executes the line where you added the breakpoint line and the execution will be stopped there.
  7. Now you can inspect the environment in the sidebar and continue the execution, continue the execution step by step, stop and pause the script with the bar which will appear over the editor tabs. You can move the bar horizontally by clicking on the first icon.