Ubuntu

How to Install Memcached on Ubuntu 22.04

Memcached is a memory object caching system specifically designed to enhance the speed of dynamic web applications by reducing the load of the database server. If we explain the usage of Memcached in simple words, it helps you to take the memory from the part of your system where it is unnecessary and assign it to that part of the memory where more memory is needed. In this way, mostly your web servers have not to deal with the cache, most developers dedicated the separate machines to deal with the cache.

In this guide, two different methods of the installation of Memcached have been discussed in detail, along with its basic configuration on Ubuntu.

How to install Memcached on Ubuntu 22.04

There are two different methods for the installation of Memcached on Ubuntu either from its default repository by using the apt package manager or by downloading its tar package from its official website.

Method 1: How to install Memcached on Ubuntu 22.04 from its default repository

The first and simple method to install Memcached on Ubuntu is by installing it from its default repository, for which we will use the apt package manager:

$ sudo apt install memcached libmemcached-tools -y

To confirm the successful execution of the above command, we will find out the installed version details of Memcached by using the command:

$ memcached --version

Using the systemctl command, we will check the status of memcached:

$ sudo systemctl status memcached

To uninstall the memcached from Ubuntu with all its configuration files, we will use the purge command with apt package manager:

$ sudo apt purge memcached libmemcached-tools -y

Method 2: Installing Memcached on Ubuntu 22.04 by downloading its tar package

Another method to install the the Memcached on Ubuntu is by downloading its zipped package from its official website using the wget command:

$ wget -c https://memcached.org/files/memcached-1.6.15.tar.gz

Unzip the zipped file to access its contents by using the command:

$ tar -zxvf memcached-1.6.15.tar.gz

List down the contents of the directory to confirm the file has been unzipped:

$ ls

Navigate to the memcached-1.6.15 directory using the cd command:

$ cd memcached-1.6.15

Now install the libevent-dev package because it is the dependency of memcached:

$ sudo apt install libevent-dev -y

Now we will configure, compile all the files using the make command and then install all the compiled files:

$ ./configure && make && make test && sudo make install

How to configure the Memcached on Ubuntu 22.04

For configuration, we will open its config file using any text editor:

$ sudo nano /etc/memcached.conf

Now change the IP address in the configuration file with your IP address, for example, our IP address is 10.0.2.15:

Stop the Memcached service by using the systemctl command:

$ sudo systemctl stop memcached.service

Now again start the service:

$ sudo systemctl start memcached.service

Also enable the service of the Memcached on your system:

$ sudo systemctl enable memcached.service

And allow the traffic on your IP address at port 11211 using the ufw command:

$ sudo ufw allow from 10.0.2.15 to any port 11211

How Memcached as caching database

There are multiple methods to connect with Memcached.

If your application is PHP based such as WordPress, Joomla, or Drupal and you want to connect Memcached as caching database then install php-memcached through the command:

$ sudo apt install php-memcached

For Python based applications:

$ pip install pymemcache
$ pip install python-memcached

Conclusion

Memcached is an open-source application that is used to run php-based dynamic web applications by reducing the load on their databases so that it can speed the performance of those applications. In this write-up, we have explored two installation methods of Memcached on Ubuntu one by using the default repository and the other by downloading its zipped package from its website.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.