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. For me, it was libmemcached-0.33. Download it with your preferred method, or type:

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

Extract the files with your preferred method, or type:

 tar -xzf libmemcached-0.33.tar.gz 

Move into the extracted directory:

 cd libmemcached-0.33/ 

Configure the package.

 ./configure 

Make.

 make 

Make install. Note this probably will require sudo rights.

 sudo make install 

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: , , , ,

Leave a Reply