Install Memcached on Ubuntu 20.04 - Step by step process ?

One of the issues that impact the performance of web applications is high database loads.

A high load increases the latency and causes slowdowns in your web applications. 

On production servers, this is highly undesirable and can make you lose potential customers. One of the ways of speeding up your applications is investing in a solid caching system.

Memcached is an open-source high-performance distributed caching system that helps speed up your web applications by alleviating the database load. 

It stores data objects in dynamic memory, which is a temporary memory storage location – instead of applications always accessing the data afresh from the database. 

The cached data alleviates the workload and make it much easier for the application to retrieve the required data.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform Linux related installation queries.

In this context, we shall look into how to install Memcached on Ubuntu 20.04 LTS.


Steps to install Memcached on Ubuntu:

Ensure that you log into your Server as the root user with sudo privileges'.

1. Install Memcached

Installing Memcached is quite easy and straightforward. This is largely due to the fact that it is available in Ubuntu 20.04 repositories, and it's, therefore, possible to install it via the APT package manager.

But first, let update the package lists on our system:

$ sudo apt update

Next, we are going to install the Memcached package along with a CLI utility known as libmemcached-tools as follows:

$ sudo apt install memcached libmemcached-tools

When prompted, hit 'Y' to get along with the installation of Memcached.


2. Check the status of Memcached

By default, Memcached listens on port 11211. You can verify this by using the netstat command as shown:

$ sudo netstat -pnltu

Additionally, you can use systemd to confirm if the Memcached daemon is running:

$ sudo systemctl status memcached

From the output above, we can clearly observe that the Memcached service is active and running. If by any chance, Memcached is not running, you can start the service as shown.

$ sudo systemctl start memcached

To make it autostart on boot time, enable Memcached as follows:

$ sudo systemctl enable memcached

To view server statistics, execute:

$ memcstat --servers localhost

You will get a flurry of information such as the PID, uptime ( seconds ) time, version and so much more.


3. Configure Memcached

If Memcached is sitting on the webserver where your web application is running, no configuration is needed since Memcached, by default, is set to run on the localhost system.

However, if Memcached is installed on a separate server, some added configuration is needed for the webserver to make a remote connection to the Memcached server.

Let's assume that the client IP – where the web application is hosted – is 192.168.2.10 and the Memcached server IP is 192.168.2.20.

Firstly, we need to edit the Memcached configuration file which is the /etc/memcached.conf file:

$ sudo vim /etc/memcached.conf

Scroll and be sure to locate this line:

-l 127.0.0.1

Replace the localhost address with Memcached server IP:

-l 192.168.2.20

To effect the changes, restart Memcached as follows:

$ sudo systemctl restart memcached

Thereafter, allow traffic from the client IP to the Memcached server through the UFW firewall:

$ sudo ufw allow from 192.168.2.10 to any port 11211


4. Use Memcached server

Memcached requires a language-specific client for our web application to communicate with it. Thankfully, Memcached supports a wide range of popular programming languages such as PHP and Python.

A good number of web apps run on PHP and Memcached is a popular caching system used by web developers to boost the performance of web applications.

To enable Memcache support for PHP, invoke the command:

$ sudo apt install php-memcached

For Python support, use the pip package manager to install the required tools as shown:

$ pip install python-memcached
$ pip install pymemcache


[Need urgent assistance in fixing missing packages on Ubuntu Linux Server? We are available to help you. ]

This article covers how you can install Memcached on Ubuntu 20.04. Memcached has proved to be a very reliable caching system since its inception in 2003 and continues to be a favorite among developers in speeding up web applications. 

Memcached is a high-performance, distributed memory object caching server. It is free and open source software intended for use in speeding up dynamic web applications by mitigating database load.


To install and configure memcached on Ubuntu:

1. Update your Ubuntu server:

$ sudo apt update
$ sudo apt upgrade

2. Install memcached server on Ubuntu:

Run apt command as follows:

$ sudo apt install memcached

3. Configure memcached server:

Edit config file named /etc/memcached.conf using a text editor such as nano command or vim command:

$ sudo nano /etc/memcached.conf

OR

$ sudo vi /etc/memcached.conf


How to restart, stop, reload memcached server on Ubuntu:

To Stop service - $ sudo systemctl stop memcached

To start Service - $ sudo systemctl start memcached

To restart Service - $ sudo systemctl restart memcached

To check the status - $ sudo systemctl status memcached

Related Posts