How To Install Memcached on Ubuntu 20.04

Overview

In this tutorial we’ll learn how to install Memcached on Ubuntu 20.04 (LTS) Focal Fossa.

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.

For installing memcached on previous version of Ubuntu LTS you can read:

Prerequisites

This tutorial assumes you have a fresh install of Ubuntu 20.04 Server. You can also follow this tutorial on any Ubuntu 20.04 flavors.

Using Ubuntu server will give minimalist installation of Ubuntu.

Install Memcached on Ubuntu 20.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 11211


$ 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[1]: Started memcached daemon.

Since Ubuntu 20.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[1]: Started memcached daemon.

Configuring Memcached

Now we have memcached running let’s learn how to configure memcached. Memcached comes with two configuration files.

  • /etc/default/memcached
  • /etc/memcached.conf

We can enable or disable memcached on boot by changing parameter on /etc/default/memcached file. The default value on this file is


ENABLE_MEMCACHED=yes

To disable memcached on boot we need to change the line above to


ENABLE_MEMCACHED=no

Now let’s check /etc/memcached.conf configuration file.

Memcached Logging on Ubuntu 20.04

Memcached’s 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

```bash

# -vv

```

Now, restart our memcached installation

```bash

sudo systemctl restart memcached

```

We can see memcached logs by running the following command

```bash

sudo journalctl | grep memcached

```

### Memcached Memory Limit {#h-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 `-m` option.

To increase memory limit to 1GB for example, we can change the line below

```bash
-m 64
```

to

```bash
-m 1024
```

### Memcached Port {#h-memcached-port}

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.

```bash
-p 11211
```

### User Running Memcached Process {#h-user-running-memcached-process}

This options is to configure which user run memcached process. We will rarely need to change this.

```bash
-u memcache
```

### Memcached listen address {#h-memcached-listen-address}

By default memcached will listen on all network interfaces. We can configure on which address memcached listen to.

```bash
-l 127.0.0.1
```

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 {#h-memcache-limit-incoming-connections}

We can limit connection using `-c` option. The default value is 1024 connections.

```bash
-c 1024
```

### Change Memcache Item Size Limit {#h-change-memcache-item-size-limit}

We can change memcached item size limit by adding the `-I` option on `/etc/memcached.conf`. The Default value for this option is 1MB. The smallest item size limit is 1K. The largest item size limit is 1G (however, some outdated docs and tutorial might still say the upper limit is 128MB).

To Change the limit you can open `/etc/memcached.conf` and add the following line. The sample below will change memcached item size limit to 16M. You can use K or G in exchange for M below.

```
-I 16M
```

## Summary {#h-summary}

In this tutorial we learn how to install memcached on Ubuntu 20.04 (Focal Fossa). Then, 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.

## References {#h-references}

You can visit links below for further references about memcached.

  * [Memcached Website][2]
  * [Memcached Github Repository][3]
  * [Understanding Memcached][4]

 [1]: https://www.howtodojo.com/install-memcached-on-ubuntu-18-04/
 [2]: https://memcached.org/
 [3]: https://github.com/memcached/memcached
 [4]: https://www.howtodojo.com/understanding-memcached/