by Nate Haug on January 25, 2008 // Short URL

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 topfunky.net (mirror) to your home directory.
  3. Set the permissions on the shell script and run it as root.
    chmod 744 install-memcached.sh
    sudo ./install-memcached.sh

    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 install-memcached.sh 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) memcache.so
  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 memcache.so specific to you're configuration.

  1. Download the complete PHP source code from php.net 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
    ./configure
    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 memache.so file to the other directory containing many other *.so files.
  7. Delete the now empty no-debug-non-zts-[date] directory that contained the memcache.so file.

Enable the Memcache Extension

  1. Open the /Applications/MAMP/conf/php5/php.ini file and add extension=memcache.so 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 startApache.sh and stopApache.sh.
  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):

<?php
$conf
= array(
 
'cache_inc' => 'sites/all/modules/contrib/memcache/memcache.db.inc',
 
'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.

Nate Haug

Senior Drupal Architect

Want Nate Haug to speak at your event? Contact us with the details and we’ll be in touch soon.

Comments

mysty

To get past Option 2 Step 5

This page is really a very handy resource indeed, thanks a lot for putting it together. I got through it - albeit pretty slowly and I thought to feedback my experience on the PPC / Option 2:
Steps 1 through 4 didn't seem to be necessary for me on a 10.5.1 G4 Powerbook with MAMP 1.7 (php 2.5.3).

1. The original source was slightly hidden - though I found it following the 5.2.5 DL link and changing 5.2.5 to 5.2.3 manually to http://uk3.php.net/get/php-5.2.3.tar.gz/from/uk.php.net/mirror
2. There was already an include directory in /Applications/MAMP/bin/php5
3,4. Within the /Applications/MAMP/bin/php5/include there was already a php directory
5. Also skipped

cd /Applications/MAMP/bin/php5/include/php
./configure

and went straight to
cd /Applications/MAMP/bin/php5/bin
./pecl i memcache

which eventually gave me

uild process completed successfully
Installing '/Applications/MAMP/bin/php5/lib/php/extensions/no-debug-non-zts-20060613/memcache.so'
install ok: channel://pecl.php.net/memcache-2.2.3
configuration option "php_ini" is not set to php.ini location
You should add "extension=memcache.so" to php.ini

YAY!! Reply

Ben

good tutorial; path problems

Thank you for the instructions. The script worked well. The only thing I had to add was add /usr/local/bin to my $PATH.

Reply

nate

Thanks GB Daddy

I've contacted the author of the blog and asked that the copied article be removed.

Reply

Paul Thompson

Config settings above

Why do you have

sites/all/modules/contrib/memcache/memcache.db.inc

and not

sites/all/modules/contrib/memcache/memcache.inc

in the settings.php config example above?

Reply

Robert Douglass

memcache.db.inc vs memcache.inc

The memcache.db.inc has fallback to the database if an item isn't found in the memcache. This is slightly less efficient in normal situations but much _more_ efficient if you have limited memcache memory or if your memcache daemons disappear.

Reply

Anonymous

at first glance, it looks

at first glance, it looks like the shell script is dated..
has anyone run it with
the latest libevent and memcache distributions

Reply

Eric Marden

If you use MacPorts...

If you are using MacPorts to maintain your local dev environment you can do:

sudo port install memcached

sudo pecl install memcache

sudo ln -s /opt/local/lib/php/extensions/no-debug-non-zts-20060613/memcache.so /opt/local/lib/php/extensions/memcache.so

Then add extension=memcached.so to your php.ini

You'll still need to do the drupal stuff, but this may make it easier to install if you're not using MAMP, but MacPorts.

Reply

Dave Edelhart

One note of caution...

I would advocate being a little less aggressive about swapping out the start and stop scripts, as noted in the bonus section; at the very least, back up the existing start and stop scripts; better yet, use a text editor to blend in the downloaded start and stop scripts using a text editor.

The replacement scripts will probably work for most people, but if, for instance, your system tech has also augmented those scripts, or the MAMP stack changes, you might end up permanently disabling the system by replacing working code with nonworking code.

Reply

Anonymous

Windows Platform

Thanks alot Nate Haug for posting this article but it would be very handy if you can make one article for the windows version that is how to install memcache on windows a step by step tutorial exaclty like the above article "Setup a Memcached-Enabled MAMP Sandbox Environment".

Most of the Drupal sites are running on the Windows platform.So It would be great help for all those people how want to make there site run faster.

Reply

primeminister

Great! Helped a lot

This was great. Using it on a production server but not my local webserver annoyed me a lot. Could not test very good. Now I can! thnx!

Reply

Colin

Stopping/Starting with MAMP issue

Thanks for the tutorial!

One thing I experienced, re: start/stopping with MAMP. I found I was getting Connection Refused (61) errors in PHP, which were due to the memcached process not running at all.

I needed to make the following modifications to the startMemcached.sh script:
/usr/local/bin/memcached -m 24 -p 11211 -u user -d
1. The full path needed to be added (alternately probably could do this with $PATH)
2. Since MAMP was starting/stopping things as root, the -u user had to be added to tell it what user to impersonate (memcached won't run as root). Obviously "user" should be replaced with an actual user name...

Just my experience, cheers...

Reply

Erik Giberti

Indispensable Resource

I'm hardly new to memcached, but I've never bothered to get it running on my local system. I've just posted what changes I needed to make from your instructions to get it running on the default build of Apache and PHP version under OS X 10.5.5.

http://af-design.com/blog/2008/10/18/memcached-with-php-on-mac-os-x/

One important change I would recommend in your startMemcached.sh script is including an interface address of 127.0.0.1 so that folks can't port scan a machine on an insecure network (think Starbucks) and potentially muck things up.

Thanks for putting this together. It is a very valuable resource.

Reply

Craig Harman

Does kqueue really need to be disabled on OS X?

Most of the memcached on OS X HOWTO's I have seen repeat the instructions from the topfunky.net shell script to set the environment variable EVENT_NOKQUEUE to '1' or 'yes'. But the source code I found on this page (by Googling for "leopard" and "EVENT_NOKQUEUE"):

http://archives.seul.org/or/cvs/Apr-2007/msg00083.html

suggest that the kqueue problem only affects PowerPC based Macs. I am running 10.5.5 on an Intel machine, have not disabled kqueue through the environment variable, and (so far) everything is working perfectly. I suspect the instructions to disable kqueue are vestigial.

Reply

Nicholas

Memcache Installation problem

Memcache Installation problem... Please i need somebody to help me out better on my server. please anybody can help me out with this am a novice and am seriously having problem with it... Cpanel Details will be mailed to any body for assistance.. please help me.

Reply

scott

getting memcache.so to work with 'MAMP PRO' (as opposed to MAMP)

It took me quite a while to figure out why I couldn't get the memcache.so extension to load using MAMP PRO instead of MAMP. I finally realized that each time MAMP PRO was stopped and restarted, it reset the PHP.INI file back to some default which did not include the 'extension=memcache.so' line. Searching for why that might be happening, I came across this webpage:

http://www.adoptioncurve.net/archives/2007/10/tweaking-mamp-pro-settings...

It was there that I discovered that MAMP PRO keeps a hidden file called 'php5.ini' inside of it's application package. As the site above prescribes, I right-clicked on the 'MAMP PRO.app' application icon in the finder, selected "Show Package Contents" and inside the package, there is a folder named "Resources". Inside that folder is a file named 'php5.ini' (and 'php4.ini' if you happen to be kickin' it oldschool). I added the extension declaration to that file and voila! It appeared in the php.ini file utilized by MAMP PRO and it now works. There are a number of configuration files stored in that package, so it might be worth poking around a little.

Reply

btully

MAMP PRO is a no-go on startup/stop

Hey there Scott. Thanks for the heads up on the hidden php5.ini in MAMP PRO's package contents. Not sure why editing the template doesn't stick -- a bug perhaps.

However I still find I have to manually run the memcached start/stop scripts from Terminal.app. I've even updated the MAMP.startup script within MAMP PRO's package contents so that the code of the start and stop scripts are added in the relevant locations. Restarted MAMP PRO with Activity Monitor open and no matter how many times I run the start/stop function, memcached doesn't get started.

Was there anything else you did to get it running automatically through MAMP PRO or are you running it manually?

Reply

Libok Zhou

Thank you all

@Nate Haug
Thank you very much for your detailed and good instructions.

@Scott
Wow, I sucked hours with the same problem in MAMP PRO that memcache not loaded in phpinfo().
At last, I found your comment. Oh god, it works now.
Very appreciated your comment.

Reply

Anonymous

Thanks

Wow, great instructions!

Reply

Robert Garrigos

great article but not right for MAMP PRO

I've found that MAMP PRO is not using the .sh files found in /Applications/MAMP/bin, thus memcached is not started when Mamp Pro starts Apache. I can run those .sh file within the terminal and memcached works great, but I wish Mamp Pro could do so by it self. Any one knows how to do so?

Reply

Prasad

Thanks

Great tutorial and worked just like that. Thanks.

Reply

Anonymous

Error...

Hi,

No matter what I do I keep getting the same error in my PHP log...
Unable to load dynamic library '/Applications/MAMP/bin/php5/lib/php/extensions/no-debug-non-zts-20060613/memcache.so' - (null) in Unknown on line 0
I've read that's because I'm using the wrong version of PHP, but my MAMP settings say 5.2.11, while Snow Leopard apparently natively has 5.3 installed. I've tried both to absolutely no avail... any ideas?

Thanks

Reply

Bill Van Pelt

Any Resolution?

Hello,

I am getting the same thing when compile for both 5.3 and 5.2.11. Did you find any resolution yet?

Thanks,
Bill

Reply

John

MAMP Pro Changes

If you are using MAMP PRO then you should edit the php.ini template and not the file, as this gets overwritten every time Apache starts.

  1. Stop the server
  2. Click File > Edit Template > PHP5 php.ini
  3. Click accept to the warnings
  4. Append the line extension=memcache.so below the ; Extensions line and save
Reply