How to enable Xdebug On Laravel Sail and debugging code with VS Code
Tags: php, vscode, laravel, opensource
Published: 2022-05-27 00:22:15

---
title: How to enable Xdebug On Laravel Sail and debugging code with VS Code
published: true
date: 2022-05-26 15:01:11 UTC
tags: php,vscode,laravel,opensource
canonical_url: https://blog.devgenius.io/how-to-enable-xdebug-on-laravel-sail-and-debugging-code-with-vs-code-872fd750b340
---

#### Debugging Laravel with XDebug and Visual Studio Code

![Debugging Laravel with XDebug and Visual Studio Code](https://cdn-images-1.medium.com/max/1024/0*XpHD5v5VksqluOwO)
_iPhoto by Kevin Ku on Unsplash_

If you have not installed Laravel sail you can read [Laravel Sail installation](https://blog.devgenius.io/laravel-create-an-admin-panel-from-scratch-part-1-installation-8c11dae7e684) and complete the Laravel Sail setup.

To debug Laravel code we need to do the following steps.

- 1. Enable Xdebug in Laravel Sail
- 2. VS Code Configure PHP Debug
- — 2.1 VS Code install PHP Debug extension
- — 2.2 Create a launch.json file
- 3. Start Debugging

### 1. Enable Xdebug in Laravel Sail

The [Laravel Sail](https://laravel.com/docs/sail#debugging-with-xdebug) Docker comes with Xdebug support default. [Xdebug](https://xdebug.org/) is a popular and powerful debugger for PHP.

Open the .env configuration file and add the below configuration

```
SAIL_XDEBUG_MODE=develop,debug
```

Down and start the sail after the .env configuration update.

```
sail down
sail up
```

Test Xdebug is installed successfully by using sail php -v

![Test Xdebug](https://cdn-images-1.medium.com/max/658/1*gwqILWDdzJ1NcWrniN0Mlw.png)

### 2. VS Code Configure PHP Debug

#### 2.1 VS Code install PHP Debug extension

Open VS code and search “[PHP debug](https://marketplace.visualstudio.com/items?itemName=xdebug.php-debug)” in the extension search.

![VS Code install PHP Debug extension](https://cdn-images-1.medium.com/max/1024/1*sOMPhOqc2iw15TWj4f061A.png)

Install the PHP debug extension.

#### 2.2 Create a launch.json file

After installtion click Run tab and click Add configuration

**Run** > **Add Configuration**

![Create a launch.json file](https://cdn-images-1.medium.com/max/772/1*jbXpVinYIDIAV6mVptCmLw.png)

After that select the PHP environment. The launch.json file will be added to the root directory by VSCode.

![Create a launch.json file](https://cdn-images-1.medium.com/max/989/1*CAeYyytAKMGqQEQe0570GA.png)

Copy the below code and add it to your launch.json file

```
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for Xdebug",
            "type": "php",
            "request": "launch",
            "port": 9003,
            "pathMappings": {
                "/var/www/html": "${workspaceFolder}"
            }
        }
    ]
}
```

### 3. Start Debugging

Click the line number to add your breakpoints.

![Start Debugging](https://cdn-images-1.medium.com/max/1021/1*vtAhnuRzDZUMMjTqtdDdjg.png)

Click the Run tab and select the Start Debugging

**Run** > **Start Debugging (or) F5**

![Start Debugging](https://cdn-images-1.medium.com/max/660/1*QJww5_l76HHuVP51QEPuUQ.png)

In the browser open route of the breakpoint added the controller. In VS code your breakpoint is highlighted.

![VS code your breakpoint is highlighted](https://cdn-images-1.medium.com/max/1024/1*3Fhh4xwmA02rfn_6j3-B_Q.png)

Start to debug your code by using debug toolbar. More general information on debugging with VS Code can be found on [https://code.visualstudio.com/docs/editor/debugging](https://code.visualstudio.com/docs/editor/debugging).

- Continue / Pause F5
- Step Over F10
- Step Into F11
- Step Out Shift+F11
- Restart Ctrl+Shift+F5
- Stop Shift+F5

Thank you for reading.

Stay tuned for more!

_Follow me at_ [**_balajidharma.medium.com_**](https://balajidharma.medium.com/).

* * *