How to Install and Configure the Memcached Object Caching System on Ubuntu

What is Memcached?

When a user visits a dynamic website (like WordPress or an e-commerce store), the web server must query the database to retrieve the page content, which is a highly CPU-intensive and time-consuming process. If 1,000 users request the homepage simultaneously, the database will crash. Memcached is a high-performance, distributed memory object caching system. It temporarily stores the results of complex database queries directly in the server’s ultra-fast RAM. When the next user requests the same page, the web server pulls the data instantly from Memcached, completely bypassing the database and drastically improving page load times.

Step 1: Install the Memcached Package

Memcached is available in the default Ubuntu repositories. Open your terminal and install both the daemon and the accompanying tools:

sudo apt update

sudo apt install memcached libmemcached-tools -y

The service will start automatically. You can verify its status using sudo systemctl status memcached.

Step 2: Configure Memory Allocation and Security

By default, Memcached is configured to use a very small amount of RAM (usually 64MB). You must edit the configuration file to increase this limit based on your server’s capacity.

Open the configuration file:

sudo nano /etc/memcached.conf

Locate the -m parameter. Change the value from 64 to the amount of RAM (in Megabytes) you want to dedicate to the cache. For a server with 8GB of RAM, allocating 1GB (1024MB) to Memcached is a solid starting point:

-m 1024

Next, locate the -l (listen) parameter. Memcached does not include built-in authentication. It is incredibly dangerous to expose Memcached to the public internet. Ensure it is strictly bound to the local loopback interface so only applications on the same server can query it:

-l 127.0.0.1

Save and close the file.

Step 3: Restart the Service

Apply your new memory allocation and security bindings by restarting the Memcached daemon:

sudo systemctl restart memcached

Step 4: Test the Cache Functionality

You can test if Memcached is actively accepting and storing data using the memcstat command-line utility (which was installed in Step 1). Run:

memcstat --servers="127.0.0.1"

This will dump a list of internal statistics. Look at the bytes, cmd_get, and cmd_set variables. As your web applications begin using the cache, you will see the curr_items (current items cached in RAM) metric begin to increase.

Step 5: Install Application Extensions (PHP/Python)

While Memcached is now running on the server, your web applications cannot “speak” to it until you install the specific language extension.

If you are running a PHP application (like WordPress or Drupal), you must install the PHP Memcached module and restart your web server:

sudo apt install php-memcached -y

sudo systemctl restart apache2 (or php-fpm)

If you are running a Python application (like Django), install the library via pip:

pip3 install pymemcache

Your web application is now fully equipped to offload its heavy database queries into high-speed RAM caching!

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.