Setup a Memcached-Enabled MAMP Sandbox Environment

Memcached is a service that allows entire database tables to be stored in memory, drastically speeding up queries to those tables and alleviating database load. In Drupal, the Memcached module allows you to store all cache tables in memory.

This article will guide you through installing memcache on your Mac OS X sandbox machine, allowing you to test memcache deployments locally. For in-depth instructions on installing memcache on a production server, read the companion article on How to install Memcache on Debian Etch. This was installed on Mac OS 10.5.1 and MAMP 1.7 with PHP 5.2.5.

If you haven't already installed MAMP for your local sandbox environment, watch the Lullabot videocast on Installing a local web server on Mac OS X

Install the Memcached service on Mac OS X

  1. Install Xcode Tools from your Mac OS X DVD or download it from Apple Developer Connection to install the GNU Compiler Collection (gcc).
  2. Download the shell script from (mirror) to your home directory.
  3. Set the permissions on the shell script and run it as root.
    chmod 744
    sudo ./

    If the script fails with an error containing: error: C compiler cannot create executables, be sure Xcode is up-to-date and installed correctly.

  4. Per the instructions at the end of the script, add EVENT_NOKQUEUE=1 to your shell environment. You can do this by opening (or creating) the .bashrc or .profile file in your home directory and pasting the line at the bottom of the file.
  5. Check that memcache is installed properly by closing your previous Terminal window and opening a new Terminal session. Type the following command: memcached -h. You should get a screen similar to this:
    nate-macbook:~ nate$ memcached -h
    memcached 1.1.12
    -p <num>      port number to listen on
    -l <ip_addr>  interface to listen on, default is INDRR_ANY
    -d            run as a daemon
    -r            maximize core file limit
    -u <username> assume identity of <username> (only when run as root)
    -m <num>      max memory to use for items in megabytes, default is 64 MB
    -M            return error on memory exhausted (rather than removing items)
    -c <num>      max simultaneous connections, default is 1024
    -k            lock down all paged memory
    -v            verbose (print errors/warnings while in event loop)
    -vv           very verbose (also print client commands/reponses)
    -h            print this help and exit
    -i            print memcached and libevent license
    -P <file>     save PID in <file>, only used with -d option
  6. Cleanup your installation by deleting the created src directory and the file.

Install Memcache PHP Extension

Option 1: Download the Binary Memcache Extension
If you're running the (as of this writing) latest MAMP, you should have PHP 5.2.5 installed. If so, and you're on an Intel Mac, you can just download this compiled extension.

  1. Download PHP 5.2.5 (Intel)
  2. Install into the /Applications/MAMP/bin/php5/lib/php/extensions/no-debug-non-zts-[date] directory. The [date] value will vary depending on your MAMP install.

Option 2: Compile the PECL Memcache Extension
If you're running a different version of MAMP or are on a PowerPC machine, you'll need to compile a specific to you're configuration.

  1. Download the complete PHP source code from into /Applications/MAMP/bin/php5. You should download the version that matches your version of MAMP, which you can check at http://localhost:8888/MAMP/info.php.
  2. Create an include directory in the the php5 directory.
  3. Unzip/tar the php source archive and move it to /Applications/MAMP/bin/php5/include
  4. Rename the php source directory to just php (so then it will look like /Applications/MAMP/bin/php5/include/php)
  5. In Terminal use the following commands to compile the extension
    cd /Applications/MAMP/bin/php5/include/php
    cd /Applications/MAMP/bin/php5/bin
    ./pecl i memcache
  6. This creates a new directory in your /Applications/MAMP/bin/php5/lib/php/extensions directory. There should be two directories, each with different dates at the end of their file names such as no-debug-non-zts-20050922. Open the directory with today's date at the end and move the file to the other directory containing many other *.so files.
  7. Delete the now empty no-debug-non-zts-[date] directory that contained the file.

Enable the Memcache Extension

  1. Open the /Applications/MAMP/conf/php5/php.ini file and add in the ; Extensions section.
  2. Restart MAMP from the application or dashboard widget.
  3. Verify the Memcache extension is loaded by visiting http://localhost:8888/MAMP/info.php

Congratulations! You now have Memcache installed locally so you can easily test your memcache deployments. Installation and usage can then be setup on Drupal by following the instructions provided by the Memcache Project.

Bonus Section: Start and Stop Memcache with MAMP

So now that memcache is installed and available for MAMP to use, you'll need to actually start instances of the memcache daemon for your memcache-enabled site to use. Updating the scripts in /Applications/MAMP/bin with these copies will automatically start 11 instances of memcache for Drupal to use.

  1. Download each of the following scripts into /Applications/MAMP/bin, replacing existing and
  2. Set the permissions for the scripts to executable for the logged in user.
    chmod 775 *.sh
  3. Stop and start MAMP.

You can now setup your Drupal site to use ports 11211 to 11221 for memcache. Such as this (in settings.php):

$conf = array(
  'cache_inc' => 'sites/all/modules/contrib/memcache/',
  'memcache_servers' => array(
    'localhost:11211' => 'default',
    'localhost:11212' => 'content',
    'localhost:11213' => 'filter',
    'localhost:11214' => 'menu',
    'localhost:11215' => 'page',
    'localhost:11216' => 'views',
  'memcache_bins' => array(
    'cache' => 'default',
    'cache_content' => 'content',
    'cache_filter' => 'filter',
    'cache_menu' => 'menu',
    'cache_page' => 'page',
    'cache_views' => 'views',

This is not a substitute for instructions on setting up memcache.module, be sure to follow all the instructions in INSTALLATION.txt and README.txt included with memcache.module.

Get in touch with us

Tell us about your project or drop us a line. We'd love to hear from you!