How to install PEAR, PHPUnit, and XDebug on Mac OS X 10.6 Snow Leopard

Published on Author Code Father

Mac OS X 10.6 Snow Leopard on my Apple Macbook — my primary web development machine. By default, Mac OS X 10.6 includes PHP 5.3.0 and Apache 2.2.11. Unfortunately, a default Mac OS X 10.6 install does not include more advanced tools for testing and debugging PHP applications. This tutorial demonstrates how to install PEAR, PHPUnit, and XDebug on Mac OS X 10.6 Snow Leopard.

Turn on PHP

After upgrading to Snow Leopard, PHP may be disabled. To activate PHP, you need to load the PHP 5 module in the Apache configuration file. Open /etc/apache2/httpd.conf file with vi or TextMate. Locate this line (line 115 for me):

#LoadModule php5_module        libexec/apache2/libphp5.so

Remove the ”#” from the beginning of the line so that it looks like this:

LoadModule php5_module        libexec/apache2/libphp5.so

Save httpd.conf. Now restart the Apache web server. Open System Preferences and click on Sharing. Uncheck Web Sharing, then re-check Web Sharing. This restarts the Apache web server and loads the newly activated PHP 5 module. To ensure PHP is working, create a new text file in ~/Sites/ namedindex.php. This file will have the following contents:

<?php phpinfo(); ?>

When you view http://localhost/~[your_user_name]/index.php in a Web browser, you should see a lot of information about your PHP installation. This means PHP and Apache are now working. If you do not see a lot of information, you need to review the previous steps. At this point, I assume Apache and PHP are working.

Install PEAR and PHPUnit

PHPUnit, a member of the xUnit family of testing frameworks, provides an easy-to-use framework for testing and analyzing your PHP applications. PHPUnit recommends that you install using PEAR. Unfortunately, when I attempted to install PHPUnit using the default PEAR install on Mac OS X 10.6, I was told PEAR was too old and must be upgraded before I could install PHPUnit.

Install PEAR

Instead of upgrading Mac OS X’s PEAR install, I install my own copy in /usr/local/. Open/Applications/Utilities/Terminal.app and enter this command:

$ cd /usr/local

Next, we begin the PEAR installation process. Enter this command into the Terminal:

$ curl http://pear.php.net/go-pear | sudo php

Enter your administrator password if prompted and answer any questions that follow. You should be okay if you accept the default answers for each question. When the script finishes, PEAR should be installed in/usr/local/bin/. The PEAR library should be accessible at /usr/local/PEAR/.

Update system PATH

Now we need to add our custom pear install to our system PATH. Create or edit your bash profile in vi(or TextMate):

$ vi ~/.bash_profile

Ensure this file includes the following line of text:

PATH=/usr/local/bin:$PATH

Save the file and restart the Terminal application for this change to take effect. Next, we should verify thatpear works. Run this command in the Terminal:

which pear

This command should answer ”/usr/local/bin/pear”.

Update PHP include path

Now we need to tell PHP where our PEAR library is located by adding pear to the PHP include path in the/etc/php.ini file. This file does not exist by default on Mac OS X 10.6. To create this file, run the following command in the Terminal:

$ sudo cp /etc/php.ini.default /etc/php.ini

Next, we need to edit /etc/php.ini file in vi or TextMate and update the PHP include path. Locate the following line in /etc/php.ini:

;include_path = ”/php/includes”

Remove the ”;” from the beginning of the line and add the PEAR library path.

include_path = ”/usr/local/PEAR:/php/includes

Save /etc/php.ini. Restart the Apache web server by unchecking and rechecking System Preferences > Sharing > Web Sharing. Next, view http://localhost/~[your_user_name]/index.php in a web browser (you created this file earlier). Search for “include_path” and verify the path now includes ”/usr/local/PEAR”. PEAR is now installed.

Install PHPUnit

Next, we install PHPUnit. This is the easy part. Run this command in the Terminal:

$ sudo pear channel-discover pear.phpunit.de

Next, run this command in the Terminal:

$ sudo pear install phpunit/PHPUnit

That’s it. PHPUnit is installed and located at /usr/local/PEAR/PHPUnit/. You can now include PHPUnit in your PHP scripts with this line:

require_once 'PHPUnit/Framework.php';

Install XDebug

XDebug is an advanced debugging and profiling tool for PHP. It provides stack and function traces, memory allocation, profiling, code coverage analysis, and more. Instead of compiling XDebug from source, I will use a pre-compiled PHP extension from ActiveState. Go to this url in a Web browser:

http://aspn.activestate.com/ASPN/Downloads/Komodo/RemoteDebugging

Download the PHP Remote Debugging file for Mac OS X. Unarchive the downloaded file, and you should see a new folder like this:

Mac OS X Screenshot of xDebug extension folder

Enter the “5.3” directory and find the file named “xdebug.so”. You need to move this file to the/usr/lib/php/extensions/no-debug-non-zts-20090626 directory. First, open a new Finder window and pressCmd + Shift + G. Enter /usr/lib/php/extensions/no-debug-non-zts-20090626/ into the prompt that appears and press “Go”. This will open the destination directory in the Finder window. Drag xdebug.so into the destination directory. You may be prompted for your administrator password. If the/usr/lib/php/extensions/no-debug-non-zts-20090626/ directory does not exist, locate the/usr/lib/php/extensions/ directory instead. This directory will contain a directory named something similar to no-debug-non-zts-20090626. Place xdebug.so into that directory instead.

Finally, we need to tell PHP to use the xdebug.so extension. Edit /etc/php.ini in vi or Textmate. Append the following lines to the bottom of the file:

	[xdebug]
	zend_extension=/usr/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so

Ensure the path to xdebug.so is correct. Save the file and restart the Apache web server by unchecking and rechecking System Preferences > Sharing > Web Sharing. View http://localhost/~[your_user_name]/index.php in a web browser. Search for “Xdebug”. If you find the xDebug section, XDebug is installed.

Conclusion

You now have an advanced PHP installation running on Mac OS X 10.6 Snow Leopard with PHP 5.3.0, PEAR, PHPUnit, and XDebug. Post any questions or comments below.

Comments

comments

Categories PHP