How to Install Memcached on Ubuntu 18.04
3 min read
Memcached is a free and open-source high-performance in-memory key-value data store. It is most commonly used to speed up applications by caching various objects from the results of database calls.
In this tutorial, we will cover the process of installing and configuring the latest version of Memcached on Ubuntu 18.04. The same instructions apply for Ubuntu 16.04 and any Ubuntu-based distribution.
Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges.
Memcached packages are included in the default Ubuntu 18.04 repositories. The installation is pretty straightforward, just follow the steps below:
Start by updating the apt packages list:
sudo apt update
Install Memcached by typing:
sudo apt install memcached libmemcached-tools
libmemcached-toolspackage contains provides several command line tools for managing the Memcached server.
Once the installation is completed, the Memcached service will start automatically. To check the status of the service, enter the following command:
sudo systemctl status memcached
The output will look like this:
● memcached.service - memcached daemon Loaded: loaded (/lib/systemd/system/memcached.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2019-04-30 15:13:41 PDT; 37s ago Docs: man:memcached(1) Main PID: 10753 (memcached) Tasks: 10 (limit: 2319) CGroup: /system.slice/memcached.service `-10753 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached.pid
That's it, at this point you have Memcached installed and running on your Ubuntu 18.04 server.
Memcached can be configured by editing the
/etc/memcached.conf file. The default configuration settings are sufficient for most users.
By default, Memcached is configured to listen on localhost only. If the client connecting to the server is also running on the same host you don’t need to change the default configuration file.
When improperly configured Memcached can be used to perform a distributed denial-of-service (DDoS) attack. If you want to allow remote access to your Memcached server, you need to configure your firewall and allow access to the Memcached UDP port 11211 only from trusted clients.
The following example assumes that you want to connect to your Memcached server over a private network. The server IP is
192.168.100.20 and the client's IP address is
Ubuntu comes with a firewall configuration tool called UFW. By default, UFW is installed but not enabled. Before enabling the UFW firewall first add a rule that will allow incoming SSH connections:
sudo ufw allow 22
Allow assess from the remote client IP address:
sudo ufw allow from 192.168.100.30 to any port 11211
Enable UFW with by typing:
sudo ufw status
Once your firewall is configured the next step is to edit the Memcached configuration and set the Memcached service to listen on the server's private networking interface:
To do so, open the
memcached.conf configuration file:
sudo nano /etc/memcached.conf
Locate the line that begins with
-l 127.0.0.1 and replace
127.0.0.1 with the server IP address
# Specify which IP address to listen on. The default is to listen on all IP addresses # This parameter is one of the only security measures that memcached has, so make sure # it's listening on a firewalled interface. -l 192.168.100.20
Restart the Memcached service for the changes to take effect:
sudo systemctl restart memcached
You can now connect to the Memcached server from your remote location.
Connecting to Memcached
To connect to the Memcached server you need to use a language-specific client.
sudo apt install php-memcached
pip install pymemcache
pip install python-memcached
You have learned how to install Memcached on your Ubuntu server. For more information on this topic consult Memcached Wiki.