Dec 3, 2014

Setting up xDebug with Sublime Text under Linux Mint

I wrote an earlier post about setting up the xDebug PHP debugger with the Sublime editor under Windows 7.

I'm now migrating to developing on a Linux system and have done this set up for Sublime Text 3 and Linux Mint 17.

This post borrows heavily from the earlier article. 

(1) Installed xDebug 2.2.3 extension for PHP.

(a) While logged in as root,

  # apt-get  install  php5-xdebug

This installed the php5-xdebug package, creating the two files

  /usr/lib/php5/20121212/xdebug.so
  /etc/php5/apache2/conf.d/20-xdebug.ini

On your system, the path for xdebug.so may be a little different.

20-xdebug.ini is actually a link to the file mentioned in step (2) below.

(b)  Restarted Apache. Loaded a page containing a call to phpinfo() to check that the extension was installed. On the page there appeared an xdebug section with several tables that showed settings and directives.

(2) Added directives for xDebug.

Edited /etc/php5/mods-available/xdebug.ini to include the following lines.

  xdebug.remote_enable = On
  xdebug.remote_host = "localhost"
  xdebug.remote_port = 9000
  xdebug.remote_handler = "dbgp"
  xdebug.remote_autostart = On
  xdebug.remote_mode = req
  xdebug.remote_connect_back = 0 


Because I only want to debug locally, I set xdebug.remote_connect_back to 0 to turn it off. I believe this should be set to 1 if you intend to debug a site running on a remote system.

(3) Installed the Sublime package manager, "Package Control".

(a)  Within Sublime, clicked Preferences > Browse Packages, then browsed up one level to find the full path for the Installed Packages folder. On my system the path is

  ~/.config/sublime-text-3/Installed Packages

(b)  Downloaded the file Package Control.sublime-package from 

   https://sublime.wbond.net/Package%20Control.sublime-package

(c)   Copied that file into the Installed Packages directory.

(d)  Re-started Sublime.

In Sublime's Command Palette window, various Package Control commands are then available on the palette and elsewhere.

(4) Using Sublime's Package Control, installed the Xdebug Client package.

  Tools > Command Palette ...  >  Package Control: Install Package

Typed Xdebug Client into the search field, and clicked on Xdebug Client in the search results to install.

This adds a submenu, Tools > Xdebug, or you can use the keyboard equivalents.

(5) To start an xDebug session.

To start a debugging session, I load the site by appending the query string ?XDEBUG_SESSION_START=1  to the initial url, like so.

  http://localhost/power-poetry/?XDEBUG_SESSION_START=1 

The value of  XDEBUG_SESSION_START  is a session name that is stored in a cookie, so it probably could be any name you choose.

There are other ways to start an xDebug session, but for getting started with using this debugger, this seems like the simplest.

Finally, in Sublime itself, to activate the editor as an xDebug client,

  Tools > Xdebug > Start  Debugging


Sources:

Debug PHP with Xdebug in Sublime Text 2 (in Linux mint)
http://saml.rilspace.org/debug-php-with-xdebug-in-sublime-text-2-in-linux-mint

XDEBUG EXTENSION FOR PHP
http://xdebug.org/docs/remote

martomo / SublimeTextXdebug
https://github.com/martomo/SublimeTextXdebug

Package Control
https://sublime.wbond.net/installation#st2

Download:  Package Control.sublime-package
https://sublime.wbond.net/Package%20Control.sublime-package

4 comments:

  1. You're so close to adding full IDE tools. Being able to trace your code with breakpoints is where the real value is. Perhaps with something like: https://github.com/Zinggi/UnrealScriptIDE

    ReplyDelete
    Replies
    1. xDebug together with Sublime as its client do provide the most basic features of a debugger. That includes stepping through code, setting breakpoints, and providing a view of the runtime stack. However, it does lack features that are more advanced, such as setting the program counter to a different statement. I may very well migrate to Komodo or PHPStorm before too long. But for the time being this is adequate and is a huge step up from inserting calls to dpm() all over the code.

      Delete
  2. Another option, running Firefox on MintOS: https://support.mozilla.org/en-US/kb/install-firefox-linux will give you access to the some plugins that will save you from having to: ?XDEBUG_SESSION_START=1

    https://addons.mozilla.org/en-us/firefox/addon/the-easiest-xdebug/

    I can't wait to see MintOS in action - show and tell time!

    ReplyDelete
    Replies
    1. This looks like a really useful add-on for Firefox. I'll keep it in mind as I get more settled into using Linux.

      Delete