UbuntuUbuntu Based

How To Install Memcached on Ubuntu 24.04 LTS

Install Memcached on Ubuntu 24.04

Memcached is a powerful, distributed memory caching system that can significantly boost the performance of your web applications. By reducing database load and caching frequently accessed data, Memcached helps improve response times and scalability. In this comprehensive guide, we’ll walk you through the process of installing and configuring Memcached on Ubuntu 24.04 LTS, ensuring you have all the tools necessary to optimize your server’s performance.

Prerequisites

Before we dive into the installation process, make sure you have the following:

  • A server running Ubuntu 24.04 LTS
  • SSH access with sudo privileges
  • Basic familiarity with the command line interface
  • A stable internet connection

Understanding Memcached

Memcached is an open-source, high-performance, distributed memory caching system. It works by storing data and objects in RAM, which allows for quick retrieval and reduces the need for external data sources like databases or APIs. This caching mechanism is particularly useful for:

  • Reducing database load
  • Speeding up dynamic web applications
  • Caching session data
  • Storing API results
  • Improving overall application responsiveness

By implementing Memcached, you can significantly enhance your website’s performance and user experience.

Installation Methods

There are two primary methods to install Memcached on Ubuntu 24.04 LTS: using the APT package manager or compiling from source. We’ll cover both methods to give you flexibility in choosing the best approach for your needs.

Method 1: APT Installation

The APT (Advanced Package Tool) installation method is the simplest and most straightforward way to install Memcached on Ubuntu 24.04 LTS. Follow these steps:

Step 1: Update System Packages

Before installing any new software, it’s crucial to update your system’s package index. Open your terminal and run:

sudo apt update && sudo apt upgrade -y

This command updates the package lists and upgrades any outdated packages on your system.

Step 2: Install Memcached

Now, let’s install Memcached and its dependencies:

sudo apt install memcached libmemcached-tools -y

This command installs both the Memcached server and the libmemcached-tools package, which provides useful command-line utilities for interacting with Memcached.

Step 3: Verify Installation

To ensure Memcached was installed correctly, check its version:

memcached -V

You should see output similar to:

memcached 1.6.24

Method 2: Source Installation

For users who require a specific version or want more control over the installation process, compiling Memcached from source is an excellent option. Here’s how to do it:

Step 1: Install Dependencies

First, install the necessary build tools and libraries:

sudo apt install build-essential libevent-dev libsasl2-dev -y

Step 2: Download Source Code

Visit the official Memcached website to find the latest stable release. Then, download it using wget:

wget https://memcached.org/files/memcached-1.6.24.tar.gz

Step 3: Extract and Compile

Extract the downloaded archive and compile Memcached:

tar xzf memcached-1.6.24.tar.gz
cd memcached-1.6.24
./configure --prefix=/usr/local
make

Step 4: Install Memcached

After compilation, install Memcached:

sudo make install

Step 5: Verify Installation

Check the installed version:

/usr/local/bin/memcached -V

Configuration Steps

After installing Memcached, it’s essential to configure it properly for optimal performance. Let’s go through the key configuration steps:

Locating the Configuration File

The default configuration file for Memcached is located at /etc/memcached.conf. Open it with your preferred text editor:

sudo nano /etc/memcached.conf

Memory Allocation

One of the most critical settings is the amount of memory allocated to Memcached. Find the line starting with -m and adjust it according to your server’s resources:

-m 64

This sets the memory limit to 64MB. Adjust this value based on your server’s available RAM and requirements.

Connection Settings

By default, Memcached listens on 127.0.0.1 (localhost) on port 11211. If you need to change these settings, modify the following lines:

-l 127.0.0.1
-p 11211

Security Considerations

To enhance security, consider implementing SASL authentication. First, ensure SASL support is enabled:

-S

Then, create a SASL password file:

echo "username:password" | saslpasswd2 -a memcached -c

Performance Tuning

For better performance, you can adjust the following parameters:

  • Number of threads: -t 4
  • Maximum simultaneous connections: -c 1024
  • Maximum object size: -I 5M

After making changes, save the file and restart Memcached:

sudo systemctl restart memcached

Testing and Verification

To ensure Memcached is working correctly, let’s perform some tests:

Check Service Status

Verify that Memcached is running:

sudo systemctl status memcached

You should see “active (running)” in the output.

Connection Testing

Use telnet to connect to Memcached:

telnet localhost 11211

If successful, you’ll see a connection established message.

Basic Operations

Try storing and retrieving data:

set testkey 0 100 10
helloworld
get testkey

You should see “helloworld” returned.

Telnet Commands

Here are some useful telnet commands for Memcached:

  • stats: Display server statistics
  • flush_all: Clear all cached data
  • quit: Close the telnet connection

Integration

Memcached can be integrated with various programming languages and frameworks. Here are some examples:

PHP Integration

Install the PHP Memcached extension:

sudo apt install php-memcached

Then, use it in your PHP code:

$memcache = new Memcached();
$memcache->addServer('localhost', 11211);
$memcache->set('key', 'value', 3600);
$value = $memcache->get('key');

Python Support

Install the Python Memcached client:

pip install pymemcache

Use it in your Python scripts:

from pymemcache.client import base
client = base.Client(('localhost', 11211))
client.set('key', 'value')
result = client.get('key')

Security Best Practices

Implementing proper security measures is crucial when using Memcached:

SASL Authentication

Enable SASL authentication as mentioned in the configuration section to prevent unauthorized access.

Firewall Configuration

Use UFW (Uncomplicated Firewall) to restrict access to the Memcached port:

sudo ufw allow from trusted_ip_address to any port 11211

Network Security

If possible, run Memcached on a private network or use a VPN to secure connections between your application servers and Memcached instances.

Troubleshooting

When working with Memcached, you might encounter some common issues. Here’s how to address them:

Connection Refused

If you can’t connect to Memcached, check if the service is running and listening on the correct IP and port:

sudo netstat -plunt | grep memcached

Out of Memory Errors

If you see “SERVER_ERROR out of memory,” increase the memory allocation in the configuration file.

Slow Performance

Use the stats command in telnet to check for high eviction rates or a large number of connections. Adjust your configuration accordingly.

Production Deployment Tips

When deploying Memcached in a production environment, consider the following:

Scaling Considerations

For high-traffic applications, consider setting up a Memcached cluster using multiple instances.

Monitoring

Implement monitoring tools like Nagios or Zabbix to track Memcached performance and availability.

Backup Strategies

While Memcached is not designed for persistent storage, consider implementing a warm cache strategy to quickly repopulate the cache after a restart.

Maintenance Procedures

Regularly update Memcached to the latest stable version and perform periodic performance audits to ensure optimal operation.

Congratulations! You have successfully installed Memcached. Thanks for using this tutorial for installing Memcached on Ubuntu 24.04 LTS system. For additional help or useful information, we recommend you check the official Memcached website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button