Install memcached PHP extension on OS X Snow Leopard
August 30, 2009 by Glen. Filed under mac, php.
memcached is a very useful memory object caching system, which can be used to increase the performance of your dynamic scripts by caching database calls.
This guide will explain how to install the memcached system, including the PHP extension, on Mac OS X 10.6.
Xcode
The Xcode package installs the necessary versions of tools like autoconf which is needed during the PHP extension compilation process. Make sure you have Xcode 3.2 installed; the install package is available on the Snow Leopard install DVD under the “Optional Installs” folder.
libevent
libevent is a pre-requisite for memcached.
cd /tmp; curl -O http://www.monkey.org/~provos/libevent-1.4.12-stable.tar.gztar zxvf libevent-1.4.12-stable.tar.gzcd libevent-1.4.12-stable./configure; makesudo make install
memcached
memcached is the daemon responsible for actually storing and retrieving arbitrary objects for your applications.
cd /tmp; curl -O http://memcached.googlecode.com/files/memcached-1.4.1.tar.gztar zxvf memcached-1.4.1.tar.gzcd memcached-1.4.1./configure; makesudo make install
libmemcached
libmemcached is the shared library that will allow clients, in this case PHP, access the memcached daemon.
- Download libmemcached, move to
/tmpand unpack cd libmemcached-0.31./configure; makesudo make install
php extension
Now we are ready to prepare the PHP extension to memcached, which is available from pecl.
cd /tmp; pecl download memcachedgzip -d < memcached-1.0.0.tgz | tar -xvf -cd memcached-1.0.0; phpize
You should see output similar to the following:
Configuring for:
PHP Api Version: 20090626
Zend Module Api No: 20090626
Zend Extension Api No: 220090626
./configure; makesudo make install
On a successful install, you will get the following message:
Installing shared extensions: /usr/lib/php/extensions/no-debug-non-zts-20090626/
Modify your php.ini configuration file and make sure you have the following line included:
extension = memcached.so
You can then restart your Apache server:
sudo apachectl restart
to make the memcached functionality available in your scripts.
References
Tags: macosx, memcached, pecl, php, snowleopard

