Install memcached PHP extension on OS X Snow Leopard

Memories

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.gz
  • tar zxvf libevent-1.4.12-stable.tar.gz
  • cd libevent-1.4.12-stable
  • ./configure; make
  • sudo 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.gz
  • tar zxvf memcached-1.4.1.tar.gz
  • cd memcached-1.4.1
  • ./configure; make
  • sudo make install

libmemcached

libmemcached is the shared library that will allow clients, in this case PHP, access the memcached daemon.

  • Download libmemcached, move to /tmp and unpack
  • cd libmemcached-0.31
  • ./configure; make
  • sudo make install

php extension

Now we are ready to prepare the PHP extension to memcached, which is available from pecl.

  • cd /tmp; pecl download memcached
  • gzip -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; make
  • sudo 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.

memcached php extension

References

Glen Scott

I’m a freelance software developer with 18 years’ professional experience in web development. I specialise in creating tailor-made, web-based systems that can help your business run like clockwork. I am the Managing Director of Yellow Square Development.

More Posts

Follow Me:
TwitterFacebookLinkedIn

33 thoughts on “Install memcached PHP extension on OS X Snow Leopard

  1. Atypik Design

    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

    Reply
  2. Glen Post author

    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.

    Reply
    1. Glen Post author

      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.

      Reply
  3. double

    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?

    Reply
  4. Martijn Engler

    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?

    Reply
  5. xipher84

    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/

    Reply
  6. Pingback: My Bookmarks For November 18th – December 4th | Cristiano on Tech/Life

  7. kadoudal

    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 ;-))

    Reply
  8. Art

    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?

    Reply
  9. Chris

    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$

    Reply
  10. Mike

    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?

    Reply
  11. Martin Bean

    I have a problem when I get to the stage of installing the PHP extension. When running pecl download memcached I get the following error:

    Warning: touch(): Unable to create file /usr/lib/php/.lock because Permission denied in PEAR/Registry.php on line 835

    I 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?

    Reply
  12. Jon z

    I realize this thread is months old but I’m having difficulty getting past the phpize step, and I haven’t been able to find any other resource on the web that helps me solve it. If you have got the time, I am encountering this error:

    jon@memcached-1.0.2: phpize
    Configuring for:
    PHP Api Version: 20090626
    Zend Module Api No: 20090626
    Zend Extension Api No: 220090626
    /Developer/usr/bin/gm4: unrecognized option `–debugfile=autom4te.cache/traces.0t’
    Try `/Developer/usr/bin/gm4 –help’ for more information.
    autom4te-2.63: /Developer/usr/bin/gm4 failed with exit status: 1

    It looks like an error in passing options to the m4 script.. any thoughts?

    This is my version of m4:
    m4 (GNU M4) 1.4.15

    Reply
    1. Glen Post author

      Hey Jon,

      You seem to have a different m4 version installed to me. Mine is:

      GNU M4 1.4.6
      Copyright (C) 2006 Free Software Foundation, Inc.
      This is free software; see the source for copying conditions. There is NO
      warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

      Written by Rene' Seindal.

      This is the version that was installed after using the Xcode installer on my Snow Leopard install DVD. How did you install your UNIX Dev Support tools?

      Reply
  13. Jon z

    Ah, I see that the version installed by fink was taking precendence, I hijacked my PATH to use the xcode installed version and the install was successful! Thank you very much.

    Reply
  14. Jim Dalton

    Regarding the Memcache vs. Memcached class name that several commenters in this thread have mentioned:

    There are TWO Memcached clients in the PHP Extension Community Library. They are quite unhelpfully named Memcache (http://pecl.php.net/package/memcache) and Memcached (http://pecl.php.net/package/memcached). Here is a handy comparison chart: http://code.google.com/p/memcached/wiki/PHPClientComparison

    The instructions in this blog post are for the Memcached client. If you’re using a library that calls the Memcache class, this client will not work for you. However, the installation steps should be quite similar to get the Memcache client up and running.

    Which is a preferable library? For a long while there was no development activity on Memcache, so it appeared as though Memcached was going to supplant it. However, as of this morning it looks like there is some new activity in the last 30 days on Memcache, so who knows. Some (old) discussion is here: http://serverfault.com/questions/63383/memcache-vs-memcached

    I lost about 15 minutes trying to sort this out, so hopefully some future visitor to this page will save themselves the trouble.

    Reply
  15. Pingback: » Install Memcache on OS X for PHP/Apache Bruce's Blog

  16. Pingback: PHP > memcache - Installation auf Snow Leopard - AWEBGO / Webdesign – Hamburg DE

  17. Pingback: Installing memcache on osx for php

  18. Pingback: 在 OS X 10.7 / 10.8 上安装 PHP memcache 扩展 ‹ 龙猫の笔记

  19. Avi

    I realize that this is a very old tutorial, but I am running into an issue that I can’t seem to figure out. I’m running MAMP on my MacBook Pro (10.6.8) and on running the phpize command in memcached-2.1.0 I get the folloiwng error:

    grep: /Applications/MAMP/bin/php/php5.3.6/include/php/main/php.h: No such file or directory
    grep: /Applications/MAMP/bin/php/php5.3.6/include/php/Zend/zend_modules.h: No such file or directory
    grep: /Applications/MAMP/bin/php/php5.3.6/include/php/Zend/zend_extensions.h: No such file or directory
    Configuring for:
    PHP Api Version:
    Zend Module Api No:
    Zend Extension Api No:

    I believe it has to do with MAMP not including the header files, but I don’t know how to fix this. A while back I ran into the same issue when installing the client library for MongoDB, but in that case I got lucky as someone had posted the precompiled mongo.so file. No such luck here, any help would be appreciated…

    Reply
  20. Mudaser Ali

    Hi,

    I had followed all steps you had mentioned on mac OS X 10.8.2

    but could not successfully installed it 🙁 🙁 🙁

    Reply
  21. Mark

    I also get:
    grep: /Applications/MAMP/bin/php/php5.4.10/include/php/main/php.h: No such file or directory
    grep: /Applications/MAMP/bin/php/php5.4.10/include/php/Zend/zend_modules.h: No such file or directory
    grep: /Applications/MAMP/bin/php/php5.4.10/include/php/Zend/zend_extensions.h: No such file or directory
    Configuring for:
    PHP Api Version:
    Zend Module Api No:
    Zend Extension Api No:

    How can I fix this problem?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.