How to install Memcached on ubuntu 18.04 and 20.04

Memcached is a free and open-source, high-performance, distributed memory object caching system which store data in-memory by key-value pair. Memcached is basically used to store cache objects which can be either API or data from the database.

This article will guide you on how to install Memcached on ubuntu 20.04 and 18.04.

Installing Memcached on Ubuntu

By default, Memcache is available in the ubuntu repository. All you have to do is to update the Ubuntu repository. Enter the below command to update.

sudo apt-get update

Once your repository is updated. Enter the following command to install Memcache in your Ubuntu machine.

sudo apt install memcached libmemcached-tools

memcached command will install the Memcached.

libmemcached-tools will provide you the command-line utility for the Memcache management.

Once the installation is completed. The Memcached will start automatically. To ensure Memcached is installed properly, Enter the following command to check the status of Memcached.

sudo systemctl status memcached

The above command's output will look like this

‚óŹ memcached.service - memcached daemon
   Loaded: loaded (/lib/systemd/system/memcached.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2020-07-12 18:49:40 IST; 27s ago

If you see the above message. you have successfully installed Memcached in your system.

Configuring Memcached

All the Memcached configuration is available in the /etc/memcached.conf configuration file. By default, Memcached is set to listen on the localhost(127.0.0.1).

Setting up Remote Access

Note: If you are using Memcached on the same client-server then you shouldn't allow remote access.

Let's assume the Memcached server's IP address is xx.xx.xx.12 and the client's server IP address is xx.xx.xx.52.

The first thing we have to do is to edit the Memcached configuration file and replace the localhost IP to your Memcached IP. Enter the following to edit the configuration file. 

# /etc/memcached.conf

sudo vi /etc/memcached.conf

Next, find the line that starts with 

-l 127.0.0.1

and replace the 127.0.0.1 with xx.xx.xx.12. Once you are done save it and close.

Next, Restart the Memcached with the below command.

sudo service memcached restart

Now open the Memcached port 11211 in the firewall with the following command.

sudo ufw allow from 192.168.100.30 to any port 11211

Connecting Memcache

Now the installation and configuration are done. There are many languages that Memcached can be implemented with.

PHP

To use Memcached with PHP it will require php-memcached extension. To install the extension enter the following command.

sudo apt-get install php-memcached

Python

To use Memcached with Python it will require the pymemcache. Enter the following command to install the package

pip install pymemcache
pip install python-memcached

Conclusion

In this article, we learn how to install, configure, and use the Memcached. For more details of Memcached you can refer the following Memcache article.

memcached/memcached Wiki