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:

  • Apple XCode Developer Tools - This is a large download (about 3.5GB); you'll want to do a default install once its downloaded.
  • MacPorts - Be sure to get the right version for your OS on the download page; this lets you install a bunch of Unix tools (such as the memcached daemon) on OS X.

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!

Discussion

ling, Feb 27, 2011 07:05 AM

Thank you very much I would never have found it myself ;)

Here is what I did to have it run on MAMP 1.9.4 (using php5.3)

with MacOSX 10.6.6

(just corrected some paths)

pecl download memcache

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

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

/Applications/MAMP/bin/php5.3/bin/phpize

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.3/bin/php-config

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

Hope this saves someone's time.

Thank you so much..., Mar 19, 2011 02:07 PM

Thanks so much for this…

Worked brilliantly… One small thing you have the wrong 'dash' '-' right after the first ”./configure”, simple thing to find and fix.

But again - thank you so much, spend hours trying to get this working…

Alex De Winne, Apr 10, 2011 01:54 AM

Also, remember to check to make sure the execute flag is set on phpize an php-config. You may need to run these commands with sudo.

Tom Lancaster, Jul 29, 2011 07:20 AM

Thanks for this. I was beginning to despair of getting it to work on my Macbook,

lan_chi, Feb 17, 2012 01:52 PM

PHP Warning: PHP Startup: Unable to load dynamic library '/Applications/MAMP/bin/php5.3/lib/php/extensions/no-debug-non-zts-20090626/memcache.so' - dlopen(/Applications/MAMP/bin/php5.3/lib/php/extensions/no-debug-non-zts-20090626/memcache.so, 9): no suitable image found. Did find:

/Applications/MAMP/bin/php5.3/lib/php/extensions/no-debug-non-zts-20090626/memcache.so: mach-o, but wrong architecture in Unknown on line 0
Jason, Mar 10, 2012 02:42 AM

I was getting the error
-bash: /Applications/MAMP/bin/php5.3/bin/phpize: Permission denied

and when using sudo
sudo: /Applications/MAMP/bin/php5.3/bin/phpize: command not found

Alex alluded to this issue, but what you have to do is make the bin files executeable
sudo chmod a+x /Applications/MAMP/bin/php5.3/bin/*

Also, if you're using Mac OS X 10.7, then adjust the deployment target to 10.7 rather than 10.5 or 10.6

Padmapriya, Jun 1, 2012 04:53 PM

Thanks a million. I would have never figured out myself!
Great tutorial!!

Priya

server-tech/macosx/install-memcache-on-mamp.txt · Last modified: Feb 18, 2011 11:32 PM by dordal
© 2005-10 David Ordal / DO IT, LLC. All Rights Reserved.