oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

PHP's PEAR on Mac OS X

by Jason Perkins

The PHP Extension and Application Repository (PEAR) is an online repository of high-quality, peer-reviewed PHP classes that conform to a rigorous coding standard. Started in 1999, PEAR was until recently only available via its CVS repository. In order to download and install the packages it contained, developers needed to master a somewhat arcane set of commands, often copied and pasted from a FAQ, and usually assuming some level of familiarity with CVS in general.

The release of the PEAR Package Manager has changed all of that. In terms of functionality, it's similar to Perl's CPAN or Fink for Mac OS X in that using this command line utility you can browse, install, update, and remove PHP packages from a central, remote PEAR server to your local system. Installing, configuring, and using the PEAR Package Manager on OS X 10.2 is the focus of this article.

Laying the Groundwork

First, make certain that you've installed Apple's 2002-08-23 Security Update by running the Software Update application in your computer's System Preferences. Apple has updated at least one library that's required for this installation to work. Next you'll need to install the PHP CGI binary before you can run the PEAR Package Manager's Command Line Installer. Execute the following from a new window in Terminal to download and install the PHP CGI binary:

% curl -O 
% gunzip php-cgi-4.1.1.gz 
% sudo mkdir /usr/local/bin 
% sudo mv ./php-cgi-4.1.1 /usr/local/bin/php 
% sudo chmod 755 /usr/local/bin/php

To test the install, at the prompt enter:

% php -v

You should be rewarded with the current version of the PHP CGI binary that you just installed. If you see a message indicating that it wasn't found, make certain that /usr/local/bin is in your path by entering at the command line:

% echo $PATH

That command will generate output that looks something similar to:


Each directory in the path is separated from the other directories by a colon. If you don't see an entry for /usr/local/bin listed in the output, then you'll need to identify your specific shell to modify its path. From the command line, enter:

% echo $SHELL

If /bin/tcsh is output, then execute these commands:

% echo 'set path = ($path /usr/local/bin)' >> ~/.cshrc 
% source ~/.cshrc

If /bin/bash is output from the echo $SHELL command then enter:

% echo 'PATH=$PATH:usr/local/bin' >> ~/.bash_profile 
% source .bash_profile

When a shell is initially launched, it looks for a configuration file in the home directory of the user that launched it. For tcsh this is the .cshrc file, and for bash it's the .bash_profile file. Within that file you can place several different types of configuration information, initialize new variables, or modify existing variables that the shell will use. We're interesting in modifying the $PATH variable.

As explained earlier, the $PATH variable is a colon delimited list of directories that the shell will search through for a command when you invoke that command from the shell. For instance, you know that ls will list the current directories contents.

Here's how this happens. When you type ls and press enter, the shell begins searching the directories specified in the $PATH variable (in the order in which they're specified in the variable) for the ls command. When it finds the ls command it stop searching and executes that command.

Related Reading

Learning Unix for Mac OS X
By Dave Taylor, Brian Jepson

We need to modify the $PATH so that we can enter php from the command line and have the shell able to locate and run the PHP binary. To accomplish this, we need to append the /usr/local/bin directory to the $PATH. That appending is what the first line in both cases accomplishes. Note that we said that the configuration file is read when the shell initially starts so our appending of the $PATH normally wouldn't take place until we launched a new shell.

The source command lets us get around that by causing the shell to read the specified configuration file at runtime. With those two commands we've modified the $PATH -- from now on when we launch a shell it will be able to find the PHP command in /usr/local/bin -- and we've caused the currently running shell to read its configuration file again. Now we can proceed with the current shell able to find and run the PHP binary.

I covered the two most common shells used; if you use an alternate such as zsh, then you'll need to check its documentation and add /usr/local/bin to its path. At this point, we've finished laying the groundwork and can start the installation of the PEAR Package Manager.

Installing and Configuring the PEAR Package Manager

To begin the installation of the PEAR Package Manager, enter the following at the prompt in Terminal:

% curl | php

Note: If you're at all concerned about security, be sure to read the source of that URL first before executing it.

This command starts curl, has it access, and pipes the source of that page (which is a PHP script) to the binary PHP executable that you just installed. As the installer runs, you'll be asked a series of questions by the script in preparation for the actual installation. First, you'll be asked to confirm that you want to install the PEAR command: press Enter to continue with the install. Second, you'll be asked for configuration details for a proxy if one exists. Third, you'll be asked to review the suggested file layout for the PEAR install. We're going to modify that section of the install.

The initial, default install of PHP on Mac OS X places the PEAR packages in /System/Library/PHP/PEAR. Placing the packages retrieved with the PEAR Package Manager there would entail either changing the permissions of that directory or running the Package Manager via sudo. For security reasons, neither of these options is attractive. We'll specify that the PEAR packages should be installed in the /usr/local directory that we established earlier. When the installer displays the directory scheme that it will use, modify it so that it matches the following:

  • Installation Prefix set to /usr/local
  • Binaries Directory set to $prefix/bin
  • PHP Code Directory set to $prefix/share/pear

Finally, you'll be asked to confirm that you want to install the packages that are bundled with the PEAR Package Manager; select yes for this option. With the setup questions behind us, the installer will run for a bit, creating the subdirectories that you specified if they don't already exist, downloading and installing the Package Manager and the additional packages that it requires. When it's finished, you'll be rewarded with:

The 'pear' command is now at your service at /usr/local/bin/pear Run it without parameters to see the available actions, try 'pear list' to see what packages are installed, or 'pear help' for help.

At this point, the installation is complete. For your local PHP install to make use of the local PEAR repository and the packages that you install there, you'll need to create or edit the file /usr/local/lib/php.ini and add (or modify) the include path to tell PHP the location of the new PEAR directory. First open the file in BBEdit (or your editor of choice) with:

% bbedit -c /usr/local/lib/php.ini

and modify (or add) the include_path variable to include the /usr/local/share/pear directory:

include_path = ".:/usr/local/share/pear";

When PHP processes a script and encounters an include statement, it will search the directories specified in the include_path for the included file. Each directory in the include_path is separated from the others by a colon. In UNIX parlance, the current directory is represented with a single period. Paraphrased, that include_path means: When PHP encounters an include, it should first look in the current directory for the specified file. If it's not in the current directory, then it should look in the /usr/local/share/pear directory for the specified file. Although I used the include statement for demonstrative purposes, this applies equally to the require statement.

If you haven't saved your modified php.ini, do so now. Congratulations, you now have the PEAR Package Manager installed and configured on your system. Now we'll discuss its use.

Pages: 1, 2

Next Pagearrow