Installing the Alternative PHP Cache (APC) on Mac OS X

by Doug Sparling

I recently needed to install APC for a Magento project I was working on. I develop on Mac OS X (Snow Leopard), and while APC is generally a simple PECL install (or homebrew), it doesn’t work on OS X:

doug@scriptrunner:~$ pecl install apc

(Lots of verbose output eliminated for brevity)

running: make
/bin/sh /private/var/tmp/pear-build-doug/APC-3.1.9/libtool --mode=compile cc -I. -I/Users/doug/Documents/Projects/edu/php_unit/temp/APC -DPHP_ATOM_INC -I/private/var/tmp/pear-build-doug/APC-3.1.9/include -I/private/var/tmp/pear-build-doug/APC-3.1.9/main -I/Users/doug/Documents/Projects/edu/php_unit/temp/APC -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /Users/doug/Documents/Projects/edu/php_unit/temp/APC/apc.c -o apc.lo
mkdir .libs
cc -I. -I/Users/doug/Documents/Projects/edu/php_unit/temp/APC -DPHP_ATOM_INC -I/private/var/tmp/pear-build-doug/APC-3.1.9/include -I/private/var/tmp/pear-build-doug/APC-3.1.9/main -I/Users/doug/Documents/Projects/edu/php_unit/temp/APC -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /Users/doug/Documents/Projects/edu/php_unit/temp/APC/apc.c -fno-common -DPIC -o .libs/apc.o
In file included from /Users/doug/Documents/Projects/edu/php_unit/temp/APC/apc.c:44:
/usr/include/php/ext/pcre/php_pcre.h:29:18: error: pcre.h: No such file or directory
In file included from /Users/doug/Documents/Projects/edu/php_unit/temp/APC/apc.c:44:
/usr/include/php/ext/pcre/php_pcre.h:37: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
/usr/include/php/ext/pcre/php_pcre.h:38: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
/usr/include/php/ext/pcre/php_pcre.h:44: error: expected specifier-qualifier-list before ‘pcre’
/Users/doug/Documents/Projects/edu/php_unit/temp/APC/apc.c:393: error: expected specifier-qualifier-list before ‘pcre’
/Users/doug/Documents/Projects/edu/php_unit/temp/APC/apc.c: In function ‘apc_regex_compile_array’:
/Users/doug/Documents/Projects/edu/php_unit/temp/APC/apc.c:454: error: ‘apc_regex’ has no member named ‘preg’
/Users/doug/Documents/Projects/edu/php_unit/temp/APC/apc.c:454: error: ‘apc_regex’ has no member named ‘preg’
/Users/doug/Documents/Projects/edu/php_unit/temp/APC/apc.c:455: error: ‘apc_regex’ has no member named ‘nreg’
/Users/doug/Documents/Projects/edu/php_unit/temp/APC/apc.c:455: error: ‘apc_regex’ has no member named ‘nreg’
/Users/doug/Documents/Projects/edu/php_unit/temp/APC/apc.c: In function ‘apc_regex_match_array’:
/Users/doug/Documents/Projects/edu/php_unit/temp/APC/apc.c:487: error: ‘apc_regex’ has no member named ‘preg’
/Users/doug/Documents/Projects/edu/php_unit/temp/APC/apc.c:487: error: ‘apc_regex’ has no member named ‘preg’
/Users/doug/Documents/Projects/edu/php_unit/temp/APC/apc.c:488: error: ‘apc_regex’ has no member named ‘nreg’
/Users/doug/Documents/Projects/edu/php_unit/temp/APC/apc.c:488: error: ‘apc_regex’ has no member named ‘nreg’
make: *** [apc.lo] Error 1
ERROR: `make' failed

The issue is that APC is expecting the PCRE headers and libraries to be installed. Fortunately, no big deal. Just head over to:

ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/

and grab the latest release. Or here to simply get the latest:

http://sourceforge.net/projects/pcre/files/latest/download?source=files

At the time of writing, pcre-8.30.tar.gz was the latest tarball. Nothing new here:

$ tar zxvf pcre-8.30.tar.gz
$ cd pcre-8.30 $ ./configure $ make $ sudo make install

Now you should be able to do the normal PECL install:

$ sudo pecl install apc

Or if you feel so inclined, go ahead and install from source. Grab the tarball here (which happened to be APC-3.1.9.tgz at time of writing):

http://pecl.php.net/get/APC

Once again, nothing new here (other than phpsize, perhaps):

$ tar zxvf APC-3.1.9.tgz
$ cd APC-3.19
$ phpize
$ ./configure
$ make
$ sudo make install

You now need to modify php.ini and add the following line (under Dynamic Extensions if you like) so the apc extension will get loaded at run time:

extension=/usr/lib/php/extensions/no-debug-non-zts-20090626/apc.so

Now restart apache and you should be good to go.

One more thing (and there always is). APC-3.1.9 has a bug, and you may see this after you’ve restarted Apache:

Fatal error: Unknown: apc_fcntl_unlock failed: in Unknown on line 0

However, another easy fix. Update apc_lock.h (in the APC directory) and modify this line (line 157 in my case).

Change:

# define apc_lck_rdunlock(a) apc_fcntl_unlock(&a TSRMLS_CC)

to

# define apc_lck_rdunlock(a) apc_fcntl_unlock(a TSRMLS_CC)

and then you’ll need to recompile apc again.

$ cd APC-3.19
$ make clean
$ phpize
$ ./configure
$ make
$ sudo make install

Then restart Apache, and voilà, you should be good to go.