In this guide we will learn how to install and configure Memcached in Ubuntu 20.04.
Memcached is an open source, distributed memory object caching system. The system caches data and objects in memory to minimize the frequency with which an external database or API must be accessed. This alleviates database load and speeds up dynamic Web applications. It offers a mature, scalable, open-source solution for delivering sub-millisecond response times making it useful as a cache or session store. Memcached is a popular choice for powering real-time applications in Web, Mobile Apps, Gaming, Ad-Tech, and E-Commerce.
Unlike databases that store data on disk or SSDs, Memcached keeps its data in memory. By eliminating the need to access disks, in-memory key-value stores such as Memcached avoid seek time delays and can access data in microseconds. Memcached is also distributed, meaning that it is easy to scale out by adding new nodes. And since Memcached is multithreaded, you can easily scale up compute capacity. As a result of its speed and scalability as well as its simple design, efficient memory management, and API support for most popular languages Memcached is a popular choice for high-performance, large-scale caching use cases.
- How to Install and Configure Memcached on Rocky Linux/Alma Linux 8
- How to Install and Configure Memcached on OpenSUSE Leap 15.3
Table of Content
- Ensuring that the server is up to date
- Installing Memcached
- Start and enable memcached service
- Enable Memcached on firewall
- Configure memcached
1. Ensuring that the server is up to date
Before proceeding, it is always a good practice to ensure that the server packages are updated. Use this command to achieve that:
sudo apt update sudo apt upgrade -y
Let us also install common packages that we will need during our installation and configuration.
sudo apt install -y vim
2. Installing Memcached
Memcached packages are available in the default Ubuntu repositories. Install them using this command:
sudo apt install -y memcached
Confirm the installation of memcached
$ apt-cache policy memcached memcached: Installed: 1.5.22-2ubuntu0.2 Candidate: 1.5.22-2ubuntu0.2 Version table: *** 1.5.22-2ubuntu0.2 500 500 http://eu-west-3.ec2.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages 100 /var/lib/dpkg/status 1.5.22-2 500 500 http://eu-west-3.ec2.archive.ubuntu.com/ubuntu focal/main amd64 Packages
3. Start and enable memcached service
Use this command to start the service
sudo systemctl start memcached
Confirm that the service is started with this command:
$ sudo systemctl status memcached ● memcached.service - memcached daemon Loaded: loaded (/lib/systemd/system/memcached.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2022-01-19 20:58:17 UTC; 25s ago Docs: man:memcached(1) Main PID: 122028 (memcached) Tasks: 10 (limit: 4624) Memory: 1.6M CGroup: /system.slice/memcached.service └─122028 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached.pid Jan 19 20:58:17 ubuntusrv systemd: Started memcached daemon.
The output above is a confirmation that Memcached is up and running.
To enable the service on boot
sudo systemctl enable memcached
4. Enable Memcached on firewall
If you have ufw installed and enableed, use this command to open port 11211 on the firewall to allow traffic from the client host.
sudo ufw allow 11211/tcp
To confirm that port 11211 is opened on the firewall, execute the command.
sudo ufw app list
5. Configure memcached
The memcached config is located in this path
/etc/memcached.conf. Use this command to open the file for editing.
sudo vim /etc/memcached.conf
This is the default memcached configuration.
-d logfile /var/log/memcached.log -m 64 -p 11211 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached.pid
By default, Memcached listens to port
11211 and is configured to listen only to the
localhost system as shown in the final line. To configure Memcached so that applications from the remote systems can connect to the server, you need to change the localhost address 127.0.0.1 to the address of the remote host or to listen on all interfaces.
To use the remote host, replace the localhost address with the remote client’s IP 10.70.5.214 as shown.
To listen on all network interfaces
0.0.0.0 instead of
127.0.0.1. Change the
OPTIONS line to below.
Once done configuring, restart the memcached server to apply the changes:
sudo systemctl restart memcached
Confirm the changes with this command
$ sudo ss -tulpn | grep 11211 tcp LISTEN 0 128 10.70.5.214:11211 0.0.0.0:* users:(("memcached",pid=55522,fd=28)) tcp LISTEN 0 128 [::1]:11211 [::]:* users:(("memcached",pid=55522,fd=29))