How to Install Memcached on Ubuntu 22.04
This tutorial exists for these OS versions
- Ubuntu 24.04 (Noble Numbat)
- Ubuntu 22.04 (Jammy Jellyfish)
- Ubuntu 20.04 (Focal Fossa)
- Ubuntu 18.04 (Bionic Beaver)
On this page
Memcached is a free, open-source, and general-purpose distributed memory-caching system used to cache database data. It is a high-performance memory caching system used to speed up dynamic web applications by reducing the database load. Memcached is made from four components that allow the client and the server to work together in order to deliver cached data as efficiently as possible. It offers API integration for all the major languages like PHP, Java, C/C++, Python, Ruby, Perl, etc.
This tutorial will show you how to install Memcached server on Ubuntu 22.04 server an how to use it with PHP.
Requirements
- A server running Ubuntu 22.04.
- A root password is configured on your server.
Install Memcached on Ubuntu 22.04
By default, the Memcached package is included in the Ubuntu default repository. You can install it easily using the following command:
apt install memcached libmemcached-tools -y
Once the Memcached is installed, start the Memcached service using the following command:
systemctl start memcached
You can check the status of the Memcached with the following command:
systemctl status memcached
You should get the following output:
? memcached.service - memcached daemon Loaded: loaded (/lib/systemd/system/memcached.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2022-10-18 13:09:47 UTC; 29s ago Docs: man:memcached(1) Main PID: 5525 (memcached) Tasks: 10 (limit: 2242) Memory: 1.7M CPU: 71ms CGroup: /system.slice/memcached.service ??5525 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached.pid Oct 18 13:09:47 ubuntu2204 systemd[1]: Started memcached daemon.
To verify the Memcached version, run the following command:
memcached -V
You will get the following output:
memcached 1.6.14
By default, Memcached listens on port 11211. You can check it with the following command:
ss -plunt | grep memcache
You should see the following output:
tcp LISTEN 0 1024 127.0.0.1:11211 0.0.0.0:* users:(("memcached",pid=5525,fd=22))
Install PHP Memcached Extension
In order to use the Memcached with PHP-based applications. You will need to install the PHP Memcached extension to your server. You can install it with Apache using the following command:
apt install apache2 php libapache2-mod-php php-memcached php-cli -y
Next, create a sample PHP test page to verify the Memcached:
nano /var/www/html/info.php
Add the following code:
<?php phpinfo();
Save and close the file then restart the Apache service to apply the changes:
systemctl restart apache2
Next, open your web browser and access the PHP page using the URL http://your-server-ip/info.php. You should see the Memcached on the following screen:
Next, create another PHP test page to verify whether the Memcached works or not.
nano /var/www/html/test.php
Add the following code:
<?php try { $memcached = new Memcached(); $memcached->addServer("127.0.0.1", 11211); $response = $memcached->get("key_cache"); if($response==true) { echo "Result coming from caching"; echo $response; } else { echo "Cache is not created yet, reload again to see changes"; $memcached->set("key_cache", "Hooray! Memcache is working now ...") ; } } catch (exception $e) { echo $e->getMessage(); } ?>
Save and close the file then open your web browser and access the PHP test page using the URL http://your-server-ip/test.php. You should see the following screen:
Next, reload the page again. This time the page load from the cache memory as shown below:
Secure Memcached with Authentication
It is recommended to use Simple Authentication and Security Layer (SASL) to secure the Memcached with user-based authentication. To do so, you will need to install the sasl2-bin package on your server.
apt install sasl2-bin
Next, create a directory and configuration file for SASL authentication:
mkdir -p /etc/sasl2
nano /etc/sasl2/memcached.conf
Add the following lines:
log_level: 5 mech_list: plain sasldb_path: /etc/sasl2/memcached-sasldb2
Save and close the file then create a SASL database with user credentials:
saslpasswd2 -a memcached -c -f /etc/sasl2/memcached-sasldb2 user1
Set your password as shown below:
Password: Again (for verification):
Next, change the ownership of the Memcached database:
chown memcache:memcache /etc/sasl2/memcached-sasldb2
Next, verify the Memcached using the following command:
memcstat --servers="127.0.0.1"
You should get the following output:
Server: 127.0.0.1 (11211) pid: 5525 uptime: 462 time: 1666099048 version: 1.6.14 libevent: 2.1.12-stable pointer_size: 64 rusage_user: 0.110123 rusage_system: 0.093906 max_connections: 1024 curr_connections: 1 total_connections: 4 rejected_connections: 0 connection_structures: 2 response_obj_oom: 0 response_obj_count: 1 response_obj_bytes: 49152 read_buf_count: 6 read_buf_bytes: 98304 read_buf_bytes_free: 32768 read_buf_oom: 0 reserved_fds: 20 cmd_get: 2 cmd_set: 1 cmd_flush: 0 cmd_touch: 0 cmd_meta: 0 get_hits: 1 get_misses: 1 get_expired: 0 get_flushed: 0 delete_misses: 0
Next, edit the Memcached configuration file:
nano /etc/memcached.conf
Add the following lines:
-S -vv
Save and close the file then restart the Memcached service to apply the changes:
systemctl restart memcached
Next, verify the SASL support using the following command:
journalctl -u memcached |grep SASL
If everything is fine, you will get the following output:
Journal file /var/log/journal/d97e195db6584d63aeedfdc35dc83c7f/user-1000.journal is truncated, ignoring file. Oct 18 13:18:19 ubuntu2204 systemd-memcached-wrapper[14980]: Initialized SASL.
Verify Memcached Authentication
First, try to check connectivity to Memcached again without authentication:
memcstat --servers="127.0.0.1"
The command should not produce output. Type the following shell command to check its status:
echo $?
You should receive the following status code means the memcstat command failed.
1
Next, run the Memcached command again by specifying the username and password:
memcstat --servers="127.0.0.1" --username=user1 --password=password
You will get the following output:
Server: 127.0.0.1 (11211) pid: 14980 uptime: 46 time: 1666099143 version: 1.6.14 libevent: 2.1.12-stable pointer_size: 64 rusage_user: 0.077843 rusage_system: 0.049072 max_connections: 1024 curr_connections: 1 total_connections: 3 rejected_connections: 0 connection_structures: 2 response_obj_oom: 0 response_obj_count: 1 response_obj_bytes: 16384 read_buf_count: 3 read_buf_bytes: 49152 read_buf_bytes_free: 16384 read_buf_oom: 0
Install Memcached for Python
If you want to integrate Memcached with Python-based web applications, then you will need to install the Memcached extension for Python. You can install it with the following command:
apt install python3-pip
pip install pymemcache
pip install python-memcached
Conclusion
In this post, we explained how to install Memcached server on Ubuntu 22.04. We also explained how to secure the Memcached with SASL-based authentication. I hope you can now integrate Memcached with your application to speed up the application performance. Feel free to ask me if you have any questions.