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

The APC extension should be a straightforward PECL install, but sadly it does not work on Mac OS X Lion. Attempting to install will result in make errors similar to the following:

In file included from /private/tmp/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 /private/tmp/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’
/private/tmp/APC/apc.c:393: error: expected specifier-qualifier-list before ‘pcre’
/private/tmp/APC/apc.c: In function ‘apc_regex_compile_array’:
/private/tmp/APC/apc.c:454: error: ‘apc_regex’ has no member named ‘preg’
/private/tmp/APC/apc.c:454: error: ‘apc_regex’ has no member named ‘preg’
/private/tmp/APC/apc.c:455: error: ‘apc_regex’ has no member named ‘nreg’
/private/tmp/APC/apc.c:455: error: ‘apc_regex’ has no member named ‘nreg’
/private/tmp/APC/apc.c: In function ‘apc_regex_match_array’:
/private/tmp/APC/apc.c:487: error: ‘apc_regex’ has no member named ‘preg’
/private/tmp/APC/apc.c:487: error: ‘apc_regex’ has no member named ‘preg’
/private/tmp/APC/apc.c:488: error: ‘apc_regex’ has no member named ‘nreg’
/private/tmp/APC/apc.c:488: error: ‘apc_regex’ has no member named ‘nreg’
make: *** [apc.lo] Error 1
ERROR: `make' failed

Thankfully, the solution is simple. To being with, make sure you have Xcode installed. If you have xCode 4.3 or above, make sure you have the command line tools installed.

APC requires PCRE libraries and header files to be available, so we can download and install these from source:

./configure
make
sudo make install

Next, we can install the APC extension using the normal PECL route:

sudo pecl install apc

Confirm that you have the following line in your php.ini file:

extension=apc.so

Restart Apache, if required:

sudo apachectl graceful

You should now have APC available, and you can confirm this by looking at your phpinfo output:

APC PHP info

Comments

  1. Achim Sperling says:

    Thanks for this short install howto. I was running in a lot of errors along this short-trip anyways and wanted to drop a note here.

    First I had no C-compiler command line tool.
    configure: error: no acceptable C compiler found in $PATH
    To solve this I installed XCode 4 through the AppStore. In the preferences dialog you'll find the tab "Downloads". Go ahead and install the command line tools.

    Then there was no autoconf when trying to install APC.
    Cannot find autoconf. Please check your autoconf installation and the
    $PHP_AUTOCONF environment variable. Then, rerun this script.

    My solution to this one was:
    brew install autoconf

    Well, it was getting better and better. XCode 4 brought me a new git version, a
    brew update
    That I had to do before resulted in an error:
    Error: Failed while executing git pull http://github.com/mxcl/homebrew.git master
    Found a solution as well:
    cd `brew --prefix`
    git fetch https://github.com/mxcl/homebrew.git
    git reset --hard FETCH_HEAD

    This does an update as well, just not using the brew update script.
    Finally I did:
    brew install autoconf
    sudo pecl install apc

    I had to add the extension to php.ini manually, but now it's working.

    Hopefully this is going to save some of you some time.

  2. Glen says:

    Thanks for the information. I've added a note to the post about needing the xCode command line tools for compilation.

  3. Uri says:

    Hi!
    Great post, very helpful..

    Maybe it will help to add a check for the existence of PCRE on the computer and the version before installing it

  4. Rob Ganly says:

    I was having the same error on OSX Lion until I found your article, good work and thanks!

    Rob Ganly

  5. Mark says:

    As an alternative, brew install apc works too.

    Follow the instructions to add to php.ini and restart apache.

    [apc]
    extension="/usr/local/Cellar/apc/3.1.9/apc.so"
    apc.enabled=1
    apc.shm_segments=1
    apc.shm_size=64M
    apc.ttl=7200
    apc.user_ttl=7200
    apc.num_files_hint=1024
    apc.mmap_file_mask=/tmp/apc.3.1.9
    apc.enable_cli=1

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>