Debugging PHP with VS Code, WSL 2 (Ubuntu 22.04), PHP 8.1 and Xdebug 3
Prerequisites
Section titled “Prerequisites”Install WSL 2 and Ubuntu 22.04
Section titled “Install WSL 2 and Ubuntu 22.04”wsl --installStart Ubuntu
Section titled “Start Ubuntu”One of:
- Click
Start>Ubuntu - Open an Ubuntu tab in Windows Terminal
In Ubuntu
Section titled “In Ubuntu”- Add Ondřej Surý’s PPA:
Terminal window sudo add-apt-repository ppa:ondrej/phpsudo apt update - Install Xdebug 3.2.0 for PHP 8.1 (this will also install PHP 8.1):
Terminal window sudo apt install -y php8.1-xdebug - Open
/etc/php/8.1/cli/conf.d/20-xdebug.iniin an editor and paste the following configuration (the first line should already exist):zend_extension = xdebug.soxdebug.mode = develop,debugxdebug.discover_client_host = 1 - Create a project folder:
Terminal window mkdir ~/democd ~/demomkdir .vscode - Create a test file
~/demo/index.phpwith the following contents:<?phpphpinfo();?> - Start the PHP web server:
Terminal window php -S localhost:8080
In VS Code
Section titled “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 WSLand pressEnter - Select the folder
demo
- Press
-
Create the file
~/demo/.vscode/launch.jsonwith 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
F5to start debugging
In your web browser
Section titled “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.