How to Install Memcached on Ubuntu 24.04

Updated on July 9, 2024
How to Install Memcached on Ubuntu 24.04 header image

Introduction

Memcached is an open-source distributed memory caching system that stores data and objects such as frequent database queries, API calls, expensive computations, session data, or temporary tokens in RAM. Memcached integrates with modern applications to reduce the number of frequent operations such as Database calls to improve the application response time and server resource usage.

This article explains how to install Memcached on Ubuntu 24.04 and configure the application to enable secure connections using the SASL (Simple Authentication and Security Layer) mechanism.

Prerequisites

Before you begin:

Install Memcached

Memcached is available in the default Ubuntu 24.04 repositories and you can install the package using the APT package manager. In addition, you can compile and install a specific version using the application source code. In the following steps, install Memcached and enable it to run on your server.

  1. Install Memcached and required add-on tools.

    console
    $ sudo apt install memcached libmemcached-tools -y
    
  2. View the installed Memcached version on your server.

    console
    $ memcached --version
    

    Output:

    memcached 1.6.24
  3. Enable the Memcached service to automatically start at boot time.

    console
    $ sudo systemctl enable memcached
    
  4. Start the Memcached service.

    console
    $ sudo systemctl start memcached
    

Configure Memcached

Memcached uses configurations in the /etc/memcached.conf file to run on your server. You can set up the Memcached port, and interface configurations such as the default port, connection limits, memory and IP address to listen for incoming connections on your server. In the following steps, configure Memcached and verify the process details to run on your server.

  1. Open the Memcached configuration file /etc/memcached.conf using a text editor such as Nano.

    console
    $ sudo nano /etc/memcached.conf
    
  2. Add -S at the end of the file after -P /var/run/memcached/memcached.pid to enable SASL authentication on your server.

    ini
    ...
    -P /var/run/memcached/memcached.pid
    -S
    
    • Find and uncomment the -v directive to enable verbose logging to the /var/log/memcache file.
    ini
    -v
    
    • Uncomment the -c 1024 directive to limit the number of simultaneous Memcached connections. Replace 1024 with your desired number of connections.
    ini
    -c 1024
    
    • Find the following Memcached port directive to verify the application connection port on your server.
    ini
    # Default connection port is 11211
    -p 11211
    
    • Find the following listen directive and verify that it's set to your server's loopback address 127.0.0.1 to only accept local Memcached connections. Change the address to your public IP or Vultr VPC address when using Memcached on a standalone server to allow external connections.
    ini
    -l 127.0.0.1
    -l ::1
    

    Save and close the file.

  3. Restart Memcached to apply the configuration changes.

    console
    $ sudo systemctl restart memcached
    

Secure Memcached

Memcached does not run with any authentication protocol unless enabled in your configuration making it insecure and open to all requests. The SASL protocol enables authentication and requires a valid user to access Memcached. Follow the steps below to install the SASL package and secure Memcached on your server.

  1. Install the SASL package on your server.

    console
    $ sudo apt install sasl2-bin -y
    
  2. Create a new directory to store your SASL authentication credentials.

    console
    $ sudo mkdir /etc/sasl2
    
  3. Create a new SASL configuration file to use with Memcached. For example, memcached.conf.

    console
    $ sudo nano /etc/sasl2/memcached.conf
    
  4. Add the following contents to the file.

    ini
    log_level: 5
    mech_list: plain
    sasldb_path: /etc/sasl2/memcached-sasldb2
    

    Save and close the file

    The above SASL configuration enables authentication using the Memcached database. Within the configuration:

    • log_level: Enables logging. The value 5 enables high-detail logs.
    • mech_list: Sets the authentication mechanism. The value plain enables plain username and password usage.
    • sasldb_path: Specifies the Memcached SASL database file to use for authentication.
  5. Create a user using the saslpasswd utility. Replace example-user with your actual username to enable it in your Memcached database.

    console
    $ sudo saslpasswd2 -a memcached -c -f /etc/sasl2/memcached-sasldb2 example-user
    

    Enter a strong password for the new user when prompted.

  6. Grant the Memcached user memcache full privileges to the /etc/sasl2/memcached-sasldb2 database file.

    console
    $ sudo chown memcache:memcache /etc/sasl2/memcached-sasldb2
    
  7. Restart Memcached to apply the configuration changes.

    console
    $ sudo systemctl restart memcached
    
  8. Connect to Memcached to test your new user credentials. Replace example-user and strong-password with your actual user details.

    console
    $ memcstat --servers="127.0.0.1" -b --username=example-user --password=strong-password
    

    Your output should look like the one below when successful.

    Server: 127.0.0.1 (11211)
    pid: 8317
    uptime: 345
    time: 1716925269
    version: 1.6.24
    libevent: 2.1.12-stable
    pointer_size: 64
    rusage_user: 0.045174
    rusage_system: 0.024324
    max_connections: 1024
    curr_connections: 2
    total_connections: 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.

  1. Install PHP and the Memcached module.

    console
    $ sudo apt install php php-memcached -y
    
  2. Create a new sample PHP script to connect to Memcached.

    console
    $ nano memcached.php
    
  3. Add the following contents to the file. Replace example-user and strong-password with your actual user credentials.

    php
    <?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 using your example user details and the binary protocol. A new key example with the value Greetings from Vultr is added to the Memcached memory and retrieved using the $memcached variable in the application.

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

    console
    $ php memcached.php
    

    Output:

    Greetings from Vultr!

    Based on the above output, the PHP script successfully connects to Memcached, creates the example key and writes a new value Greetings from Vultr to retrieve from memory.

Conclusion

You have installed Memcached on an Ubuntu 22.04 server and secured the application to integrate with application frameworks such as PHP. Memcached improves your server performance and integrates with dynamic web applications to store repeated queries such as database calls in memory. For more information, visit the Memcached wiki repository.