---
title: How to Install Memcached on Ubuntu 22.04
url: https://docs.vultr.com/how-to-install-memcached-on-ubuntu-2204
description: Learn how to install Memcached on Ubuntu 22.04 with our step-by-step guide. Improve your application performance with this powerful caching system.
publish_date: 2025-04-29T11:50:37.198000Z
last_updated: 2025-04-29T12:09:09.267000Z
author: Aashish Chaurasiya
header_image: https://docs.vultr.com/public/doc-assets/2170/ece492aa-e54a-4af2-afa6-336add7b99e6.png
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

Memcached is an open-source distributed caching system that stores data like database queries, API calls, and session data in RAM. It helps reduce frequent operations, improving application response time and server resource usage.

In this article, you are to install Memcached on Ubuntu 22.04 and configure it to enable secure connections using the SASL (Simple Authentication and Security Layer) mechanism.

## Prerequisites

Before you begin:

* Have an [Ubuntu 22.04 server](https://www.vultr.com/servers/ubuntu/).

* Access the server using SSH as a non-root user with sudo privileges.

* [Update the server](https://docs.vultr.com/how-to-update-a-vultr-cloud-server).

## Install Memcached

Memcached is available in the default Ubuntu 22.04 repositories and can be installed via the APT package manager. Alternatively, you can compile and install a specific version from the source code. Follow the steps below to install and enable Memcached on your server.

1. Update the server package index.

    ```console
    $ sudo apt update
    ```

1. Install Memcached and required add-on tools.

    ```console
    $ sudo apt install memcached libmemcached-tools -y
    ```

1. View the installed Memcached version on your server.

    ```console
    $ memcached --version
    ```

    Output:

    ```
    memcached 1.6.14
    ```

1. Enable the Memcached service to automatically start at boot time.

    ```console
    $ sudo systemctl enable memcached
    ```

1. Start the Memcached service.

    ```console
    $ sudo systemctl start memcached
    ```

## Configure Memcached

Memcached uses the `/etc/memcached.conf` file for configurations, including the port, connection limits, memory, and IP address to listen for incoming connections. Follow the steps below to configure Memcached and verify its process details 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
    ```

1. 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
    ```

1. Find and uncomment the `-v` directive to enable verbose logging to the `/var/log/memcache` file.

    ```ini
    -v
    ```

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

    ```ini
    -c 1024
    ```

1. Find the following Memcached port directive to verify the application connection port on your server.

    ```ini
    # Default connection port is 11211
    -p 11211
    ```

1. Locate the listen directive and ensure it is set to the server's loopback address `127.0.0.1` to accept only local Memcached connections. If using Memcached on a standalone server, change the address to your public IP or Vultr VPC address to allow external connections.

    ```ini
    -l 127.0.0.1
    -l ::1
    ```

    Save and close the file.

1. 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
    ```

1. Create a new directory to store your SASL authentication credentials.

    ```console
    $ sudo mkdir /etc/sasl2
    ```

1. Create a new SASL configuration file to use with Memcached. For example, `memcached.conf`.

    ```console
    $ sudo nano /etc/sasl2/memcached.conf
    ```

1. 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. A value of `5` sets high-detail logging.
    * **`mech_list`**: Sets the authentication mechanism. `plain` enables username and password authentication.
    * **`sasldb_path`**: Specifies the path to the SASL database file for Memcached authentication.

1. Create a user using the `saslpasswd2` utility. Replace `example-user` with your actual username to add it to the Memcached SASL database.

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

    Enter a strong password for the new user when prompted.

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

    ```console
    $ sudo chown memcache:memcache /etc/sasl2/memcached-sasldb2
    ```

1. Restart Memcached to apply the configuration changes.

    ```console
    $ sudo systemctl restart memcached
    ```

1. Check the status of Memcached server.

    ```console
    $ sudo systemctl status memcached
    ```

1. List all the created user in SASL authentication database.

    ```console
    $ sudo sasldblistusers2 -f /etc/sasl2/memcached-sasldb2
    ```

    Output:

    ```
    example-user@ubuntu22: userPassword
    ```

1. Connect to Memcached to test your new user credentials. Replace `example-user@ubuntu22` and `strong-password` with your actual user details.

    ```console
    $ memcstat --binary --servers="127.0.0.1" --username='example-user@ubuntu22'  --password=strong-password
    ```

    Your output should look like the one below when successful.

    ```
    Server: 127.0.0.1 (11211)
        pid: 3988
        uptime: 369
        time: 1743878609
        version: 1.6.14
        libevent: 2.1.12-stable
        pointer_size: 64
        rusage_user: 0.058393
        rusage_system: 0.023357
        max_connections: 1024
        curr_connections: 2
        total_connections: 5
    ```

## Connect to Memcached

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

1. Install PHP and the Memcached module.

    ```console
    $ sudo apt install php php-memcached -y
    ```

1. Create a new sample PHP script to connect to Memcached.

    ```console
    $ nano memcached.php
    ```

1. Add the following contents to the file. Replace `example-user@ubuntu22` 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@ubuntu22', '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 and the binary protocol. It sets a new key `example` with the value `Greetings from Vultr!` and retrieves it using the `$memcached` variable.

1. 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

In this article, you installed and secured Memcached on an Ubuntu 22.04 server. Memcached improves server performance by caching repeated operations like database queries and integrates easily with dynamic web applications.
