Space Cat, Prince Among Thieves

Installing the PHP "memcached" Extension on OS X 10.9 Mavericks - macOS 10.12 Sierra

Please Note

This tutorial only covers the default PHP installation that ships with Mac OS X / macOS. If you have installed a new installation this does not cover you.

For those using brew, you can simply brew install the appropriate brew formula for the extension.

These directions are verified to work on the following:

  • Mac OS X 10.9 Mavericks
  • Mac OS X 10.10 Yosemite
  • Mac OS X 10.11 El Capitan
  • macOS 10.12 Sierra

Updated Instructions (Jan 2017):

This has been tested and confirmed for macOS Sierra.

OS X El Capitan / macOS Sierra

If you are running OS X El Capitan or macOS Sierra you will need to first turn off "System Integrity Protection" aka "rootless" for these directions to work.

To disable System Integrity Protection, boot into recovery mode by restarting and then holding `⌘R` as you hear the startup chime. Then start the Terminal from the Utilities menu.

Run the following command

$ csrutil disable

Then reboot. You are good to go.

If you wish to turn it back on, which you probably should when you are done, follow the instructions above but instead use:

$ csrutil enable

The first step is to install the latest and greatest Xcode.

Install the Xcode developer tools. Apple seems to have removed this option from the preferences so you'll need to handle this from the command line.

$ xcode-select --install

Next we will install the required dependencies. If you are not already using Homebrew you should be.

$ brew install wget autoconf pkg-config libmemcached

You will also want to make sure you have PEAR installed; instructions can be found here but can be summarized as follows:

$ wget http://pear.php.net/go-pear.phar
$ php go-pear.phar

Then in the /tmp directory we will create a folder we can work in.

$ cd /tmp
$ mkdir memcached-work
$ cd memcached-work

Then we will use pecl, part of pear, to fetch the current version of the extension. The version of the memcached extension will vary so you may need to update your paths accordingly.

$ pecl download memcached
$ open memcached-{{version}}.tgz
$ cd memcached-{{version}}/memcached-{{version}}
$ phpize
$ ./configure
$ make
$ sudo make install

Finally you will need to add the following line to your php.ini

extension = memcached.so

You can verify your installation with the following:

$ php --info | grep memcached\\.

Depending on your setup now you may want to restart apache.

$ sudo apachectl restart

You should be all set to go! Godspeed.


Comment by: liangyu on

liangyu GravatarHello, Jesse

as to the command --phpize, users may meet this problem like "Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script."

-- brew install autoconf
can fix this problem

Comment by: liangyu on

liangyu Gravatar--./configure --with-zlib-dir=/usr/local/Cellar/zlib/1.2.8

The terminal will complain it cannot find the path for libmemcached lib.
Didn't you meet this error?

Comment by: marvin on

marvin Gravatarclang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [memcached.la] Error 1

I am getting this error on 'make'

Comment by: jv on

jv Gravatardon’t forget:

brew install autoconf
brew install Libmemcached

then phpize etc.

Comment by: jv on

jv Gravatar@marvin, edit php_libmemcached_compat.h

add the following line at the end:

typedef const struct memcached_server_st *memcached_server_instance_st;

Comment by: Jared on

Jared GravatarJesse,

Thanks for the guide. I followed your guide and was successful on building memcahce on my localhost. However when I try to connect to a remote memcache server it crashes with the following error...any ideas?

donedyld: lazy symbol binding failed: Symbol not found: _mmc_queue_free
Referenced from: /usr/lib/php/extensions/no-debug-non-zts-20100525/memcache.so
Expected in: flat namespace

dyld: Symbol not found: _mmc_queue_free
Referenced from: /usr/lib/php/extensions/no-debug-non-zts-20100525/memcache.so
Expected in: flat namespace

Comment by: Jared on

Jared GravatarNever mind my question. It turned out that there were some other issues with my system causing this problem. Thanks again for the helpful post.

Comment by: will on

will GravatarI just get a bunch of compile errors when making after pecl download. I'd paste them here but the 6 errors and 24 warnings are a bit much, all of the errors go something like this though "unknown type name 'memcached_server_instance_st'; did you mean 'memcached_server_list_st'?" Which sounds like a very bad bug, but I'm guessing (hoping) that it has something to do with the version downloaded by pecl. Any thoughts? Anybody?

Comment by: Colin on

Colin GravatarHad the same issue as Will but the fix from jv got it installed for me. thanks jv!

Comment by: Sid on

Sid GravatarThanks, this was very helpful!

Comment by: Valentin on

Valentin Gravatarin a nutshell run in console:

xcode-select --install
brew install autoconf
brew install Libmemcached

cd /tmp
mkdir memcached-work
cd memcached-work

