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
sudoprivileges. - An internet connection to download packages from the Ubuntu repositories.
- Basic familiarity with the command line (terminal).
Step-by-Step
- Update the package index. Open a terminal and run:
sudo apt updateThis refreshes the list of available packages from the repositories.
- Install Memcached. Run:
sudo apt install memcachedWhen prompted, press Y and then Enter to confirm the installation. This will install the Memcached daemon and its default configuration.
- Check the service status. After installation, the service should start automatically. Verify it with:
sudo systemctl status memcachedYou should see
active (running)in the output. If not, start it manually withsudo systemctl start memcached. - Enable auto-start on boot. Ensure Memcached starts automatically when the system boots:
sudo systemctl enable memcachedThis creates the necessary symlinks.
- Find the configuration file. The main configuration is located at
/etc/memcached.conf. You will edit this file in the next steps. - 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 is64. Change it to a value appropriate for your server (e.g.,-m 256for 256 MB). Save and exit. - 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.1and change it to a specific IP (e.g.,-l 192.168.1.100). For security, avoid using0.0.0.0unless you have a firewall protecting the port. - Restart Memcached. Apply the changes by restarting the service:
sudo systemctl restart memcached - Verify the installation. Use the built-in stats command to check if Memcached is responding:
echo "stats" | nc 127.0.0.1 11211If
nc(netcat) is not installed, install it withsudo apt install netcat-openbsd. You should see a list of stats includinguptimeandversion. - Test with a simple set/get (optional). You can use
telnetto manually interact:telnet 127.0.0.1 11211Then type
set mykey 0 60 5, press Enter, typehello, press Enter. You should seeSTORED. To retrieve it, typeget mykeyand you’ll seeVALUE mykey 0 5followed byhello. Typequitto 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.0without 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
-mtoo 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 withsudo 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.sockto 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.confto/etc/memcached.conf.bak. - Keep it updated. Regularly run
sudo apt update && sudo apt upgradeto 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
-mvalue 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
- How to Factory Reset iPad If You Forgot Password
- Ubuntu in Windows Store: Which Editions Work?
- Secure Guest Account on Windows 11: Step-by-Step
- Stop Outlook From Changing Hyperlink Font
- Hiberfil.sys: What It Is & How to Delete It
- How to Make a Mask on Snapchat: Step-by-Step