Using memcached with Drupal and MAMP

I've recently needed to work with memcached on my MAMP setup. Normally for something like this, I'd consider setting up a Vagrant box and get the whole environment mirrored, but I wanted to see how hard it would be to make it work this way. Generally, it's not very difficult, but there are a couple snags you might run into.

First, the instructions on the Memcache module handbook are quite good, so the Drupal part isn't a big deal. But if you use MAMP, you can't just turn on memcached — you have to install your own memcached, libmemcached, and memcache extension for PHP. With homebrew, the first two are really easy. The third one has been tricky at times. There are older instructions telling you to compile it yourself from source, but now Nikola Majksner has come to your rescue with a repo on GitHub. With that, you get extensions that work with every version of PHP that ships with MAMP 3 (as of this writing). Install everything where they belong, start up memcached, restart MAMP, and you'll have memcached available in PHP. Great! You can check your admin/reports/status/php in your Drupal site to see the output phpinfo() and confirm that memcached is there.

Second, getting to that point was fairly easy, but when I tried using drush, I got a bunch of errors making it sound like it couldn't find memcached. WTF? There were several problems:

  1. You need to make sure that your command-line PHP is the same one as MAMP. This involves setting the DRUSH_PHP environment variable. In bash, that would look something like this:

declare -x DRUSH_PHP="/Applications/MAMP/bin/php/php5.3.28/bin/php"

I use fish, which is a little different:

set DRUSH_PHP /Applications/MAMP/bin/php/php5.3.28/bin/php

The end result is the same: once you've sourced your config file, or started a new shell, when you run drush status, you'll see that drush is using your MAMP-installed version of PHP.

  1. Running php from the command line, even the MAMP-installed one, doesn't guarantee that you're loading the same php.ini files (the configuration files where you define what extensions PHP should load, among other things). In my case, I needed to add the extension=memcached.so line to the php.ini file that drush could see. In my case, this one:

/Applications/MAMP/bin/php/php5.3.28/conf/php.ini

At that point, everything worked.

Categories: 
Comments are closed on this post to keep spammers at bay. If you want to chime in, please email or send a tweet.