Community Documentation

v3 Knowledgebase

How to install Memcached on a CentOS server

What is caching

Before diving into Memcache specifically, let’s take a step back. What is caching? Why should you care and why should you use it? Caching is used for two very important reasons: to speed up the delivery of the pages and to alleviate system resources. Caching is used to speed up dynamic sites; database driven sites will benefit most form caching. Think about a WordPress site. Each page you visit is not an actually file, but rather an amalgamation of the theme, widgets, posts, footers, headers, etc… Each time a page is accessed, PHP will generate the page requested on the fly from the database. It takes time to query the database to create the page. These database queries put a strain on the resources of your server.

However, what if instead of continually generating a new page, the same page, for every visitor, you were to turn those pages into static HTML files? No database querying is needed for the new visitors. A static file can be served up much faster and with significantly less resource consumption. Your visitors see their requested page sooner, and you save on CPU cycles. Everyone is happy. This is what caching does.

What is Memcached

Memcached is a general-purpose distributed memory caching system. It is one of the most popular caching tools and is used in such popular sites as: YouTube, Reddit, Zynga, Facebook, and Twitter.

According to Memcached’s official site, Memcached is defined as: Free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

In simpler terms, it decreases database load by storing objects in memory.

Installing Memcached (daemon)
The quickest and easiest method would be to install via Yum. First, you must grab the RPM that matches your OS:

CentOS 6 (64 bit):

rpm -Uvh

CentOS 6 (32 bit):

rpm -Uvh

CentOS 5 (64 bit):

rpm -Uvh

CentOS 5 (32 bit):

rpm -Uvh

CentOS 4 (32 bit):

rpm -Uvh

Now, to install it with Yum:

yum install memcached

Start the memcached service:

/etc/init.d/memcached start

Configure the memcached service to start when the server boots:

chkconfig memcached on

Finally, disable the RPM so that it is not used for future Yum functions:

perl -pi -e "s/enabled=1/enabled=0/g;" /etc/yum.repos.d/epel.repo

How to install PHP memcache

This guide will help you install PHP Memcache on a CentOS server.

What is PHP memcache?

In my previous article, I showed you how to install Memcached, the service daemon. Now, if you would like your PHP software to interface with that daemon, you will want to install a PHP extension for it.

There are actually two separate implementations of a PHP Client that wraps the memcached (daemon); both are provided via the PECL library. One is called ‘memcache’ and the other is called ‘memcached’. I know that it is a little confusing that ‘memcached’ shares the same name as the daemon itself, but it IS a separate PHP wrapper.

This article will focus on installing ‘memcache’ as it is the most commonly used. You can use the information to install ‘memcached’ as well; the installation steps are the same. The main difference is that ‘memcached’ requires the libMemcached library. You can see a comparison of the two different PHP clients here:

Installation, the quick method

The easiest method is to simply use PECL’s ‘install’ command. This will grab the latest stable release, configure it with the default options, and add it to the server’s php.ini:

pecl install memcache

*Note: Un-installation is just as easy:

pecl uninstall memcache

Installation, the manual method

The quick ‘install’ method uses the default configuration options and should serve most peoples’ purposes. However, if you need more control over the installation options, you can manually install from source and add it flags as you need.

Download the source package via PECL:

cd /usr/local/src/
pecl download memcache

Unpack the tar and enter into the newly extracted directory (be sure to replace the X’s with your downloaded version):

tar -xvzf memcache-X.X.X.tgz
cd memcache-X.X.X

Configure and install:

./configure && make && make install

Verify the install

Verify the installation by displaying all the installed PHP modules. See if ‘memcache’ is listed:

php -m

Problems with manual installation

The ‘make install’ should load the module into your server’s extension directory automatically. However, if you do not see memcache listed in a ‘php -m’, you will need to add the module manually

First, you’ll want to very the location of your server’s extensions directory:

extension_dir = "/usr/local/lib/php/extensions/no-debug-non-zts-20060613"

Next, copy the memcache module into that directory (be sure to replace the path with the one found on your server):

cp modules/ /usr/local/lib/php/extensions/no-debug-non-zts-20060613

Finally, add the module to your php.ini:

echo '' >> /usr/local/lib/php.ini


It used to be a common issue that the quick ‘install’ method could not be used for servers where the /tmp partition was mounted ‘noexec’. The problem was that the ‘configure’ process could not execute because the PECL client downloaded the module into in /tmp. When /tmp is mounted ‘noexec’, servers cannot execute scripts from /tmp.

However, PECL now downloads and executes the script from /root/tmp. No need to worry about compiler errors with /tmp. You can use the ‘install’ method even with a secured /tmp.