How to Install Memcached on Ubuntu 24.04

By Anurag Singh

Updated on Aug 03, 2024

How to Install Memcached on Ubuntu 24.04

In this tutorial, we'll explain how to install Memcached on Ubuntu 24.04.

Introduction

Memcached is a high-performance, distributed memory caching system designed to speed up dynamic web applications by reducing the database load. It achieves this by caching data and objects in RAM, thereby minimizing the need to repeatedly fetch data from the database or other slow storage layers.

Key Features

  • In-Memory Storage: Memcached stores data in memory, ensuring rapid access times and significantly reducing latency.
  • Distributed System: It can scale horizontally by adding more nodes, allowing for efficient load balancing and fault tolerance.
  • Simple Protocol: Uses a straightforward, text-based protocol that is easy to implement and integrate with various programming languages.
  • Data Types: Supports various data types, including strings, objects, and collections, allowing for flexible caching strategies.
  • Event-Based Architecture: Employs an event-driven architecture that efficiently handles multiple simultaneous connections.

Prerequisites

  • A Ubuntu 24.04 installed dedicated server or KVM VPS.
  • Access the server using SSH as a non-root user with sudo privileges.
  • Basic Linux command knowledge.

How to Install Memcached on Ubuntu

Step 1: Update Your System

First, ensure that your system is up-to-date by running the following commands:

sudo apt update
sudo apt upgrade -y

Step 2: Install Memcached

Install Memcached by running the following command:

sudo apt install memcached -y

Execute following command to verify Memcached installation:

memcached --version

Output:

memcached 1.6.24

This command should display the version of Memcached.

Step 3: Configure Memcached

The main configuration file for Memcached is located at /etc/memcached.conf. Open this file in your preferred text editor. For example:

sudo nano /etc/memcached.conf

In this file, you can set the memory size, default port, IP address, and other options. Here are some important configurations:

Memory Usage: Set the maximum amount of memory to use for object storage.

-m 64

Default Port: Set the port Memcached will listen on.

-p 11211

IP Address: Specify the IP address Memcached will listen on. By default, it listens on all available addresses.

-l 127.0.0.1

Uncomment the -c 1024 directive to limit the number of simultaneous Memcached connections. Replace 1024 with your desired number of connections.

-c 1024

After making the necessary changes, save and close the file.

To apply the changes, restart the Memcached service using the following command:

sudo systemctl restart memcached

To ensure Memcached is running properly, check its status with:

sudo systemctl status memcached

Step 4: Testing Memcached

You can use the telnet command to test if Memcached is working correctly. First, install telnet:

sudo apt install telnet -y

Then, connect to Memcached:

telnet 127.0.0.1 11211

You should see a prompt similar to this:

Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.

To test storing and retrieving data, use the following commands:

set mykey 0 900 4
data

This command sets a key mykey with the value data.

To retrieve the stored value:

get mykey

Output:

VALUE mykey 0 4
data
END

To exit telnet, type:

quit

Step 5: Connect to Memcached

Memcached is compatible with multiple application frameworks such as PHP, Perl, Python, Ruby, and Java. Follow the steps below to connect to Memcached and test the application using PHP.

Install PHP and the Memcached module.

sudo apt install php php-memcached -y

Create a new sample PHP script to connect to Memcached.

nano memcached.php

Add the following contents to the file.

<?php
$memcached = new Memcached();
$memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
$memcached->addServer('127.0.0.1', 11211);
$memcached->setSaslAuthData('example-user', 'strong-password');

// Set and retrieve a value to test the connection
$memcached->set('example', 'Greetings from Vultr!');
echo $memcached->get('example');
?>

Save and close the file.

The above PHP script connects to Memcached. A new key example with the value Greetings from HostMyCode is added to the Memcached memory and retrieved using the $memcached variable in the application.

Run the script using PHP to test the connection to Memcached.

php memcached.php

Output:

Greetings from HostMyCode!

Step 6: Securing Memcached

For security reasons, it's a good practice to bind Memcached to the localhost interface to prevent unauthorized access. Ensure the -l option in /etc/memcached.conf is set to 127.0.0.1:

-l 127.0.0.1

Additionally, consider setting up a firewall to restrict access to the Memcached port (11211).

Use ufw (Uncomplicated Firewall) to restrict access to the Memcached port (11211).

Enable ufw if it is not already enabled:

sudo ufw enable

Allow access only from the localhost:

sudo ufw allow from 127.0.0.1 to any port 11211

Verify the firewall rules:

sudo ufw status

Use SASL Authentication (Optional)

Memcached can be compiled with SASL support to enable authentication. If you need to secure your Memcached server with authentication, you can compile Memcached with SASL support. This is more complex and might not be necessary if you are already binding Memcached to localhost and using a firewall.

Enable Memcached on Boot. To ensure Memcached starts automatically on boot, enable the service:

sudo systemctl enable memcached

In this tutorial, we've successfully seen how to install Memcached on Ubuntu 24.04 server. You can integrate it with your applications to improve performance by caching data.

If you have any other questions or need further assistance, feel free to ask!