Debugging PHP with VS Code, WSL 2 (Ubuntu 22.04), PHP 8.1 and Xdebug 3
Prerequisites
- [Windows](…/Microsoft Windows/) 11
- [Visual Studio Code](…/Microsoft Visual Studio Code/)
Steps
Install WSL 2 and Ubuntu 22.04
wsl --install
Start Ubuntu
One of:
- Click
Start
>Ubuntu
- Open an Ubuntu tab in [Windows Terminal](…/Windows Terminal/)
In Ubuntu
- Add Ondřej Surý’s PPA:
sudo add-apt-repository ppa:ondrej/php sudo apt update
- Install Xdebug 3.2.0 for PHP 8.1 (this will also install PHP 8.1):
sudo apt install -y php8.1-xdebug
- Open
/etc/php/8.1/cli/conf.d/20-xdebug.ini
in an editor and paste the following configuration (the first line should already exist):zend_extension = xdebug.so xdebug.mode = develop,debug xdebug.discover_client_host = 1
- Create a project folder:
mkdir ~/demo cd ~/demo mkdir .vscode
- Create a test file
~/demo/index.php
with the following contents:<?php phpinfo(); ?>
- Start the PHP web server:
php -S localhost:8080
In VS Code
Install the WSL extension like this:
Press
F1
, pressBackspace
, paste the following command and pressEnter
:ext install ms-vscode-remote.remote-wsl
Install the PHP Debug extension like this:
Press
F1
, pressBackspace
, paste the following command and pressEnter
:ext install xdebug.php-debug
Open the project folder in WSL like this:
- Press
F1
, enterWSL: Connect to WSL
and pressEnter
- Select the folder
demo
- Press
Create the file
~/demo/.vscode/launch.json
with the following contents:{ "configurations": [ { "name": "Listen for XDebug", "type": "php", "request": "launch" } ] }
Open the file
index.php
Set a breakpoint on the second line
Press
F5
to start debugging
In your web browser
Install “Xdebug helper” extension
Go to http://localhost:8080/
Click the extension’s icon and enable it
Reload the page
The breakpoint should now be hit.