How to install and configure Memcached on Ubuntu 24.04
Memcached on Ubuntu 24.04 speeds up web applications by storing data in computer memory.
Memcached is a free tool that keeps information your website uses often, like results from database lookups, right in your server’s RAM. This makes your website much faster because it doesn’t have to wait for data to be read from slower storage disks.
Installing Memcached, often version 1.6.21 or newer on Ubuntu 24.04, is the first step. Afterward, you’ll adjust its settings to best help your specific web applications. This process significantly improves how quickly your site responds and performs.
Install Memcached with `sudo apt install memcached libmemcached-tools`. Start and enable the service using `sudo systemctl start memcached` and `sudo systemctl enable memcached`. Configure settings in `/etc/memcached.conf` and restart the service to apply changes.
Install and configure Memcached on Ubuntu
Installing Memcached on Ubuntu 24.04 is straightforward. You start by refreshing your system’s software list with `sudo apt update`, then you can install Memcached itself by running `sudo apt install memcached`. This gets Memcached ready to help your applications run faster by storing data in memory.
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
After installing Memcached on Ubuntu, you can adjust its settings by editing the configuration file located at `/etc/memcached.conf`. While the default settings are usually fine for most people, you can open this file to change things like how much memory Memcached uses or which network connections it listens on, helping to speed up your specific applications.
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
# 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
You can check and manage Memcached directly from your command line using Telnet. First, install the Telnet package by running `sudo apt install telnet`. Once it’s installed, you can connect to Memcached by typing `telnet localhost 11211` into your terminal to see its performance stats.
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.
Was this guide helpful?
About the Author
Richard
Tech Writer, IT Professional
Richard, a writer for Geek Rewind, is a tech enthusiast who loves breaking down complex IT topics into simple, easy-to-understand ideas. With years of hands-on experience in system administration and enterprise IT operations, he’s developed a knack for offering practical tips and solutions. Richard aims to make technology more accessible and actionable. He's deeply committed to the Geek Rewind community, always ready to answer questions and engage in discussions.
No comments yet — be the first to share your thoughts!