Search Our Database

How to Install memcached And libmemcached

Last updated on |

Memcached is an open source, high-performance, distributed memory caching system intended to speed up dynamic web applications by reducing the database load. It is a key-value dictionary of strings, objects, etc., stored in the memory, resulting from database calls, API calls, or page rendering.

The system caches data and objects in memory to minimize the frequency with which an external database or API must be accessed.


Installing memcached and libmemcached

1. Download and install memcached by running the following commands:

*The latest version of memcached can be found at:

**Ensure that libevent & libevent-devel packages have been installed prior.

cd /usr/src
tar xvf memcached-1.5.18.tar.gz
cd memcached-1.5.18
./configure -–with-libevent
make && make install

2. Download and install libmemcached by running the following commands:

*The latest version of memcached can be found at:

tar xvf libmemcached-1.0.18.tar.gz
cd libmemcached-1.0.18
make && make install

3. Start the memcached as a daemon process and ensure memcached is automatically started after each server reboot.

memcached -d -u nobody -m 1048 -p 11211
vim /etc/rc.d/rc.local
# add the line below the rc.local file
/usr/local/bin/memcached -d -u nobody -m 1048 -p 11211

4. Install the Memcache PECL Extension for PHP.

pecl install memcache

5. Add the following codes into php.ini

vim /usr/local/lib/php.ini
# Add the line below in the php.ini file

6. Restart the httpd service.

/etc/init.d/httpd restart

7. Verify the memcached using php and libmemcached. Create a new .php file and add the following codes:

connect("localhost",11211); # You might need to set "localhost" to ""

echo “Server’s version: ” . $memcache->getVersion() . ”

$tmp_object = new stdClass;
$tmp_object->str_attr = “test”;
$tmp_object->int_attr = 123;

echo “Store data in the cache (data will expire in 10 seconds)

echo “Data from the cache:

or else, run

# strace -p `pidof memcached`