DEV Community

Cover image for Installing Memcached on Ubuntu 26.04
Aashish Chaurasiya for Vultr

Posted on • Originally published at docs.vultr.com

Installing Memcached on Ubuntu 26.04

Memcached is a high-performance distributed memory caching system that reduces database load by storing frequently accessed data in RAM. This guide installs Memcached on Ubuntu 26.04, configures SASL authentication to secure access, and verifies the setup with a PHP script that stores and retrieves a cached value. By the end, you'll have a secured Memcached instance ready for application use.


Install Memcached

Memcached is available in Ubuntu 26.04's default APT repository.

1. Update the APT package index:

$ sudo apt update
Enter fullscreen mode Exit fullscreen mode

2. Install Memcached and the management tools:

$ sudo apt install memcached libmemcached-tools -y
Enter fullscreen mode Exit fullscreen mode

What you just installed:

  • memcached: the caching server daemon
  • libmemcached-tools: CLI utilities for inspecting and managing Memcached

3. Verify the installed version:

$ memcached --version
Enter fullscreen mode Exit fullscreen mode

Manage the Memcached Service

Enable Memcached to start automatically when the server boots.

1. Enable and start the service:

$ sudo systemctl enable memcached
$ sudo systemctl start memcached
Enter fullscreen mode Exit fullscreen mode

2. Check the service status:

$ sudo systemctl status memcached
Enter fullscreen mode Exit fullscreen mode

3. Stop or restart the service when needed:

$ sudo systemctl stop memcached
$ sudo systemctl restart memcached
Enter fullscreen mode Exit fullscreen mode

Configure Memcached

1. Open the configuration file:

$ sudo nano /etc/memcached.conf
Enter fullscreen mode Exit fullscreen mode

Verify the following settings are present:

-l 127.0.0.1
-l ::1
-p 11211
-c 1024
-S
Enter fullscreen mode Exit fullscreen mode

These settings bind Memcached to localhost only, set the port to 11211, cap connections at 1024, and enable SASL support.

2. Restart Memcached to apply changes:

$ sudo systemctl restart memcached
Enter fullscreen mode Exit fullscreen mode

Enable SASL Authentication

1. Install the SASL utilities:

$ sudo apt install sasl2-bin -y
Enter fullscreen mode Exit fullscreen mode

2. Create the SASL configuration file:

$ sudo nano /etc/sasl2/memcached.conf
Enter fullscreen mode Exit fullscreen mode
log_level: 5
mech_list: plain
sasldb_path: /etc/sasl2/memcached-sasldb2
Enter fullscreen mode Exit fullscreen mode

3. Create a Memcached user:

$ sudo saslpasswd2 -a memcached -c -f /etc/sasl2/memcached-sasldb2 example_user
Enter fullscreen mode Exit fullscreen mode

4. Set the correct ownership on the credential database:

$ sudo chown memcache:memcache /etc/sasl2/memcached-sasldb2
Enter fullscreen mode Exit fullscreen mode

5. Restart Memcached:

$ sudo systemctl restart memcached
Enter fullscreen mode Exit fullscreen mode

Test with PHP

1. Install PHP and the Memcached extension:

$ sudo apt install php php-memcached -y
Enter fullscreen mode Exit fullscreen mode

2. Create a test script:

$ nano memcached_test.php
Enter fullscreen mode Exit fullscreen mode
<?php
$memcached = new Memcached();
$memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
$memcached->addServer('127.0.0.1', 11211);
$memcached->setSaslAuthData('example_user', 'your_password');
$memcached->set('test_key', 'Hello from Memcached on Ubuntu 26.04');
echo $memcached->get('test_key');
?>
Enter fullscreen mode Exit fullscreen mode

3. Run the script:

$ php memcached_test.php
Enter fullscreen mode Exit fullscreen mode

The output Hello from Memcached on Ubuntu 26.04 confirms SASL authentication and caching are working correctly.


Next Steps

Memcached is running and secured with SASL authentication. From here you can:

  • Integrate Memcached with a PHP application using the php-memcached extension
  • Use memstat to monitor cache hit rates and memory usage
  • Deploy Memcached as a session storage backend for high-traffic web applications

For the full guide with additional tips, visit the original article on Vultr Docs.

Top comments (0)