Cybersecurity Updates & Tools

Install Memcached on Ubuntu 18.04: Setup, Config, and Client Guide

Memcached is a free, open-source, high-performance in-memory caching system. It stores data as key-value pairs in RAM, making reads and writes dramatically faster than hitting a database. Web applications use it to cache the results of expensive database queries, API calls, and rendered page fragments, reducing backend load and cutting response times significantly.

If your application runs against a MySQL or PostgreSQL database that handles repeated, identical queries, Memcached sits between the application and the database. It returns cached results instantly for repeat requests and only queries the database when the cache is empty or expired.

This guide covers installing Memcached on Ubuntu 18.04, configuring it for local and remote access, and connecting to it from PHP and Python applications.

<strong>Prerequisite:</strong>&nbsp;You need sudo access.

Install Memcached on Ubuntu

Update the package index and install Memcached along with its management tools:

bashsudo apt updatesudo apt install memcached libmemcached-tools

The libmemcached-tools package provides a set of command-line utilities for interacting with and managing the Memcached server. The Memcached service starts automatically after installation.

Verify it is running:

bashsudo systemctl status memcached

Look for Active: active (running) in the output. By default, Memcached listens on 127.0.0.1 (localhost only) on port 11211 and allocates 64 MB of RAM for the cache. The default configuration in /etc/memcached.conf is sufficient for most single-server setups.

Configure Remote Access

<strong>Security warning:</strong>&nbsp;Memcached has minimal built-in security. Without proper firewall rules, an exposed Memcached server can be exploited to amplify DDoS attacks. Only allow access from trusted, private network IP addresses.

By default, Memcached only accepts connections from localhost. If your application server runs on a different machine, you need to open the Memcached port in UFW and update the bind address in the config file.

This example uses Memcached server IP 192.168.100.20 and client IP 192.168.100.30.

First, add an SSH rule so you do not lock yourself out:

bashsudo ufw allow 22

Allow Memcached traffic only from the trusted client:

bashsudo ufw allow from 192.168.100.30 to any port 11211

Enable UFW and verify the rules are active:

bashsudo ufw status

Next, update the Memcached bind address. Open the configuration file:

bashsudo nano /etc/memcached.conf

Find the line beginning with -l 127.0.0.1 and replace the IP with your server’s private address:

-l 192.168.100.20

Restart Memcached to apply the change:

bashsudo systemctl restart memcached

The server now accepts connections from the allowed client IP on port 11211.

Connect PHP and Python Applications to Memcached

PHP

To use Memcached with a PHP application — such as WordPress, Drupal, Joomla, or Magento — install the PHP Memcached extension:

bashsudo apt install php-memcached

After installing the extension, restart your web server (Apache or Nginx) so PHP picks it up. You can then configure WordPress or any other CMS to use Memcached as its object cache backend, which dramatically reduces the number of database queries on each page load.

Python

Two widely used Python libraries are available for interacting with Memcached. Install your preferred one with pip:

bashpip install pymemcache
bashpip install python-memcached

pymemcache is actively maintained and generally the better choice for new projects. python-memcached is older but still common in legacy codebases.

Memcached is now installed and configured on your Ubuntu 18.04 server. For most applications, the default 64 MB cache with localhost-only access is a safe starting point. Scale the memory allocation in /etc/memcached.conf as your caching needs grow. Leave a comment below if you run into any issues during setup.