How to install memcached on Ubuntu for use with Drupal

This guide is based on various community forum posts. Special thanks to Jonathan Wagener for the Ubuntu specific instructions

This guide is intended as a relatively easy step by step guide to:

  • Install and configure memcached and memcache PHP extension on Ubuntu 11.04 and higher for use with Drupal and the Drupal Memcache module.
  • This is a very basic single memcached server setup but a good starting point for getting memcached working. 

Requirements:

  • Ubuntu 11.04 or later installed on your machine. 
  • PHP 5.2.0 or higher installed

 


1. Install memcached on your server.

  • Open the Terminal Window and enter :
sudo apt-get install memcached libmemcached-tools

2. Install memcache PHP extension using PECL.

  • PECL is great for installing PHP extensions.
sudo apt-get install php5-dev php-pear make
  • After you have installed PECL on your system, open the Terminal Window and enter :
sudo pecl install memcache

3. Add memcache.so to php.ini

  • We must instruct PHP to load the extension.
  • You can do this by adding a file named memcache.ini to the configuration directory /etc/php5/conf.d
  • Open the Terminal Window and enter :
sudo nano /etc/php5/conf.d/memcache.ini
  • Add the following line to the file and save :
extension=memcache.so
  • If you intend to use memcached with Drupal also add the following line to your php.ini or memcache.ini file and save :
memcache.hash_strategy="consistent"

4. Open firewall port 11211.

  • The default port for the memcached server is TCP port 11211.
  • Configure your firewall to open port 11211 for TCP traffic.

5. Configure the memcached allowed memory.

  • All memcached configuration settings can be found in /etc/memcached.conf
  • The default memory setting for memcached is 64 MB. 
  • Depending on the amount of RAM available on the server allocate a block of memory to memcached.
  • Open the Terminal Window and enter :
sudo nano /etc/memcached.conf
  • Change the following line FROM-
# Start with a cap of 64 megs of memory. It's reasonable, and the daemon default
# Note that the daemon will grow to this size, but does not start out holding this much
# memory
-m 64
  • TO the following by changing the -m 64 to -m 4096 to allow memcached 4 GB of RAM. Adjust the size in MB according to the memory that you have available. Save the file when done.
# Start with a cap of 64 megs of memory. It's reasonable, and the daemon default
# Note that the daemon will grow to this size, but does not start out holding this much
# memory
-m 4096

6. Start the memcached service.

  • Open the Terminal Window and enter :
sudo service memcached start
  • OR on older systems :
sudo /etc/init.d/memcached start

7. Restart Apache.

  • Open the Terminal Window and enter :
sudo service apache2 restart
  • OR on older systems :
sudo /etc/init.d/apache2 restart

8. Check to see if memcached server is active and listening on port 11211.

  • Open the Terminal Window and enter :
netstat -tap | grep memcached

9. Check the status and stats with memstat tool

  • Part of the memcached package is a handy tool called memstat.
  • You need to specify the host IP and port. In this case the host IP is 127.0.0.1 and the port 1211.
  • Open the Terminal Window and enter :
memstat 127.0.0.1:11211

10. Activate the Drupal memcached module.

  • Install the Drupal Memcache module and activate. For more complete instructions visit the Drupal Memcache Documentation
  • Edit settings.php in your Drupal installation to include memcache.inc
  • For Drupal 6, edit the settings.php file and add the following :
$conf['cache_inc'] ='sites/all/modules/memcache/memcache.inc';
  • For Drupal 7, edit the settings.php file and add the following :
$conf['cache_backends'][] = 'sites/all/modules/memcache/memcache.inc';
$conf['cache_default_class'] = 'MemCacheDrupal';
$conf['memcache_key_prefix'] = 'something_unique';

* note : Replace the "something_unique" in the last line with your own unique memcache key prefix. The memcache_key_prefix is also needed for both Drupal 6 & 7 in a multi-site environment if you would like to use memcached for more than one Drupal installation on the same server. 

Tags: 

Comments

Brilliant tutorial. Just

Brilliant tutorial. Just followed it exactly for Drupal 7 and all is working great. Thank you.

Excellent tutorial got it and

Excellent tutorial got it and configured the system as needed. Thank you

It should be added that the

It should be added that the path to the memcache module may be different if it was installed using drush. If drush is used and a "contrib" directory is present under "sites/all/modules", then the correct path will end up being "sites/all/modules/contrib/memcache/memcache.inc". Also, for me, memstat was not installed with memcached (Ubuntu 12.04), but was simple enough to install with apt-get. Thanks for putting together some clear information! -Corey

Wow, thanks so much for this!

Wow, thanks so much for this! I was searching many pages to find some easy to follow instructions for installing memcached on my Ubuntu machine, as well as on my Ubuntu VPS. This is great!! :) Jim

Great tutorial.

Great tutorial. Straightforward and easy to read. Cheers!

Drupal Memcache

Great tutorial for Memcache. Thank you very much

Add note about securing memcached

First of all, great guide! It would be great, though, if you could add a note about securing memcached. See http://blog.codesherpas.com/on_the_path/2010/08/securing-memcache-in-2-minutes.html On a single server, this is as simple as making sure to use ip 127.0.0.1 in memcached.conf and Drupal's settings.php. You should NOT use the public ip of your server.