pecl download memcached
open memcached-2.1.0.tgz
cd memcached-2.1.0/memcached-2.1.0
phpize
./configure
make
sudo make install

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

to find which php.ini you're using run in console:
php --ini

Don't forget to restart apache:
sudo apachectl -k restart

Comment by: follower on

follower GravatarHi
I've done all this steps, restarted Apache.
php --ini says 'memcached support => enabled', etc. (too many lines).

But I still get error "Fatal error: Class 'Memcached' not found in ..."
What should I check?

Comment by: Jesse G. Donat on

Jesse G. Donat GravatarI'd probably ensure Apache is using the same copy of PHP as your command line. Then I'd ensure you have nothing in your apache config disabling it. Also, if you haven't tried rebooting I'd give that a go.

Comment by: follower on

follower GravatarYes, Apache uses another copy of PHP,
Of course I tried rebooting.
How can I check if nothing disabling it?

PHP config has included memcached extension ('/etc/php.ini' path is displaying in phpinfo() output)

Comment by: JB Manos on

JB Manos GravatarThere is a built-in PEAR installation on the mac. It's there, but goes more smoothly if you install Xcode command line tools.

xcode-select --install

then, run the PEAR installer on the built-in: sudo php -d detect_unicode=0 /usr/lib/php/install-pear-nozlib.phar

upgrade PEAR: sudo pear upgrade

then PECL: sudo pecl install -f ... (in this case, memcached)

The benefit to using this method is that you'll get a system-wide pear installation for the native PHP. Cheers!

Comment by: Rob Colburn on

Rob Colburn GravatarBrew now has a PHP memcached Extension

brew install php55-memcached
(replace with your version of brew php formulae)

http://braumeister.org/repos/josegonzalez/homebrew-php/formula/php55-memcached

Comment by: Jesse Donat on

Jesse Donat Gravatar@Rob, that only works if you've installed PHP with brew. My post is on installing it with the native PHP installation.

Comment by: MintyStark on

MintyStark GravatarThanks for the Post.
I think I got it 99% of the way there.
I installed it with my AMPPS PHP
/Applications/AMPPS/php-5.3/bin/phpize CFLAGS='-arch i386'
CFLAGS='-arch i386' ./configure --with-php-config=/Applications/AMPPS/php-5.3/bin/php-config
make
sudo make install
Everything installed ok.

PHP is showing class_exists('Memcached') as true, but Memcached is not displayed in phpinfo(). Also I get a crash when connecting to it.

Also my apache error log shows:
dyld: Symbol not found: _memcached_create
Referenced from: /Applications/AMPPS/php-5.3/lib/extensions/ext/memcached.so
Expected in: flat namespace

[Mon Nov 17 13:50:18.953115 2014] [core:notice] [pid 53238] AH00052: child pid 56107 exit signal Trace/BPT trap (5)

Any Thoughts?

Comment by: Martin on

Martin GravatarThanks for these instructions. When i run the xcode-select --install command i get the following error:-

xcode-select: error: no developer tools were found, and no install could be requested (perhaps no UI is present), please install manually from 'developer.apple.com'.

I however installed from the AppStore - is that ok?

Comment by: prabhat on

prabhat GravatarHow to install it for PHP CLI?

Please help

Comment by: Jesse G. Donat on

Jesse G. Donat GravatarNot sure exactly what you're asking, but I have a hunch that you might not be running the OS X built in PHP? If you run
which php
at the command line you *should* get
/usr/bin/php
- if you get something else you've installed a secondary copy of PHP. These directions will only work with OS X's built in copy of PHP I'm afraid.

Comment by: prabhat on

prabhat GravatarThanks
This command helped
pecl install memcache

Comment by: Ansuraj on

Ansuraj GravatarHi,

Even after following the above steps, I was unable to use memcached in my project, neither was I able to see it in phpinfo(). It was because memcached and php5-memcached extension was installed via cli.

I had to do the following:

sudo cp /usr/local/Cellar/php55/5.5.30/lib/php/extensions/no-debug-non-zts-20121212/memcache.so /usr/lib/php/extensions/no-debug-non-zts-20121212/memcache.so

sudo cp /usr/local/Cellar/php55/5.5.30/lib/php/extensions/no-debug-non-zts-20121212/memcached.so /usr/lib/php/extensions/no-debug-non-zts-20121212/memcached.so

Please change the above commands to suit your php-version paths.

Comment by: Jesse G. Donat on

Jesse G. Donat GravatarThis is because you are using an instance of PHP installed with Brew and not the base MacOS X version. My instructions only cover the base php installation.

You should actually not be following these directions and instead just install the extension with brew.

Email address will never be publicly visible.