How to Install and Configure Memcached on Ubuntu

Memcached is a free and opensource in-memory caching system that speeds up web applications by caching large volumes of data in memory that are generated from page load requests or API calls. Memcached is particularly useful in speeding up PHP-based web applications such as WordPress, Joomla, Drupal, and Python applications as well.

In this tutorial, we look at how you can install Memcached on Ubuntu. For demonstration purposes, we will use Ubuntu 20.04 Focal Fossa. Granted, the same guide will apply for Ubuntu 16.04 and later versions.

Prerequisites

As we move along, ensure you have the following in check:

Let’s now roll our sleeves and dive in.

Installing Memcached in Ubuntu Server

Before installing Memcached, let’s first update the package list of the packages installed using apt command.

$ sudo apt update
Update Ubuntu 20.04 Server
Update Ubuntu 20.04 Server

This should take a minute or two depending on the speed of your internet connection. Once the update is complete, install Memcached by running the command below. This will install Memcached alongside other dependencies and packages.

$ sudo apt install memcached libmemcached-tools
Install Memcached in Ubuntu 20.04
Install Memcached in Ubuntu 20.04

When prompted, press ‘Y’ on the keyboard and hit ENTER to proceed with the installation.

Once installed, the Memcached service should start automatically. This can be verified by checking the status of Memcached as follows.

$ sudo systemctl status memcached
Check Memcached Status
Check Memcached Status

The output confirms that Memcached is up and running.

Configuring Memcached in Ubuntu

The default configuration file for Memcached is /etc/memcached.conf. It’s also important to mention that by default, Memcached listens on port 11211 and is configured to listen on the localhost system. You can confirm this by checking the configuration file on line 35 as shown.

$ sudo nano /etc/memcached.conf
Check Memcached Configuration
Check Memcached Configuration

If the application connecting to the Memcached service is sitting on the same server where Memcached is installed, then there’s no need to make changes to this line. However, if you have a remote client that you want to allow access to Memcached caching service, then you need to edit this line & add the remote client’s IP address.

Suppose, you have a remote client with IP 192.168.2.105 running an application that needs to connect to the Memcached service. To allow access, simply delete the localhost IP address (127.0.0.1) and replace it with the remote client’s IP address. The assumption here is that both systems are in the same Local area network.

-l 192.168.2.105
Configure Memcached Connection
Configure Memcached Connection

Save and exit the configuration file.

Next, restart the Memcached service to apply the changes.

$ sudo systemctl restart memcached

Lastly, to allow remote connections to the Memcached server, we need to open the Memcached default port – port 11211 – on the firewall.

To achieve this run the commands:

$ sudo ufw allow 11211/tcp

Then reload the firewall to apply the changes.

$ sudo ufw reload

To verify that the port is open, execute:

$ sudo ufw status
Open Memcached Port on UFW Firewall
Open Memcached Port on UFW Firewall

Enabling Memcached for Applications

Depending on the application you are running, you need to install a language-specific client to enable Memcached to serve the requests.

For PHP applications such as Joomla or WordPress, execute the command below to install the additional packages:

$ sudo apt install php-memcached
Install Memcached PHP Extension
Install Memcached PHP Extension

For Python applications, ensure that the following Python libraries are installed using the pip package manager.

$ pip install pymemcache
$ pip install python-memcached

And this wraps up our topic on how to install and configure Memcached on Ubuntu. Your feedback will be much appreciated.

James Kiarie
This is James, a certified Linux administrator and a tech enthusiast who loves keeping in touch with emerging trends in the tech world. When I'm not running commands on the terminal, I'm taking listening to some cool music. taking a casual stroll or watching a nice movie.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.