Sep 20, 2023 4 min read

How to Install Memcached on Ubuntu 20.04

Install Memcached on Ubuntu 20.04 with our step-by-step tutorial. It stores data in the memory to enhance website and application performance.

Install Memcached on Ubuntu 20.04
Table of Contents

Choose a different version or distribution

Introduction

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

Memcached is a widely used open-source caching system that stores data in the memory to enhance website and application performance. It works by reducing database loads and speeding up data retrieval, which leads to faster response times. Memcached is highly scalable, making it suitable for handling heavy traffic.

It utilizes a simple key-value storage approach, where data is stored in key-value pairs. With its efficient memory management, Memcached improves overall system efficiency, enabling businesses to deliver faster and more responsive user experiences.

This tutorial demonstrates how to set up Memcached on Ubuntu 20.04.

Advantages of Memcached

  1. Enhanced Performance: Memcached improves website and application performance by storing data in memory, reducing database loads, and speeding up data retrieval.
  2. Faster Response Times: With its caching mechanism, Memcached enables faster response times, leading to a better user experience.
  3. Scalability: Memcached is highly scalable, making it suitable for handling heavy traffic and ensuring consistent performance as the workload increases.
  4. Efficient Memory Management: Memcached optimizes memory usage, effectively utilizing available resources and enhancing overall system efficiency.
  5. Simplified Data Storage: Memcached follows a simple key-value storage approach, making it easy to store and retrieve data, contributing to efficient development and maintenance.

Install Memcached

The standard Ubuntu 20.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 Mon 2020-07-13 19:32:01 UTC; 23s ago

That completes the installation of memcached on your Ubuntu 20.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 setup 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 -y

Python

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

sudo apt install python3-pip -y
pip install pymemcache
pip install python-memcached

FAQs to Install Memcached on Ubuntu 20.04

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

Use the command systemctl status memcached to check the status of Memcached. If it is running, you will see an active (running) status.

How do I configure Memcached on Ubuntu 20.04?

The main configuration file for Memcached is located at /etc/memcached.conf. You can modify this file to adjust settings like port, memory allocation, and connection limits.

How can I start, stop, or restart Memcached on Ubuntu 20.04?

To start, use sudo systemctl start memcached. To stop, use sudo systemctl stop memcached. To restart, use sudo systemctl restart memcached.

Can I change the default port for Memcached on Ubuntu 20.04?

Yes, you can change the default port by editing the configuration file at /etc/memcached.conf and modifying the port parameter.

How can I test if Memcached is working on Ubuntu 20.04?

You can use the command telnet localhost 11211 to test the connection to Memcached. If successful, you will see a response indicating a successful connection.

How do I enable Memcached extensions on Ubuntu 20.04?

Install the necessary PHP extension by running sudo apt-get install php-memcached in the terminal. Then, restart the PHP service for the changes to take effect.

How can I monitor Memcached performance on Ubuntu 20.04?

Tools like memcached-tool and telnet can be used to monitor Memcached performance. Additionally, you can enable Memcached statistics by configuring the stats parameter in the configuration file.

How do I uninstall Memcached from Ubuntu 20.04?

To uninstall Memcached, run the command sudo apt-get remove memcached in the terminal. This will remove the Memcached package from your system.

Conclusion

In this tutorial, you learnt to install Memcached on Ubuntu 20.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.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.