Memcached is a widely used open-source memory object caching system that speeds up dynamic web applications by caching data in memory. It is designed for PHP-based web applications such as WordPress, Joomla, Drupal, and Python-based applications. It can increase database performance and page speed by temporarily storing the result of database queries or rendered web pages in memory (RAM). This reduces the number of direct requests to the database and hard disk.

This tutorial will show you how to install Memcached on Ubuntu 24.04 LTS server.

Requirements

  • A server or desktop running Ubuntu 24.04.
  • You have root or sudo access to the server.

Install Memcached on Ubuntu 24.04

The Memcached package is available in the Ubuntu 24.04 repository. You can install Memcached by running the following command:

sudo apt install memcached libmemcached-tools -y

Once the Memcached is installed, you can verify the Memcached version using the following command:

memcached --version

You should see the following output:

memcached 1.6.24

Manage Memcached Service

The memcached service is started and managed by systemd.

To start the Memcached service, run the following command:

sudo systemctl start memcached

To enable the Memcached service to automatically start it after the system reboot, run the following command:

sudo systemctl enable memcached

You can also check the status of the Memcached service using the following command:

sudo systemctl status memcached

You should see the following output:

? memcached.service - memcached daemon
     Loaded: loaded (/usr/lib/systemd/system/memcached.service; enabled; preset: enabled)
     Active: active (running) since Wed 2024-05-29 11:03:38 UTC; 51s ago
       Docs: man:memcached(1)
   Main PID: 707 (memcached)
      Tasks: 10 (limit: 4557)
     Memory: 5.8M (peak: 5.8M)
        CPU: 53ms
     CGroup: /system.slice/memcached.service
             ??707 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 -l ::1 -P /var/run/memcached/memcached.pid

May 29 11:03:38 server1 systemd[1]: Started memcached.service - memcached daemon.

<img alt="Memcached service status" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/ubuntu-memcached-status.png665716d00b77c.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="177" loading="lazy" src="data:image/svg xml,” width=”750″>

By default, Memcached listens on port 11211. You can check it using the following command:

ss -antpl | grep memcache

You should see the Memcached listening port in the following output:

LISTEN 0      1024                               127.0.0.1:11211      0.0.0.0:*    users:(("memcached",pid=707,fd=26))
LISTEN 0      1024                                   [::1]:11211         [::]:*    users:(("memcached",pid=707,fd=27))

Configure Memcached

The Memcached configuration file is /etc/memcached.conf. You can edit the config file to change the default settings if needed.

sudo nano /etc/memcached.conf

Change the following lines as per your requirement:

## Specify the IP address on which Memcached listens on. 
-l 127.0.0.1

## Disable the UDP

-U 0

## Define the memory to store the cache.

-m 1000

Save and close the file, then restart Memcached to apply the config changes:

sudo systemctl restart memcached

Install the Memcached PHP Extension

If you use a PHP-based application, you must install the PHP extensions to be able to use Memcached in your web app. You can install it by running the following command:

sudo apt install php-memcached -y

Once all the packages are installed then enable the PHP Memcached module with the following command:

sudo phpenmod memcached

Next, restart the Apache service to apply the changes:

sudo systemctl restart apache2

Add Python Memcached Support

You must install the Python Memcached library on your server if you use Python-based web applications.

sudo apt install python3-pymemcache -y

Add Perl Memcached support

If you are using Perl-based web applications, then you will need to install the Perl Memcached library on your server.

sudo apt install libcache-memcached-libmemcached-perl -y

Access Memcached CLI

You can use the telnet command to interact with Memcached on the command-line.

sudo apt install telnet

Run the Telnet command to connect to Memcached. The number 11211 is the Memcached port, if you changed it to an alternate port, then take care to adjust it here as well.

telnet localhost 11211

You should see the following output:

Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

Next, run the following command to get an overview of your Memcached service:

stats

You should see the following output:

STAT pid 707
STAT uptime 256
STAT time 1716980872
STAT version 1.6.24
STAT libevent 2.1.12-stable
STAT pointer_size 64
STAT rusage_user 0.023712
STAT rusage_system 0.052694
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
STAT read_buf_oom 0
STAT reserved_fds 20
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 0
STAT cmd_touch 0
STAT cmd_meta 0

Conclusion

You have successfully installed Memcached on your Ubuntu 24.04 server and can now use it with web applications to increase their performance. For more information, visit the Memcached documentation