Glen,
Thank’s again for this excellent tutorial. 2 issues here for me:
I first followed the tutorial but couldn’t get memcached to work on SL 10.6.
Then i realized that i had to specify has for “mcrypt” to compill for 64 bit so i started the whole process by typing in terminal:
export CFLAGS=”-arch x86_64″
Everything installed fine and i added to php.ini:
extension=memcached.so
2nd (minor) issue: for the php extension, this line is missspelled:
cd memcache-1.0.0; phpize –> # cd memcached-1.0.0; phpize
Thank’s again
Thanks for the feedback, I’ve corrected the path to the memcached PHP extension directory. I personally didn’t need the CFLAGS environment variable set, but if you have problems then it makes sense to include it to specifically build 64 bit libraries.
Just curious as to why you choose to use /temp to hold all the files?
Actually, it’s not particularly important where you have your source files. I picked /tmp because I wouldn’t need the sources after install. The system cleans /tmp periodically so it’s a handy way to save space. If you think you might need the source code again in the future, it would make more sense to save to, for example, ~/src or perhaps /usr/local/src if you want to make it available system-wide.
Thanks, that is what I figured. This is my first time through compiling my own php extensions — I had been using entropy previously. One more question. I notice you did not ure the –prefix flag. Do you have any strategies for keeping /usr/local organized?
Okay, I followed your steps without any problems, but here’s the weird thing:
Fatal error: Class ‘Memcache’ not found in
That’s what I get when I’m trying to use the memcached module in PHP..
BUT, when I create a phpinfo()-file:
memcached
memcached support enabled
Version 1.0.0
libmemcached version 0.31
Session support yes
igbinary support no
Doesn’t make sense imho. Who knows what’s going on here?
Martijn: can you post the code that is causing the error?
Yes, I’m having the same issue
I installed memcached, I got it listed with phpinfo, but if I do something like
$a = new Memcache();
it brokes saying that it cannot find the class
I’m having the same problem of The Memcache class not being found. Has anyone found a solution for this?
The class name is actually Memcached, so you would create a new instance using the following:
m1 = new Memcached();For full information about the class, please see http://php.net/manual/en/class.memcached.php
Hi there, I did have this kind of error and I also did php -i | grep memcache and it says memcache is enabled, but my solution that solved the problem was when I edited the php.ini, i simply modified the extension_dir=”./” line to the full path of the extensions directory which now looked like this, extension_dir=”/usr/local/lib” — you need to check where the extension directory of the php reside and make sure memcache.so is there..
then i simply restarted httpd and alas the problem is gone.
you can check the detailed steps here:
http://joemarie-aliling.com/223/php-programming/php-memcache-not-found-problem/
[...] Install memcached PHP extension on OS X Snow Leopard – Much easier than I expected [...]
I actually don’t get it : which class should it be ? Memcache or Memcached
I installed everything as stated, got memcached support => enabled
libmemcached version => 0.31
but in the lextensions library, I have a module memcached.so testing
$memcache = new Memcached;
$memcache->connect(’localhost’, 11211) or die (”Could not connect”);
I get :
Fatal error: Call to undefined method Memcached::connect()
it seems that the class should be Memcached.., but connect is wrong !
so I tested
$memcache = new Memcached;
$memcache->addServer(’localhost’, 11211) or die (”Could not connect”);
and got the connection
but an error later in the number of parameters :
$memcache->set(’key’, $tmp_object, false, 10) or die (”Failed to save data at the server”);
Warning: Memcached::set() expects at most 3 parameters, 4 given
I am not a php dev… so what’s wrong there ? why some teste examples use Memcache and other Memcached (any change between php versions ?…)
thanks for php enlightments
)
I tried this on 10.6.2 Server, and I got through the process ok, but memcached doesn’t show up in the phpinfo() page. Is there anything different I should be doing?
Got it figured out. Needed a specific directive with the full path as Xifer84 suggested. Thanks!
When I run a make after phpizing the PHP extension
bash > make
app:memcached-1.0.0 Admin$ make
/bin/sh /tmp/memcached-1.0.0/libtool –mode=compile cc -I/usr/include/php -I. -I/tmp/memcached-1.0.0 -DPHP_ATOM_INC -I/tmp/memcached-1.0.0/include -I/tmp/memcached-1.0.0/main -I/tmp/memcached-1.0.0 -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/usr/local/include -DHAVE_CONFIG_H -g -O2 -c /tmp/memcached-1.0.0/php_memcached.c -o php_memcached.lo
mkdir .libs
cc -I/usr/include/php -I. -I/tmp/memcached-1.0.0 -DPHP_ATOM_INC -I/tmp/memcached-1.0.0/include -I/tmp/memcached-1.0.0/main -I/tmp/memcached-1.0.0 -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/usr/local/include -DHAVE_CONFIG_H -g -O2 -c /tmp/memcached-1.0.0/php_memcached.c -fno-common -DPIC -o .libs/php_memcached.o
/tmp/memcached-1.0.0/php_memcached.c: In function ‘php_memc_get_impl’:
/tmp/memcached-1.0.0/php_memcached.c:371: warning: passing argument 4 of ‘memcached_mget_by_key’ from incompatible pointer type
/tmp/memcached-1.0.0/php_memcached.c:408: warning: assignment discards qualifiers from pointer target type
/tmp/memcached-1.0.0/php_memcached.c:440: warning: passing argument 4 of ‘memcached_mget_by_key’ from incompatible pointer type
/tmp/memcached-1.0.0/php_memcached.c: In function ‘php_memc_getMulti_impl’:
/tmp/memcached-1.0.0/php_memcached.c:577: warning: passing argument 4 of ‘memcached_mget_by_key’ from incompatible pointer type
/tmp/memcached-1.0.0/php_memcached.c:607: warning: assignment discards qualifiers from pointer target type
/tmp/memcached-1.0.0/php_memcached.c:610: warning: assignment discards qualifiers from pointer target type
/tmp/memcached-1.0.0/php_memcached.c: In function ‘php_memc_getDelayed_impl’:
/tmp/memcached-1.0.0/php_memcached.c:726: warning: passing argument 4 of ‘memcached_mget_by_key’ from incompatible pointer type
/tmp/memcached-1.0.0/php_memcached.c: In function ‘zim_Memcached_fetch’:
/tmp/memcached-1.0.0/php_memcached.c:801: warning: assignment discards qualifiers from pointer target type
/tmp/memcached-1.0.0/php_memcached.c:804: warning: assignment discards qualifiers from pointer target type
/tmp/memcached-1.0.0/php_memcached.c: In function ‘zim_Memcached_fetchAll’:
/tmp/memcached-1.0.0/php_memcached.c:852: warning: assignment discards qualifiers from pointer target type
/tmp/memcached-1.0.0/php_memcached.c:855: warning: assignment discards qualifiers from pointer target type
/tmp/memcached-1.0.0/php_memcached.c: In function ‘zim_Memcached_setOption’:
/tmp/memcached-1.0.0/php_memcached.c:1785: error: ‘memcached_st’ has no member named ‘hash’
/tmp/memcached-1.0.0/php_memcached.c: In function ‘php_memc_do_result_callback’:
/tmp/memcached-1.0.0/php_memcached.c:2324: warning: assignment discards qualifiers from pointer target type
/tmp/memcached-1.0.0/php_memcached.c:2327: warning: assignment discards qualifiers from pointer target type
make: *** [php_memcached.lo] Error 1
app:memcached-1.0.0 Admin$
I received the successful install message -
“mike-2:memcached-1.0.2 mike$ sudo make install
Installing shared extensions: /usr/lib/php/extensions/no-debug-non-zts-20090626/”
- and restarted apache, but I don’t see the memcache section within a file containing phpinfo(). I’m running SL 10.6.4 – any thoughts?
I have a problem when I get to the stage of installing the PHP extension. When running
pecl download memcachedI get the following error:Warning: touch(): Unable to create file /usr/lib/php/.lock because Permission denied in PEAR/Registry.php on line 835I checked the folder, and despite being administration of my iMac (running Snow Leopard), the folder says I can only read in the “Get Info” dialog, and I’ve been unable to change the owner to myself via
sudo.What can I do to install memcached at this stage?
Ignore my last comment; I’m now at the same point as Mike (comment xvii). Any thoughts?