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


Want advice on building better software? Join my mailing list…


Glen Scott

I’m a freelance software developer with 18 years’ professional experience in web development. I specialise in creating tailor-made, web-based systems that can help your business run like clockwork. I am the Managing Director of Yellow Square Development.

More Posts

Follow Me:
TwitterFacebookLinkedIn

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

  1. Achim Sperling

    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.

    Reply
    1. Glen Post author

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

      Reply
  2. Uri

    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

    Reply
  3. Mark

    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

    Reply
  4. Robbie Smith

    I did as you said, no install problems, even added the [apc] section to my php.ini file, i’ve rebooted apache at least 4 times on my mac and I still cannot see the apc config nor get the apc_ functions to work in php. I’m on XAMP. What else am I missing?

    -Robbie

    Reply
  5. Johncar

    Thanks a lot! it worked great after installing PECL to enable the installation of the APC extension using the normal PECL route:

    –> Installing PEAR and PECL:
    sudo php /usr/lib/php/install-pear-nozlib.phar
    pear config-set php_ini /private/etc/php.ini
    pecl config-set php_ini /private/etc/php.ini

    –> Installing APC:
    sudo pecl install apc

    John

    Reply
  6. Charley

    I had to use the following to get APC installed on my system:

    [path_to_php]/bin/phpize && CFLAGS=-m32 CPPFLAGS=-m32 CCASFLAGS=-m32 ./configure –with-php-config=[path_to_php]/bin/php-config && make

    Reply
  7. Myuing

    During pecl install:
    2 warnings and 6 errors generated.
    make: *** [apc_compile.lo] Error 1
    ERROR: `make’ failed

    …….

    great…. piece of useless shit
    Error this, error that… I had to find solutions for every single fucking line to run without a fistful of errors! It’s under Mavericks. What the fuck should one do to install a fucking extension? I have been fucking with this whole day long while I’ve installed 2 win-based + 3 linux systems without a hassle (each one of them with the same php version and modules)
    Pathetic

    Reply
    1. Glen Scott Post author

      I’ve just managed to successfully install APC on Mavericks 10.9.2 system by doing the following:

      * Install Apple’s Command Line Tools (https://developer.apple.com/downloads/index.action)
      * Download and install autoconf (http://ftp.gnu.org/gnu/autoconf/autoconf-latest.tar.gz)
      * Download and install PCRE (http://sourceforge.net/projects/pcre/files/latest/download?source=files)
      * Download and unpack APC package (http://pecl.php.net/get/APC)
      * Inside the APC package dir run the following commands
      * phpize
      * ./configure
      * make
      * sudo make install

      This installs the shared extension inside /usr/lib/php/extensions/no-debug-non-zts-20100525/

      Enable it by including the following line in your /etc/php.ini file:

      extension=apc.so

      Restart Apache, and bingo.

      Reply
  8. fred

    Hi Glen,

    I tried the same with Yosemite 10.10.2 (as you explained in your comment from March 21st 2014), but it doesn’t work. I get 2 warnings and 6 errors when executing the make command on APC.

    Reply
  9. Bhaskar

    Hi,

    I am not able to make apc work with High Sierra and PHP 7.1.14. I have done everything.

    Below is the output for php -i | grep apc

    PHP Warning: Method apc_store() cannot be a NULL function in Unknown on line 0
    PHP Warning: apc: Unable to register functions, unable to load in Unknown on line 0
    Warning: Method apc_store() cannot be a NULL function in Unknown on line 0
    Warning: apc: Unable to register functions, unable to load in Unknown on line 0
    apcu
    apc.coredump_unmap => Off => Off
    apc.enable_cli => On => On
    apc.enabled => On => On
    apc.entries_hint => 4096 => 4096
    apc.gc_ttl => 3600 => 3600
    apc.mmap_file_mask => no value => no value
    apc.preload_path => no value => no value
    apc.serializer => php => php
    apc.shm_segments => 1 => 1
    apc.shm_size => 64M => 64M
    apc.slam_defense => On => On
    apc.smart => 0 => 0
    apc.ttl => 7200 => 7200
    apc.use_request_time => On => On
    apc.writable => /tmp => /tmp

    And output of phpinfo() also shows apcu is enabled.

    But when I run my application, below error gets printed:

    [Fri May 25 18:43:30.523133 2018] [php7:error] [pid 1693] [client 10.146.5.127:54542] PHP Fatal error: Uncaught Error: Call to undefined function apc_store()

    This is the code snippet which adds data into the apc_store:

    public static function add(LoggedInUser $loggedInUser) {
    if (!is_null($loggedInUser) && !is_null($loggedInUser->getUserData()) &&
    !is_null($loggedInUser->getUserData()->userId)) {
    apc_store($loggedInUser->getUserData()->userId, $loggedInUser, 60);
    }
    }

    Can someone kindly help on this.

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.