How to install and configure Memcached on Ubuntu 24.04 : Boost Your Application Performance

Memcached, a free and open-source memory object caching system, can significantly improve the performance of dynamic web applications. By temporarily storing frequently accessed data in RAM, Memcached reduces the load on your database and web server, leading to faster page load times and a smoother user experience.

This guide will walk you through installing and configuring Memcached on Ubuntu 24.04. We’ll cover everything from updating your package lists to verifying a successful installation.

Understanding Memcached

Before diving in, let’s understand how Memcached works. Traditional web applications rely heavily on databases to retrieve data. This constant database interaction can become a bottleneck, especially for high-traffic websites. Memcached acts as an intermediary, caching frequently accessed data like database queries or rendered pages in memory. When a request arrives, Memcached checks its cache first. If the data is present, it delivers it much faster than retrieving it from the database. This reduces database load and improves application responsiveness.

Prerequisites

  • An Ubuntu 24.04 server with a user with sudo privileges.
  • Basic understanding of command line interface (CLI).

How to Install Memcached on Ubuntu

Step 1: Update Package Lists

Before installing any software, it’s crucial to ensure your package lists are up-to-date. This guarantees you’re installing the latest versions available. Open your terminal and run the following command:

sudo apt update

This command updates the package lists and upgrades any existing packages to their latest versions.

Step 2: Install Memcached and Tools

Now, let’s install Memcached and the libmemcached-tools package, which provides utilities for managing the Memcached service. Use the following command:

sudo apt install memcached libmemcached-tools -y

The -y flag ensures automatic acceptance of any prompts during installation.

Step 3: Verify Installation

You can verify the installation by checking the installed version of Memcached:

memcached --version

This should display the installed Memcached version.

memcached version: 1.6.24

Step 4: Start and Enable Memcached Service

By default, Ubuntu should automatically start the Memcached service upon installation. However, it’s always a good practice to verify its status:

systemctl status memcached

The output will indicate the status of the Memcached service. You might see something like:

 memcached.service - Memcached memory caching daemon
     Loaded: loaded (/lib/systemd/system/memcached.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2024-06-11 15:30:23 CEST; 1min 12s ago
       Process: 12345 (memcached)
     CGroup: /system/systemd/service/memcached.service
           └─ 12345 (memcached)

If the service is not running, you can start it manually using:

sudo systemctl start memcached

To ensure Memcached automatically starts at system boot, use:

sudo systemctl enable memcached

How To Configure Memcached On Ubuntu

The default Memcached configuration file is located at /etc/memcached.conf. You can edit it using a text editor like nano:

sudo nano /etc/memcached.conf

Note: You can replace nano with your preferred text editor (e.g., vim).

Understanding the configuration options:

The configuration file contains various options that control Memcached’s behavior. Here’s a breakdown of some key settings:

  • -m: Defines the amount of memory allocated to Memcached (in megabytes). Allocate at least 1GB for optimal performance, considering your system’s available memory.
  • -p: Specifies the port on which Memcached listens for connections. The default port is 11211. If you need to change it for specific reasons, ensure your applications are configured to use the new port.
  • -l: Binds Memcached to a specific IP address. By default, it listens on all interfaces (0.0.0.0). For security, consider restricting access to localhost (127.0.0.1) unless remote connections are absolutely necessary.
  • -u: Sets the user under which Memcached runs. The default is typically nobody or a dedicated user account for Memcached.
  • -t: Configures the number of worker threads handling incoming connections. Adjust this based on your expected load and hardware capabilities.

Important Note: These are just the core configuration options. The configuration file offers a plethora of other settings for advanced users. Refer to the official Memcached documentation for a comprehensive explanation of each option https://memcached.org/.

Adjust settings as needed:

Based on your system resources and application requirements, modify the relevant options in the configuration file. Here’s an example:

-m 1024  # Allocate 1GB of memory
-p 11211  # Default port (optional)
-l 127.0.0.1  # Listen on localhost only (recommended)
-u memcached  # Run as a dedicated user (optional)
-t 4  # Adjust worker threads based on load (optional)

Save and close the configuration file.

Important Note: Remember to restart the Memcached service after making any configuration changes for them to take effect:

sudo systemctl restart memcached

How To Test Memcached

You can use the memcat tool from the libmemcached-tools package to test basic Memcached functionality. Here’s an example:

echo "test_data" | memcat localhost:11211

This command sets a key named “test_data” with a value of “test_data” on the Memcached server running on localhost port 11211 (the default port).

To retrieve the data:

memcat localhost:11211 test_data

This command should output “test_data”, indicating successful interaction with Memcached.

Important Note: These verification steps are optional and primarily for testing purposes. The actual integration of Memcached with your web application will depend on the specific framework or programming language you’re using.


How To Enable Memcached Support for Applications on Ubuntu

In the previous steps, we installed and configured Memcached itself. Now, let’s explore how to enable Memcached support for specific applications commonly used on Ubuntu 24.04.

Enabling PHP Support:

PHP can leverage Memcached to cache data and improve application performance. Here’s how to enable it:

sudo apt install php-memcached
sudo phpenmod memcached

Enabling Python Support

Python can also use Memcached for caching. Here’s how to enable it:

sudo apt install python3-pymemcache

Enabling Perl Support

Perl can leverage Memcached using the Memcached Perl module. Here’s how to enable it:

sudo apt install libcache-memcached-libmemcached-perl

Conclusion

By following these steps, you’ve successfully installed and configured Memcached on your Ubuntu 24.04 server. With Memcached caching frequently accessed data, your web applications should experience improved performance and faster response times. Remember, Memcached configuration might require further adjustments based on your specific application needs.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top