Space Cat, Prince Among Thieves

Installing the PHP "memcached" Extension

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 with Mac OS X 10.9 Mavericks through macOS 10.14 Mojave.

System Integrity Protection

If you are running OS X El Capitan or newer you will need to disable System Integrity Protection to modify system files and directories.

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, follow the instructions above but instead use:

$ csrutil enable
Click here for a full post on the topic.

The first step is to install the latest version of Xcode. On modern versions of MacOS this can be done from the App Store.

After this, install the Xcode developer tools.

$ xcode-select --install

Special note to Mojave users if the following steps fail, you may need to reinstall your Xcode header files. Instructions for that can be found here.

Next we will need to install the required dependencies before we can build the extension. 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; full 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

Next 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 will need to update the 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!


Comment by: liangyu on

liangyu's Gravatar Hello, 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's 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's Gravatar clang: 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's Gravatar don’t forget:

brew install autoconf
brew install Libmemcached

then phpize etc.

Comment by: jv on

jv's 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's Gravatar Jesse,

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's Gravatar Never 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's Gravatar I 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's Gravatar Had the same issue as Will but the fix from jv got it installed for me. thanks jv!

Comment by: Sid on

Sid's Gravatar Thanks, this was very helpful!

Comment by: Valentin on

Valentin's Gravatar in 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's Gravatar Hi
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's Gravatar I'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's Gravatar Yes, 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's Gravatar There 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's Gravatar Brew 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 G. Donat on

Jesse G. Donat's 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's Gravatar Thanks 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's Gravatar Thanks 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's Gravatar How to install it for PHP CLI?

Please help

Comment by: Jesse G. Donat on

Jesse G. Donat's Gravatar Not 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's Gravatar Thanks
This command helped
pecl install memcache

Comment by: Ansuraj on

Ansuraj's Gravatar Hi,

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's Gravatar This 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.

Comment by: olidev on

olidev's Gravatar Nicely explained. However, I still prefer Debian for my PHP apps as it is easy to setup. Configuring memcached with PHP on Debian is also really easy as explained here: https://www.cloudways.com/blog/memcached-with-php/

Comment by: Charlotte on

Charlotte's Gravatar Hi! I was almost done with all steps but I got stuck when I encountered an error on make.

I got this error:

./php_memcached.h:23:10: fatal error: 'Zend/zend_smart_str.h' file not found

Any idea on how to fix this? Thanks!

Comment by: Andi on

Andi's Gravatar For those who get the error "./php_memcached.h:23:10: fatal error: 'Zend/zend_smart_str.h' file not found":
pecl download memcached-2.1.0 works

Email address will never be publicly visible.

Basic HTML allowed.