Today we are going to setup our Ubuntu to start debugging PHP with Sublime Text 2 and Xdebug.

The process is quite simple and is mostly extracted from the README of the SublimeXdebug github project.

1Install xdebug

We are going to install the standard package with:

sudo apt-get install php5-xdebug

After installing customize the xdebug settings with:

sudo gedit /etc/php5/conf.d/xdebug.ini

We will add to that file if not already present this settings :

xdebug.remote_enable=On
xdebug.remote_host="localhost"
xdebug.remote_port=9000
xdebug.remote_handler="dbgp"

That will enable remote debugging for localhost in the port 9000. Now restart apache:

sudo service apache2 restart

2Download and unzip Python 2.6

In newer Ubuntu systems you cannot install the old Python 2.6 so we need to manually download it and unzip it into the Sublime Text 2 folder.

From the Lucid repository download the old Lucid Python 2.6 Package for your system (amd64 for 64bits systems and i386 for 32bits). I downloaded the amd64 version into my user folder:

cd ~
wget http://ubuntuarchive.eweka.nl/ubuntu//pool/main/p/python2.6/python2.6_2.6.5-1ubuntu6_amd64.deb

Now we are going to extract its contents to a folder called python2.6.

For the amd64 version:

dpkg-deb -x python2.6_2.6.5-1ubuntu6_amd64.deb python2.6

For the i386 version:

dpkg-deb -x python2.6_2.6.5-1ubuntu6_i386.deb python2.6

Finally we will copy the usr/lib/python2.6 folder from the extracted folder to our Sublime Text 2 folder (in my case ~/programas/sublime_text2 ):

cp -R python2.6/usr/lib/python2.6/ ~/programas/sublime_text2/lib/

3Install SublimeXdebug Package

We will install SublimeXdebug through Package Control. If you need help installing Package Control you can read our previous post:

Sublime Text 2 for Joomla development

With Package Control installed we will install SublimeXdebug with: 

Ctrl + Shift + P > install > Xdebug

Then restart your Sublime Text 2 and your system is already to start debugging.

4Install and configure a remote debugger

Now we only need a tool to remote debugging. I've tested the Firefox easy Xdebug add-on and the Chrome Xdebug Helper extension.

We will use the Chrome Xdebug. After downloading and installing the Chrome extension you have to restart Chrome. Once restarted you we will see a new icon in the Chrome address bar:

chrome-xdebug

 

Clicking on it will enable/disable the debug. But we first need to adjust the extension to use the Sublime Text sessionkey.

In Chrome go to Tools > Extensions

And open the Xdebug Helper preferences:

chrome-xdebug-settings

As you see we added the "sublime.xdebug" sessionkey and enabled debugging only for localhost.

We are done. We can start debugging!

5Example debug

In Sublime we have created a test.php file in the folder www/test with this PHP content:

for ($i=0; $i < 50; $i++) { 
	echo 'This is the line: ' . ($i + 1) . '
'; }

Put your cursor in the echo line and press Ctrl + F8. This will create a breakpoint in that line.

st2-debug

Now start debugging with Shift + F8 > Start debugging. Sublime will start waiting for a remote debug connection.

In Chrome navigate to http://localhost/test/test.php and click in the Xdebug Helper button to set it green. Reload the page and you will notice that it's freezed. That's because it is already captured by Sublime.

Now in Sublime start looping the for bucle with Ctrl + Shift + F5. You will see the variables values changing in the Xdebug Context tab.

6SublimeXdebug shortcuts

  • Shift+f8: Open XDebug quick panel
  • f8: Open XDebug control quick panel when debugger is connected
  • Ctrl+f8: Toggle breakpoint
  • Ctrl+Shift+f5: Run to next breakpoint
  • Ctrl+Shift+f6: Step over
  • Ctrl+Shift+f7: Step into
  • Ctrl+Shift+f8: Step out 

7References

https://github.com/Kindari/SublimeXdebug
http://stackoverflow.com/questions/11779284/xdebug-sublime-text-2-and-kindarisublimexdebug