Install Memcached in Ubuntu

Memchached is a popular caching server data in memory. With it you can greatly increase the performance of various web applications. By itself, Memcached only allows you to store pairs of key-value in memory and quickly access them. Memcached is usually used together with any programming language. For example with Php, Python or server-side JavaScript.

The service can be used to cache various objects in web servers such as Apache or Nginx. In this article we will examine the installation of Memcached on Ubuntu 18.04, as well as the basic settings of the program.

How to install Memcached in Ubuntu

1. Installation service

By itself, installing Memcached will not cause problems. You issue the following command:

sudo apt install memcached libmemcached-tools

Along with the service, we establish a set of additional utilities for easy management of Memcached. The installation is now complete.

2. Configuring Memcached

Configure Memcached Ubuntu is done via the file /etc/memcahced.conf. In this file contain options that are passed to it at startup:

  • -d – work in service mode;
  • -v – mode is more verbose;
  • -vv even more verbose output information;
  • -m – the amount of RAM that will use the service, I recommend to increase this parameter, since the values 64 megabytes, the default, will not be enough;
  • -p port on which to operate the service;
  • -u – the user on whose behalf the service is running;
  • -l – IP address on which Memcached will listen better to use to the external network, no one had access to your data;
  • – the number of concurrent connections;
  • -P – path to PID file in the file system.

Thus, most of the options can be left default. It is necessary to change only the RAM and IP address.

3. The launch of the service

Even if the service was running by default after changing the settings it needs to be restarted:

sudo systemctl restart memcached

You can still make sure that Memcached is running on that IP and port that we need:


Then you can see the statistics of using the service command:

memcstat --servers=""

To work with Memcached we can now. To do this, use Telnet. Connect to the server:

telne localhost 11211

If the connection is successful, you will see this message:

Use the stats command to obtain General statistics that we saw in the output of memcstat:


In order to save data, use the command add. She has this syntax:

add keyname flags vremena razmerzaniya

Flags is an integer that is stored along with the data and with them returned to the user, you can simply write 0. The retention time is the time in seconds, how long will stored the data, 0 means forever. Data size in bytes determines how much memory will be used for these data. Then press Enter and enter the data that you want to keep. For example, save the line in the memcached key for all:

add key 0 0 9

To get the value of the saved key the get command:

get key

And delete this key with delete command:

the delete key

But to view the list of all keys in Telnet it just won’t work. First you need to see a list of repositories:

stats items

And then stats cachedump command to display the list of storage keys. The first parameter is a store identifier, and the second limit of the key for output:

stats cachedump 1 100

To exit, type the command quit. Command memcdump you can list all the stored keys:

memdump --servers=

And with the help of memccat can print the value of the desired key:

memccat key --servers=

But all of these features are used only for debugging. For real storage used memcached clients for different programming languages. They are very easy to install. Let’s look at the example Php.

4. Configuring Memcached and Php

Let’s take a look at configuring Memcached libraries for Php. To install it, run:

sudo apt install php-memcached

Then restart apache, if you want to use the module on the web server:

sudo systemctl restart apache2

Then you can be sure that the module is activated by calling the function phpinfo():

<?php phpinfo();?>


In this article we discussed how to install Memcached in Ubuntu and how to use this service to cache data from the command line or in PHP. Don’t forget to verify that the service is listening for connections only on the local IP address, because there is no authentication, and your data will be able to access other users. It can also be unsafe due to the fact that Memcached has been found to have vulnerabilities that enable the attack type denial of service.


(Visited 42 times, 1 visits today)