Table of Contents

Installing memcache on Mac OS X & MAMP

Installing memcache under MAMP is an absolute pain in the rear. Here's the easiest way I've found to do it.

NOTE #1: I'm using Mac OS X 10.6.4 and MAMP Pro 1.8.3 for the install. The instructions will also work for newer versions, such as MAMP 1.9, but you'll need to change some paths, because MAMP 1.9 introduced PHP 5.2 and PHP 5.3 as options (rather than PHP 4.11 & PHP 5.2), and thus rejiggered its internal structure.

NOTE #2: You'll need to download more than 3GB of software to make this work; particularly the Apple XCode Developer Tools. There's no way around it, but if you're a developer you'll hopefully already have it on your machine.

Downloading Software

You need to download and install two things before you get started:

Installing the memcached daemon

There are two two parts to memcache you need to install: a daemon and a PHP client library. To install the daemon, use MacPorts via Terminal:

sudo port install memcached

Once memcached is installed, start it up:

sudo memcached -u root -d

Installing the PHP client library in MAMP

This is the hard part. In order for PHP to talk to memcache, it uses a PHP extension, which you must compile & install.

I've provided the memcache extension that I compiled; you may be able to drop this straight into MAMP's PHP extensions folder and then follow the instructions under Enable & Test memcache.so. However, YMMV; when in doubt you should compile from scratch.

memcache.so - 32&64 bit - v2.2.6 - PHP5.2

Building memcache.so

First, download the memcache sources. This is the easiest way:

pecl download memcache

Extact the source & enter the memache-2.2.6 dir:

tar xvfz memcache-2.2.6.tgz && cd memcache-2.2.6

MAMP doesn't include the PHP C header files, so you need to link them in from the Apple Developer SDK folder. Note that there is a MacOS X 10.5 SDK and a 10.6 SDK. If you're using PHP 5.2, you should use the 10.5 SDK, as OS X 10.5 came with PHP 5.2. For PHP 5.3, you should use the 10.6 SDK.

ln -s /Developer/SDKs/MacOSX10.5.sdk/usr/include/ /Applications/MAMP/bin/php5/include

Now, from the memcache-2.2.6 directory, run phpize to create the config file:

/Applications/MAMP/bin/php5/bin/phpize

Then run configure with this command:

MACOSX_DEPLOYMENT_TARGET=10.6 CFLAGS='-O3 -fno-common -arch i386 -arch x86_64' LDFLAGS='-O3 -arch i386 -arch x86_64' CXXFLAGS='-O3 -fno-common -arch i386 -arch x86_64' ./configure --with-php-config=/Applications/MAMP/bin/php5/bin/php-config

There's a couple things to note about that command: we're asking it to compile both i386 (32 bit) and x86_64 (64 bit) versions of the library, and we're using MAMP's PHP config to do it.

Once that's done, build the library:

make

Finally, copy the built library into your PHP extensions directory. Note that if you're building on MAMP 1.9 / PHP 5.3, these paths will probably be slightly different. The key part is to find where other PHP extensions are kept, and copy the new extension to that location.

cp modules/memcache.so /Applications/MAMP/bin/php5/lib/php/extensions/no-debug-non-zts-20060613/

Enable & Test memcache.so

Now, go into MAMP. From the File menu, choose 'Edit Template' and pick 'PHP5.ini'. Locate the listing of PHP extensions (search for extension=). Add the memcache extension onto the end of the extension list: extension=memcache.so

Stop & restart MAMP, to reload the new PHP5.ini file.

Click the 'Webstart' button in MAMP, and bring up the phpinfo page. Search the PHP configuration for 'memcache' and make sure the extension is listed. You should see the version information displayed (2.2.6) as well as a few configuration items.

That's it! Your memcached daemon is running, and MAMP can talk to it. Happy developing!