How to install PHP memcached on an Ubuntu server

I needed to install memcached on my local development server the other day and hit several snags during the process when I couldn’t find a clear guide. It doesn’t help that there’s another package floating around called “memcache” which is not the same as “memcached”.

Fortunately I found this post in German which Google kindly translated for me, and it solved my problems.

Since the translation screws up some of the code, I thought I would post the steps here in English in case anyone finds it useful.

Step 1: LAMP

If you don’t already have PHP 5 and Apache2 set up, this will get you started. Open up a terminal and type:
[bash]sudo tasksel install lamp-server[/bash]

Guiding you through setting up PHP and Apache is beyond the scope of this guide, so if you get stuck, check the multitude of guides available on Google.

Step 2: Upgrade LAMP

With PHP 5 and Apache installed, the next step is to install the developer versions.

  • php5-dev (otherwise you’ll get an error about “phpize”)
  • apache2-threaded-dev (or else you’ll get an “apxs” error)

[bash] sudo apt-get install php5-dev apache2-threaded-dev [/bash]

Step 3: Getting build tools

We’re going to be building from source. If you’ve never done that before it’s pretty easy, but first you’ll need some extra tools.
[bash] sudo apt-get install build-essential [/bash]

Step 4: PEAR and memcached

You’re also going to need PEAR and the memcached binary.
[bash] sudo apt-get install php-pear memcached [/bash]

Step 5: Building libmemcached

You’ll need to get the latest source package from here. You may have to try different versions until you get it working. In 9.04, libmemcached-0.33 worked. For 9.10, it was libmemcached-0.37. As of Ubuntu 10.04′s launch, the latest version was libmemcached-0.40, which appears to install correctly but under actual usage conditions the Memcached::add() method failed to write every time, returning error code 5. I recommend sticking with libmemcached-0.37 for now if you’re running Ubuntu 10.04.

Download the appropriate version with your preferred method, or type:
[bash] wget http://download.tangent.org/libmemcached-0.37.tar.gz [/bash]

Extract the files with your preferred method, or type:
[bash] tar -xzf libmemcached-0.37.tar.gz [/bash]

Move into the extracted directory:
[bash] cd libmemcached-0.37/ [/bash]

Configure the package.
[bash] ./configure [/bash]

Make.
[bash] make [/bash]

Make install. Note this probably will require sudo rights.
[bash] sudo make install [/bash]

Step 6: PECL

Finally, you can install the memcached extension through PECL. [Thanks to Andy Lav for pointing out this very important missing step in the comments.]
[bash] sudo pecl install memcached [/bash]

When this process completed, you should see something like this:
[bash]
Build process completed successfully
Installing ‘/usr/lib/php5/20060613+lfs/memcached.so’
install ok: channel://pecl.php.net/memcached-1.0.0
Extension memcached enabled in php.ini
[/bash]

If you see this, you should be ready to go.

You might instead get a message that says: You should add “extension = memcached.so” to php.ini. If that’s the case, open the file with the following and paste extension = memcached.so at the top:
[bash] gksudo gedit /etc/php5/apache2/php.ini [/bash]

After you save and close the file, restart Apache.
[bash] sudo /etc/init.d/apache2 restart [/bash]

Tags: , , , ,

