sodcalculator

10 Tech How-To

Install Memcached on Ubuntu 20.04 LTS: Step-by-Step

Guide 10-55748 · Updated · Step by step

This guide walks you through installing Memcached on Ubuntu 20.04 LTS, configuring it for basic use, and verifying that it’s running correctly. You’ll end up with a high-performance, in-memory key-value store that can speed up your web applications by caching database queries, API responses, and session data.

What You’ll Need

  • A server or machine running Ubuntu 20.04 LTS (64-bit recommended).
  • Access to a user account with sudo privileges.
  • An internet connection to download packages from the Ubuntu repositories.
  • Basic familiarity with the command line (terminal).

Step-by-Step

  1. Update the package index. Open a terminal and run:
    sudo apt update

    This refreshes the list of available packages from the repositories.

  2. Install Memcached. Run:
    sudo apt install memcached

    When prompted, press Y and then Enter to confirm the installation. This will install the Memcached daemon and its default configuration.

  3. Check the service status. After installation, the service should start automatically. Verify it with:
    sudo systemctl status memcached

    You should see active (running) in the output. If not, start it manually with sudo systemctl start memcached.

  4. Enable auto-start on boot. Ensure Memcached starts automatically when the system boots:
    sudo systemctl enable memcached

    This creates the necessary symlinks.

  5. Find the configuration file. The main configuration is located at /etc/memcached.conf. You will edit this file in the next steps.
  6. Adjust memory usage (optional). Open the config file with a text editor, e.g., sudo nano /etc/memcached.conf. Look for the line starting with -m. This sets the maximum memory in megabytes. The default is 64. Change it to a value appropriate for your server (e.g., -m 256 for 256 MB). Save and exit.
  7. Set the listening IP (optional but recommended). By default, Memcached listens on 127.0.0.1 (localhost only). If you need to allow remote connections, find the line -l 127.0.0.1 and change it to a specific IP (e.g., -l 192.168.1.100). For security, avoid using 0.0.0.0 unless you have a firewall protecting the port.
  8. Restart Memcached. Apply the changes by restarting the service:
    sudo systemctl restart memcached
  9. Verify the installation. Use the built-in stats command to check if Memcached is responding:
    echo "stats" | nc 127.0.0.1 11211

    If nc (netcat) is not installed, install it with sudo apt install netcat-openbsd. You should see a list of stats including uptime and version.

  10. Test with a simple set/get (optional). You can use telnet to manually interact:
    telnet 127.0.0.1 11211

    Then type set mykey 0 60 5, press Enter, type hello, press Enter. You should see STORED. To retrieve it, type get mykey and you’ll see VALUE mykey 0 5 followed by hello. Type quit to exit.

Common Mistakes

  • Forgetting to restart after config changes. Editing the config file has no effect until you restart the service. Always run sudo systemctl restart memcached.
  • Opening the port to the world. Setting -l 0.0.0.0 without a firewall exposes Memcached to the internet, which can lead to data theft or abuse. Use a specific IP or keep it on localhost.
  • Running out of memory. Setting -m too high can cause the system to swap or crash. Set it to a reasonable value based on your available RAM.
  • Using a client library that is not compatible. Ensure your application’s Memcached client library supports the version you installed (Memcached 1.6.x on Ubuntu 20.04).
  • Ignoring the firewall. If you enable remote access, make sure your firewall (ufw) allows only trusted IPs to connect to port 11211.

Tips

  • Monitor performance. Use memcached-tool (install with sudo apt install libmemcached-tools) to view stats like hit ratio and evictions.
  • Use Unix sockets for local-only access. For better performance and security, you can configure Memcached to listen on a Unix socket instead of a TCP port. Add -s /tmp/memcached.sock to the config.
  • Set appropriate expiration times. In your application, use sensible TTLs (time-to-live) for cached data to avoid stale data and memory waste.
  • Back up your config. Before major changes, copy /etc/memcached.conf to /etc/memcached.conf.bak.
  • Keep it updated. Regularly run sudo apt update && sudo apt upgrade to get security patches.

Troubleshooting

  • Service fails to start. Check the logs with sudo journalctl -u memcached. Common causes: invalid config syntax, port already in use, or insufficient permissions on the socket file.
  • Cannot connect from another machine. Verify that Memcached is listening on the correct IP (sudo netstat -tulpn | grep 11211). Also check your firewall rules: sudo ufw status. If the firewall is active, allow the port from specific IPs: sudo ufw allow from 192.168.1.0/24 to any port 11211.
  • Memory usage is too high. Reduce the -m value in the config and restart. Also check if your application is setting excessive expiration times or storing large objects.
  • Performance is poor. Monitor hit ratio with memcached-tool 127.0.0.1:11211 stats. A low hit ratio means your cache isn’t being used effectively. Check your application’s caching logic.
  • Authentication errors. Memcached does not have built-in authentication. If you need access control, use a firewall or a SASL-enabled build (not default). For most use cases, keep it on localhost.

Frequently Asked Questions

What is Memcached used for?

Memcached is an in-memory key-value store used to cache data in RAM, reducing database load and speeding up dynamic web applications. It stores objects like database query results, API responses, and session data.

How do I uninstall Memcached?

Run sudo apt remove memcached. To also remove configuration files, use sudo apt purge memcached. Then optionally run sudo apt autoremove to clean up unused dependencies.

Can I use Memcached with PHP?

Yes. Install the PHP extension with sudo apt install php-memcached (for PHP 7.4) or php8.1-memcached for PHP 8.1. Then restart your web server.

How do I change the port Memcached listens on?

Edit the -p option in /etc/memcached.conf. For example, -p 11212. Restart the service after the change.

Is Memcached persistent?

No. Memcached stores data only in RAM. When the service stops or the system reboots, all cached data is lost. For persistence, consider Redis or a database.

How do I check the Memcached version?

Run memcached -V or echo "version" | nc 127.0.0.1 11211.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →