Memcached (pronounced: mem-cash-dee) is a free, high performance, distributed memory object caching system.
Memcached can be use for any caching usage but mostly used by dynamic web application to reduce database load. We can also cache API calls and page rendering.
In this tutorial we’ll learn how to install Memcached on Ubuntu 18.04 (LTS) Bionic Beaver.
For installing memcached on previous version of Ubuntu LTS you can read:
- How to install memcached on Ubuntu 14.04 (LTS) Trusty Tahr
- How to install memcached on Ubuntu 18.04 (LTS) Xenial Xerus
This tutorial assumes you have a fresh install of Ubuntu server 18.04. You can also follow this tutorial on any Ubuntu 18.04 flavors. Using Ubuntu server will give minimalist installation of Ubuntu.
Install Memcached on Ubuntu 18.04
Let’s update our base system to latest update using command below.
sudo apt-get update sudo apt-get upgrade
Install memcached by running command below
sudo apt-get install memcached
That’s it, memcached installed. Now let’s check whether memcached already started and listen to specific port.
Run netstat and find memcached process.
$ sudo netstat -naptu | grep memcached tcp 0 0 127.0.0.1:11211 0.0.0.0:* LISTEN 3125/memcached udp 0 0 127.0.0.1:11211 0.0.0.0:* 3125/memcached
Or we can also run run netstat and find memcached default port
$ sudo netstat -naptu | grep 11211 tcp 0 0 127.0.0.1:11211 0.0.0.0:* LISTEN 3125/memcached udp 0 0 127.0.0.1:11211 0.0.0.0:* 3125/memcached
We can also use ss as replacement of netstat to check where memcached listen.
ss -4n state listening | grep 11211 tcp 0 128 127.0.0.1:11211 *:*
We can check memcached service status by running command below. This is sysv compatible service command.
ubuntu@ubuntu-xenial:~$ sudo service memcached status ● memcached.service - memcached daemon Loaded: loaded (/lib/systemd/system/memcached.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2018-09-29 02:24:57 UTC; 53s ago Main PID: 2351 (memcached) CGroup: /system.slice/memcached.service └─2351 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 Sep 29 02:24:57 ubuntu-xenial systemd: Started memcached daemon.
Since Ubuntu 18.04 already use systemd, we can also
systemctl command to check memcached service status.
$ sudo systemctl status memcached ● memcached.service - memcached daemon Loaded: loaded (/lib/systemd/system/memcached.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2018-09-29 02:24:57 UTC; 1min 4s ago Main PID: 2351 (memcached) CGroup: /system.slice/memcached.service └─2351 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 Sep 29 02:24:57 ubuntu-xenial systemd: Started memcached daemon.
Now we have memcached running let’s learn how to configure memcached. Memcached comes with two configuration files.
We can enable or disable memcached on boot by changing parameter on
/etc/default/memcached file. The default value on this file is
To disable memcached on boot we need to change the line above to
Now let’s check
/etc/memcached.conf configuration file.
Memcached Logging on Ubuntu 18.04
logging directive is skipped by systemd-memcached-wrapper. This script located in
/usr/share/memcached/scripts/systemd-memcached-wrapper. In this scipt you can find line:
my $ignore_directives = ("logfile");
This doesn’t mean you cannot see memcached logs. The logs will be captured by systemd and we can use
journalctl to see the log.
Let’s test by increasing the verbosity of the logs. Open
/etc/memcached.conf. Find and uncomment this line
Now, restart our memcached installation
sudo systemctl restart memcached
We can see memcached logs by running the following command
sudo journalctl | grep memcached
Memcached Memory Limit
By default memcached will use 64 MB of memory. Memcached doesn’t reserve the memory on start but the memory usage will grow as needed with the limit as specified in
To increase memory limit to 1GB for example, we can change the line below
We can configure memcached port using
-p option. By default memcached use port 11211. If you change this port to non default you need to configure your application to also pointing to the same port.
User Running Memcached Process
This options is to configure which user run memcached process. We will rarely need to change this.
Memcached listen address
By default memcached will listen on all network interfaces. We can configure on which address memcached listen to.
Please note that this is the only security mechanism that memcached have. If you plan to open your memcached server from another server ensure you add firewall to the memcached server.
If you use cloud service like Amazon Web Services you can use security groups.
You can use iptables if you use provider that doesn’t offer firewall or you install on your own infrastructure.
Memcache Limit incoming connections
We can limit connection using
-c option. The default value is 1024 connections.
In this tutorial we learned how to install memcached on Ubuntu 18.04 Bionic Beaver. We also learn how to do basic configuration of memcached.
I hope this tutorial is useful to help you installing and configuring memcached.
Introducing cache on your stack can improve the performance of your web application. Until next time.