How to install and configure Memcached on Ubuntu 24.04

This article provides a guide for installing and configuring Memcached on Ubuntu 24.04. Memcached significantly enhances web application performance by caching data and objects in RAM, reducing the need for frequent data source access. The post covers installation steps, configuration, enabling support for other applications, accessing Memcached CLI, and concludes with an invitation for feedback.

This article explains how to install and configure Memcached on Ubuntu 24.04.

Memcached, a popular open-source, high-performance distributed memory caching system, is a game-changer for dynamic database-driven websites. Caching data and objects in RAM dramatically reduces the number of times an external data source needs to be read, thereby turbocharging web application performance.

Installing and using Memcached on Ubuntu can improve the performance and scalability of your web applications, slashing database load and supercharging page load times. It’s a fantastic tool to optimize your web server’s performance and deliver an exceptional user experience.

The steps below walk you through installing and configuring Memcached on Ubuntu 24.04.

Install and configure Memcached on Ubuntu

The Memcached packages are available in the Ubuntu 20.04 repositories, so there is no need to install additional software.

You can install Memcached on Ubuntu by using the command below.

sudo apt update
sudo apt install memcached libmemcached-tools

Once Memcached is installed, use this command to verify the installed version.

memcached --version

You should see an output similar to the one below.

memcached 1.6.24

On Ubuntu Linux, systemd starts and manages the Memcached service. Use the following command to start, enable, and check the service’s status.

To start the Memcached service, run this command:

sudo systemctl start memcached

To enable the Memcached service to start after the system reboots automatically, run this command:

sudo systemctl enable memcached

To check the status of the Memcached service, run this command:

sudo systemctl status memcached

You should see an output similar to the one below:

● memcached.service - memcached daemon
Loaded: loaded (/usr/lib/systemd/system/memcached.service; enabled; preset: enable>
Active: active (running) since Thu 2024-05-30 09:36:26 CDT; 34s ago
Docs: man:memcached(1)
Main PID: 4246 (memcached)
Tasks: 10 (limit: 4561)
Memory: 2.1M (peak: 2.4M)
CPU: 70ms
CGroup: /system.slice/memcached.service
└─4246 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 -l ::1 ->
May 30 09:36:26 Ubuntu2404 systemd[1]: Started memcached.service - memcached daemon.

Configure Memcached on Ubuntu

The Memcached configuration file can be found at /etc/memcached by default.conf

The file’s default settings should be good enough for most environments and applications. However, for more advanced settings, open the file and make the changes you want to apply to your environment.

First, run this command to open the Memcached configuration file.

sudo nano /etc/memcached.conf

For example, Memcached listens on the server’s local IP address (127.0.0.1). If you want it to listen only on a different IP, edit the lines in the file and replace the IP address and/or port number.

# Default connection port is 11211
-p 11211

# Run the daemon as root. The start-memcached will default to running as root if no
# -u command is present in this config file
-u memcache
# Specify which IP address to listen on. The default is to listen on all IP addresses
# This parameter is one of the only security measures that memcached has, so make sure
# it's listening on a firewalled interface.
-l 127.0.0.1

Save the file and exit. Then, restart Memcached services to apply the changes.

Enable Memcached support for other applications

You can install tools to enable Memcached support for PHP, Python, Perl, and others.

Enable PHP support

To enable Memcached PHP support, run this command:

sudo apt install php-memcached
sudo phpenmod memcached

Enable Python support

To enable Memcached Python support, run this command:

sudo apt install python3-pymemcache

Enable Perl support

To enable Memcached Perl support, run this command:

sudo apt install libcache-memcached-libmemcached-perl

Access Memcached CLI

Install the Telnet package to interact with Memcached on the command line interface.

sudo apt install telnet

Once Telnet is installed, use the command below to access Memcached.

telnet localhost 11211

Run the stats command to get an overview of Memcached content.

stats

You should see an output similar to the one below.

STAT pid 4246
STAT uptime 1172
STAT time 1717080956
STAT version 1.6.24
STAT libevent 2.1.12-stable
STAT pointer_size 64
STAT rusage_user 0.315977
STAT rusage_system 0.157988
STAT max_connections 1024
STAT curr_connections 2
STAT total_connections 3
STAT rejected_connections 0
STAT connection_structures 3
STAT response_obj_oom 0
STAT response_obj_count 1
STAT response_obj_bytes 16384
STAT read_buf_count 2
STAT read_buf_bytes 32768
STAT read_buf_bytes_free 0
...................

Quit the CLI.

quit

That should do it!

Conclusion:

  • Memcached is a powerful open-source, high-performance distributed memory caching system that can significantly enhance the performance and scalability of dynamic database-driven websites.
  • By caching data and objects in RAM, Memcached minimizes the need to read from external data sources, optimizing web application performance and reducing database load.
  • Ubuntu 24.04’s installation and configuration process is straightforward, and the default settings are suitable for most environments and applications.
  • Enabling Memcached support for PHP, Python, and Perl and accessing Memcached through the command line interface further extends its usability and benefits for web developers.
  • Implementing Memcached on Ubuntu 24.04 can improve web server performance and provide a superior user experience for website visitors.
Richard Avatar

Comments

Leave a Reply

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


Exit mobile version