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:

sudo tasksel install lamp-server

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)
 sudo apt-get install php5-dev apache2-threaded-dev 

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.

 sudo apt-get install build-essential 

Step 4: PEAR and memcached

You’re also going to need PEAR and the memcached binary.

 sudo apt-get install php-pear memcached 

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. Download it with your preferred method, or type:

 wget http://download.tangent.org/libmemcached-0.37.tar.gz 

Extract the files with your preferred method, or type:

 tar -xzf libmemcached-0.37.tar.gz 

Move into the extracted directory:

 cd libmemcached-0.37/ 

Configure the package.

 ./configure 

Make.

 make 

Make install. Note this probably will require sudo rights.

 sudo make install 

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.]

 sudo pecl install memcached 

When this process completed, you should see something like this:

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

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:

 gksudo gedit /etc/php5/apache2/php.ini 

After you save and close the file, restart Apache.

 sudo /etc/init.d/apache2 restart 

Tags: , , , ,

2 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!

Leave a Reply