How to install, Configure and Use Memcached in Ubuntu

By: Sunil Kumar |  In: PHP  |  Last Updated: 2018/06/13

When you are working on a high traffic web application the main component that got loaded is backend database. Especially in case, your database is not designed in such a way that it can handle high load and the database is not distributed. There are many ways to deal with this problem. One of them is using a caching system like Memcached. Memcached is a 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. Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering. In this guide, we're going to discuss how to install, configure and use Memcached on an Ubuntu server.

Installing Memcached and its Components

To get started with Memcached first we need to get all the necessary component from the Ubuntu Repository which is not a tough task. To get all the updated Component first update your system using

now to install Memcached

Here I am supposing that you have already installed PHP and MySQL-server. If not you need to install these first. for this, you can use

This should install and configure everything you need.

Check Memcached Installation

After installation, our first step should be to verify whether it is installed correctly or not. There are several ways to verify that. The first way is using PHP. Create a .php(info.php) file in your project(Here in the root directory). our default document root is /var/www/html. and put following code into it

now when we visit http://localhost/info.php we will find all information about PHP's configuration If you scroll down or search for the "Memcached" section header, you should find something that looks like this: This means that the Memcached extension is enabled and being found by the web server. We can also check by typing this command in terminal

Start, Stop, Restart Memcached service

To Start, Stop, Restart Memcached service we can use the following command

Storing data inside Memcached

Now at this point, we are sure that Memcached is installed successfully and working fine, We can test it for data caching. For this, we have to store some data into Memcache. To do so we have to create a .php script where we going to write the code-

As Memcached store data in the key-value pair, we are storing data into "first_data_key" key.

Retrieving data from Memcache

Retrieving data from Memcached is more simple then storing data into it. Suppose we wat to retrieve the data we set above

when we browse http://localhost/memcached_test.php output should be like this

Deleting data from Memcached

We can easily delete the data by the key which is used at the time of storing the data. If we want to delete the data of "first_array_data" key

Working on Memcached Command line

We also can use telnet to do all the above stuff directly from the terminal or command line

Connecting to Memcached server

eg. telnet localhost 11211 Setting the data


Getting data from key

Flush(delete) Memcache data Flush the server key/value pair (invalidating them) after an optional [

if we have to flush all Memcached data


By now, you should have a decent understanding of how Memcached works and how you can leverage it to keep your web server from hitting the database repeatedly for the same content. Memcached is most useful when you have to query data from database most of the time or your data keep changing frequently. But if your content is static or it changes infrequently, Varnish cache is more pleasant.

You can read full varnish installation and setup guide here.


Leave a Comment

Your email address will not be published.


Sunil Kumar

I am the owner of acmeextension. I am a passionate writter and reader. I like writting technical stuff and simplifying complex stuff.
Know More

Subscribe to our mailing list

%d bloggers like this: