How to Install Memcached on Ubuntu 22.04

Choose a different version or distribution

Introduction

Before we begin talking about how to install Memcached on Ubuntu 22.04, let's briefly understand – What is Memcached?

Memcached is an efficient and widely used in-memory caching system. It boosts website and application performance by storing frequently accessed data in RAM, resulting in lightning-fast response times. Memcached reduces the load on databases by caching data and minimizing repetitive queries.

With its easy implementation and scalability, it is a popular choice among developers. Memcached's distributed caching model ensures high availability and fault tolerance, making it an excellent solution for improving overall application efficiency.

This tutorial demonstrates how to set up Memcached on Ubuntu 22.04. We will also address a few FAQs on how to install Memcached on Ubuntu 22.04.

Advantages of Memcached

  1. Lightning-fast performance: Memcached is an in-memory caching system that accelerates data access by storing frequently accessed data in RAM, providing blazing-fast response times.
  2. Scalability: Memcached is highly scalable, allowing you to add or remove cache servers seamlessly to handle increasing data loads and maintain high performance.
  3. Reduced database load: By caching data in Memcached, you can offload repetitive database queries, reducing the load on your backend systems and improving overall application efficiency.
  4. Simple implementation: Memcached is easy to integrate into existing applications, with client libraries available for various programming languages, making it a popular choice for developers.
  5. Distributed caching: Memcached supports a distributed caching model, allowing you to distribute cached data across multiple servers, ensuring high availability and fault tolerance.

Install Memcached

The standard Ubuntu 22.04 repositories contain the Memcached package. Enter the upcoming command as root or another user with sudo access to install it:

sudo apt update
sudo apt install memcached libmemcached-tools
ℹ️
You can control the Memcached server using the command-line tools provided by the libmemcached-tools package.

The Memcached service will launch automatically after the installation is finished. Enter the following to verify the service's status:

sudo systemctl status memcached

You will get an output like below:

Output

● memcached.service - memcached daemon
     Loaded: loaded (/lib/systemd/system/memcached.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2023-06-21 05:20:26 UTC; 3min 10s ago
       Docs: man:memcached(1)
   Main PID: 21212 (memcached)
      Tasks: 10 (limit: 2307)
     Memory: 1.8M
        CPU: 232ms
     CGroup: /system.slice/memcached.service
             └─21212 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached.pid

Jun 21 05:20:26 ip-172-31-80-81 systemd[1]: Started memcached daemon.

That completes the installation of Memcached on your Ubuntu 22.04 server, you are now ready to use it.

Configure Memcached

The file /etc/memcached.conf allows for the configuration of Memcached parameters. Memcached is configured by default to only listen on localhost.

For the majority of users, the default configuration options are adequate.

Remote Access

You shouldn't permit remote access if the client that is connected to Memcached is also operating on the same host.

Memcached can be used to launch a distributed denial-of-service (DDoS) attack if it is set up incorrectly. You must set up the firewall and open the Memcached UDP port 11211 exclusively to trustworthy clients in order to permit remote access to the Memcached server.

The example that follows assumes you want to connect through a private network to the Memcached server The IP address of the client is 192.168.100.30, while the IP address of the Memcached server is 192.168.100.20.

Editing the Memcached configuration and setting the service to listen on the server's private networking interface are the initial steps.

Open the memcached.conf configuration file to perform this:

sudo nano /etc/memcached.conf

Find the line that starts with -l 127.0.0.1, and substitute 127.0.0.1 with 192.168.100.20 there.

-l 192.168.100.20

For the changes to take effect, restart the Memcached service:

sudo systemctl restart memcached

The Memcached port has to be opened in the firewall after the service has been configured.

sudo ufw allow from 192.168.100.30 to any port 11211

Connecting to Memcached

Memcached clients come in a variety of forms and are developed in a variety of computer languages.

PHP

Installing the php-memcached extension is necessary to use Memcached as a caching database for your PHP application, such as WordPress, Drupal, Joomla, or Magento:

sudo apt install php-memcached

Python

For working with Memcache, there are numerous Python libraries. Using pip, you can install the library of your choice:

pip install pymemcache
pip install python-memcached

FAQs to Install Memcached on Ubuntu 22.04

What are the prerequisites for installing Memcached on Ubuntu 22.04?

There are no specific prerequisites for installing Memcached on Ubuntu 22.04. However, make sure you have administrative privileges to install packages.

How can I check if Memcached is running on Ubuntu 22.04?

You can check the status of Memcached by running the command systemctl status memcached. If it is running, you will see the active (running) status.

How can I configure Memcached on Ubuntu 22.04?

The configuration file for Memcached on Ubuntu 22.04 is located at /etc/memcached.conf. You can modify the settings in this file to configure Memcached according to your needs.

How can I change the listening IP address for Memcached on Ubuntu 22.04?

To change the listening IP address, edit the Memcached configuration file and modify the -l option to specify the desired IP address.

How can I set a custom port for Memcached on Ubuntu 22.04?

You can set a custom port by editing the Memcached configuration file and modifying the -p option to specify the desired port number.

How can I enable Memcached to start automatically on system boot in Ubuntu 22.04?

Memcached should start automatically by default. However, if it doesn't, you can enable automatic startup by running the command sudo systemctl enable memcached.

Can I secure Memcached with authentication on Ubuntu 22.04?

By default, Memcached doesn't provide built-in authentication. However, you can use tools like SASL or implement application-level authentication to secure Memcached.

How can I uninstall Memcached from Ubuntu 22.04?

To uninstall Memcached, run the command sudo apt-get remove memcached. This will remove Memcached and its associated configuration files from your system.

Conclusion

In this tutorial, you installed Memcached on Ubuntu 22.04. For more information on this topic, consult Memcached Wiki.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them.