14 Responses to “How to install PHP memcached on an Ubuntu server”

  1. andy lav says:

    thanks for a very useful HOWTO :-)

    i’ve managed to get this installed on 9.10 using v0.37 of libmemcached, and am i right in thinking there’s a step missing after ‘sudo make install’ which should be ‘sudo pecl install memcached’, to build the extension once you’ve done the client lib?

    thanks again,
    andy

    • Steve Love says:

      Yes, definitely forgot that important step. :) The post has been updated. I was able to get it running on 9.10 with v0.37 also, but not with v0.38. Very odd. Thanks for the comment!

  2. Isaac says:

    Hey I want to thank you for this very comprehensive memcached install instructions. I had been looking for a week for something like this (I’m a Linux newbie) and finally found it here.

    I have one thing I want to add that will complete this package and I hope will help out…

    If the Memcached doesn’t seem to work (e.g. you get WRITE FAILURE when using getResultMessage() to debug after a set() ), you may need to type the following in the terminal:

    sudo apt-get install php5-memcached

    then, restart…

    sudo /etc/init.d/apache2 restart

    Thank you so very much for this, you have lifted an incredible weight off my shoulders.

    • Steve Love says:

      Very glad you found the instructions useful! Thanks for the tip on installing php5-memcached. Perhaps that needs to be part of Step 4? I wonder if building from source is even necessary anymore. Hmm.

  3. Brett says:

    I got down to the part where I ran “pecl install memcached” and got this error:

    checking for libmemcached location… configure: error: memcached support requires libmemcached. Use –with-libmemcached-dir= to specify the prefix where libmemcached headers and library are located
    ERROR: `/tmp/pear/temp/memcached/configure’ failed

    But I just installed libmemcached 0.40… any thoughts?

  4. miki says:

    In case I can save somebody’s time looking for the right versions. For Red Hat CentOS release 5.4 (Final), the versions that compile are:

    - libmemcached: 0.35
    - pecl memcached: 1.0.0

    • Neo says:

      hey miki, Thanks it does saved lots of my time, too. As there is no clean/straight documentation regarding compatible installations/combinations on various m/cs.
      To Add, on CentOs release 5.4 (Final), i have successfully compiled
      pecl memcached : 1.0.1 and
      1.0.2 requires libmemcached 0.39+ (Found in one comment at pecl bugs site, Thanks to him).

      Hope this will help others
      enJoy :)

  5. Kevin says:

    I am following your steps, everything looks ok, except Step 6,

    sudo pecl install memcached

    running: make
    /bin/bash /var/tmp/pear-build-root/memcached-1.0.2/libtool –mode=compile cc -I/usr/local/zend/include/php -I. -I/tmp/pear/temp/memcached -DPHP_ATOM_INC -I/var/tmp/pear-build-root/memcached-1.0.2/include -I/var/tmp/pear-build-root/memcached-1.0.2/main -I/tmp/pear/temp/memcached -I/usr/local/zend/include/php -I/usr/local/zend/include/php/main -I/usr/local/zend/include/php/TSRM -I/usr/local/zend/include/php/Zend -I/usr/local/zend/include/php/ext -I/usr/local/zend/include/php/ext/date/lib -I/usr/local/include -DHAVE_CONFIG_H -g -O2 -c /tmp/pear/temp/memcached/php_memcached.c -o php_memcached.lo
    mkdir .libs
    cc -I/usr/local/zend/include/php -I. -I/tmp/pear/temp/memcached -DPHP_ATOM_INC -I/var/tmp/pear-build-root/memcached-1.0.2/include -I/var/tmp/pear-build-root/memcached-1.0.2/main -I/tmp/pear/temp/memcached -I/usr/local/zend/include/php -I/usr/local/zend/include/php/main -I/usr/local/zend/include/php/TSRM -I/usr/local/zend/include/php/Zend -I/usr/local/zend/include/php/ext -I/usr/local/zend/include/php/ext/date/lib -I/usr/local/include -DHAVE_CONFIG_H -g -O2 -c /tmp/pear/temp/memcached/php_memcached.c -fPIC -DPIC -o .libs/php_memcached.o
    /tmp/pear/temp/memcached/php_memcached.c:168: warning: useless storage class specifier in empty declaration
    /tmp/pear/temp/memcached/php_memcached.c:203: error: expected declaration specifiers or ‘…’ before ‘memcached_server_instance_st’

    …. more error and warning msgs …..

    /tmp/pear/temp/memcached/php_memcached.c:1942: error: expected declaration specifiers or ‘…’ before ‘memcached_server_instance_st’
    /tmp/pear/temp/memcached/php_memcached.c: In function ‘php_memc_do_version_callback’:
    /tmp/pear/temp/memcached/php_memcached.c:1949: error: ‘instance’ undeclared (first use in this function)
    make: *** [php_memcached.lo] Error 1
    ERROR: `make’ failed

    I am with Ubuntu 10.04 and zend server ce.

    I have tried pecl download memcached ; and ./configure –with-libmemcached-dir=
    but no luck.

    Any thoughts ?

    Kevin

    • Nelson says:

      Hey Kevin,
      This was happening to me too (Ubuntu Server 10.0.4). My problem was that I was using the same version of libmemcached that was posted (0.37). I repeated the steps with the latest version (0.44 as of today), and everything worked out.

      Hope this helps.

  6. Myhnea says:

    I do have the same problem as Kevin on Ubuntu 9.10.

Leave a Reply