How to Install Memcached on Ubuntu 20.04

Memcached or Memory Object Caching System is an open-source program that extracts information from a database. This process is covered using an object caching program. As a Linux user, it is essential to install and secure Memcached on Ubuntu for PHP applications.

Memcached optimizes backend database performance using an object caching program. This process temporarily stores information in memory. This retains the recently accessed or frequently requested records in the memory. This, in turn, reduces the requests sent back to the database. Also, Memcached enables PHP or Python-based applications to run smoothly with very little latency. 

In this article, we will cover how to install Memcached on Ubuntu systems. It will also cover how to configure it and add authentication to secure Memcached. 


For this tutorial, you need a running Ubuntu system with sudo privileges or root access. The Ubuntu version should be 20.04.

Install Memcached on Ubuntu

1. Update and Install Memcached

Before starting with the installation of Memcached, it is essential to update the required packages and distributions. Execute the following command for this step:

sudo apt update

Next, we will install the Memcached official package using the install command.

sudo apt install memcached
Install Memcached

We will also install a libmemcached-tools package. This package consists of tools that we can use to examine and test our Memcached server. We will install this using the following command:

sudo apt install libmemcached

The package is installed successfully when you get a similar output. 

Next, we will start the Memcached service on our Ubuntu machine. To start the service, we will use the systemctl command. We will write the keyword “start” followed by the service name with the systemctl command. 

For example: 

sudo systemctl start memcached

The Memcached service has successfully started. Now we will move to its configuration.

2. Configure Memcached

For Memcached configuration, we will make changes in its configuration file. This file is located in the /etc/ directory. If your Memcached server will only support IPV4 connections, then you can head over to step 3 and skip this part. This configuration is required when a Memcached server needs to support UDP sockets or IPV6 connections. 

Firstly, confirm that the Memcached server is running on its local IP, which is 127.0.07. We will confirm this using the ss command:

sudo ss -plunt  

You will get a similar output as shown below:

Install Memcached on Ubuntu

This output confirms that the Memcached server is using its local IPV4 address. 

Secondly, you will configure the Memcached configuration file to support IPV6 addresses. You can also use this file to configure Memcached to support UDP or Unix Domain Sockets. However, in this tutorial, we will only cover the configuration of IPV6 connections. 

Open the configuration file using the sudo command:

sudo nano /etc/memcached.conf

Thirdly, look for the lines consisting of -l This line represents the Memcached configuration to listen to IPV4 connections. Here, you will add the IPV6 local loopback address, which is::1.

-l ::1 
IPV6 Config

Save the file using Ctrl + O. To exit the nano editor, press Ctrl + X

Now, restart the Memcached to implement the changes.

sudo systemctl restart memcached

Next, enable the Memcached service using the systemctl command:

sudo systemctl enable memcached.service

After you have enabled the service, you can confirm that Memcached now supports IPV6 connections by using the ss command.

sudo ss -plunt

You will get a similar output as shown below:

Install Memcached on Ubuntu

If you wish to disable the support for IPV4 connections, you can simply remove the IPV4 local address from the configuration file. Next, save the file using Ctrl + O, and restart the server using the systemctl command. 

sudo systemctl restart memcached

Finally, you have configured the Memcached to support IPV6 connections.

3. Configure PHP to Use Memcached

Now that you have configured the Memcached. You can set up a PHP-based application to use Memcached. PHP-based applications consist of WordPress, Joomla, and Drupal. To learn how to install PHP, check out our guide. Here, we have installed a php-memcached extension. 

sudo apt install php-memcached
Install Memcached on Ubuntu

Hence, Memcached support is enabled in PHP. Alternatively, you can install PHP and Apache servers instead of Memcached PHP extension. For this, you will execute the following command:

apt-get install apache2 libapache2-mod-php php php-cli php-memcached -y

Next, create a PHP file in the Apache root directory:

nano /var/www/html/phpinfo.php

Thirdly, edit the PHP file, and add this code:


Lastly, restart the Apache service to implement the changes:

systemctl restart apache2

To test the Memcached support, open your web browser. Then, type your IP address followed by phpinfo.php file.


You will get a similar output as shown below:

Memcached on Ubuntu

From the above image, you can see that Memcached support is enabled. 

4. Other Language Configurations for Memcached

You can also enable other configurations as well such as Python and Perl. For Python configuration, we will install the Python Memcached extension. For this, we will execute the following command:

apt-get install python3-pymemcache -y

Alternatively, you can use the pip command to install the Python Memcached library.

pip install pymemcache

Similarly, for Perl support, you can execute: 

apt-get install libcache-memcached-libmemcached-perl -y

This will install the Perl package for Memcached. 

Block Remote Access to Memcached on Ubuntu

While using the Memcached server, there is a risk of distributed denial-of-service (DDoS) if Memcached is not configured properly. While configuring remote access on Memcached, make sure to only allow trusted clients. 

Firstly, we will set up the Ubuntu firewall to block all IP Addresses except those that belong to the trusted client or are allowed on port 11211. To perform this step, type the following command and hit Enter key to complete the execution. 

sudo ufw allow <port_range> 11211

In this tutorial, you learned how to configure Memcached with TCP IPv4 IPv6 connections. You also learned how to configure Memcached to support PHP-based applications. Lastly, you learned how to add support and configuration for languages other than PHP, which included Python and Perl. 

To learn more about Memcached, check out its official documentation.

If this guide helped you, please share it.

Leave a Reply
Related Posts