Memcached can be extremely useful for speeding up response times on dynamic websites. Because the software resides in RAM and not on external storage devices, it can take advantage of when your website needs information quickly without waiting. When used correctly, Memcached can significantly improve the user experience on your website by reducing latency and increasing the overall speed of response times. Additionally, because Memcached is open-source, anyone can use it without paying licensing fees. However, because Memcached relies on RAM, it can be expensive to scale up if you have a large website with a lot of traffic. Overall, Memcached can be a great way to improve the performance of your website, but you need to carefully consider whether or not it is the right solution for your specific needs.
The following tutorial will teach you how to install and configure Memcached on your Ubuntu 22.04 or Ubuntu 20.04 LTS Linux system and some basic configuration examples.
Table of Contents
Recommended Steps Before Installation
Before proceeding with the tutorial, ensuring your system is up-to-date with all existing packages is good.
sudo apt update
Optionally, you can list the updates for users who require review or are curious.
sudo apt --list upgradable
Proceed to upgrade any outdated packages using the following command.
sudo apt upgrade
Install Memcached on Ubuntu Linux
By default, Ubuntu 22.04 has Memcached in its repositories. To install Memcached, enter the following command:
sudo apt install memcached libmemcached-tools -y
Note the second install option, the “libmemcached-tools“ package, provides additional commands to bring extra interaction and execute abilities to Memcached.
Next, verify Memcached was installed correctly using the –version input command.
memcached --version
Example output:

Enable Memcached Service on Ubuntu Linux
Memcached should be activated by default. To verify this, use the systemctl status command as follows.
systemctl status memcached
Example output:
If the service has not been activated, start Memcached using the following command:
sudo systemctl enabled memcached --now
Other useful commands for managing the Memcached service are as follows:
Start the Memcached service in the current user session:
sudo systemctl start memcached
Enable the Memcached service on system boot:
sudo systemctl enable memcached
Stop the Memcached service:
sudo systemctl stop memcached
Disable the Memcached service on system boot:
sudo systemctl disable memcached
Restart the Memcached service:
sudo systemctl restart memcached
Next, verify Memcached is actively listening to localhost on the default port 11211. To confirm this type, the following:
ps -ef | grep memcached
Example output:

Configure Memcached on Ubuntu Linux
Memcached must be listening to 127.0.0.1. Next, open the default setting in the configuration file located at “/etc/memcached.conf”.
sudo nano /etc/memcached.conf
Now scroll down, find the following line, and check the “-l” parameter. Leave as the default unless you have internal IP on a local network or external IP from outside; you must modify the default IP address from 127.0.0.1 to the new IP address.
-l 127.0.0.1
It is recommended to disable UDP. Unless you require this function to be enabled, add the following line to disable it.
-U 0
Before you finish, changing the default 64MB memory allocation is recommended as this isn’t much for larger websites, and you will not see much benefit using Memcached.
You need to set this to something reasonable for your server. If you have 3 to 6 GB of hardly used RAM, put it to 1GB or 2GB. This is a setting you must decide that runs best for your server. The guide’s server runs on 8GB, so we only adjusted our cache to 2GB as an example and will increase as needed.
-m 2000
Now save the file CTRL+O and hit “Y,” then CTRL+X to exit and restart your Memcached instance.
sudo systemctl restart memcached
Allow Memcached UFW Rules on Ubuntu Linux
If you have UFW active, you must create UFW allow rules on the TCP port 11211. Depending on your installation and requirements if using singular or in a cluster network, some examples are below:
Singular IP network connection example:
sudo ufw allow proto tcp from <ip address> to any port 11211
Cluster IP network connection with many instances example:
sudo ufw allow proto tcp from <ip address>/24 to any port 11211
Note the second UFW rule is a subnet rule. Note, ensure the internal network is secure and trustworthy before allowing it.
Install Memcached PHP Libraries on Ubuntu Linux
Memcached comes with various extensions for the programming languages, but it is primarily used for the PHP. To install the PHP library enter the following.
sudo apt install php-memcached apache2 libapache2-mod-php php php-cli php-memcached php-memcached
Apache HTTP Server
Apache users can execute the following code to enable Memcached on their system.
phpenmod memcached && sudo service apache2 restart
Nginx HTTP Server
Memcached for Nginx will be enabled in your PHP block by default once installed.
Install Additional Libraries on Ubuntu Linux
You can install Python and or Perl support by executing the following commands.
Install Memcached Python support:
sudo apt install python3-pymemcache -y
Install Memcached Perl support:
sudo apt install libcache-memcached-libmemcached-perl -y
Access Memcached from Command Line on Ubuntu Linux
Memcached stats can be gathered by much additional software and WEB UI’s that it works with. However, a better way to check is to interact with Memcached directly using the command line.
First, TELNET into your service. Remember, if you use Telnet, I would lock it off with the UFW firewall rule and use it in a local environment only, do not use it from outside. For example, Telnet from your PC to your server with TELNET. You would SSH in, then open a Telnet session in your server’s local environment.
telnet localhost 11211
Example output:
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Install Telnet using the following command if it is not installed.
apt-get install telnet
Remember, ensure you have UFW firewall rules set and use only on the local server, do not connect remotely outside the server.
Next, you can get an overview of your Memcached service by using the following command:
stats
Example output:
STAT pid 5934
STAT uptime 1275
STAT time 1631930242
STAT version 1.6.9
STAT libevent 2.1.12-stable
STAT pointer_size 64
STAT rusage_user 0.113635
STAT rusage_system 0.000000
STAT max_connections 1024
STAT curr_connections 1
STAT total_connections 2
STAT rejected_connections 0
STAT connection_structures 2
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
STAT get_hits 0
STAT get_misses 0
STAT get_expired 0
STAT get_flushed 0
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 7
STAT bytes_written 0
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT time_in_listen_disabled_us 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT slab_reassign_rescues 0
STAT slab_reassign_chunk_rescues 0
STAT slab_reassign_evictions_nomem 0
STAT slab_reassign_inline_reclaim 0
STAT slab_reassign_busy_items 0
STAT slab_reassign_busy_deletes 0
STAT slab_reassign_running 0
STAT slabs_moved 0
STAT lru_crawler_running 0
STAT lru_crawler_starts 6
STAT lru_maintainer_juggles 1325
STAT malloc_fails 0
STAT log_worker_dropped 0
STAT log_worker_written 0
STAT log_watcher_skipped 0
STAT log_watcher_sent 0
STAT unexpected_napi_ids 0
STAT round_robin_fallback 0
STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT slab_global_page_pool 0
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT evicted_active 0
STAT evictions 0
STAT reclaimed 0
STAT crawler_reclaimed 0
STAT crawler_items_checked 0
STAT lrutail_reflocked 0
STAT moves_to_cold 0
STAT moves_to_warm 0
STAT moves_within_lru 0
STAT direct_reclaims 0
STAT lru_bumps_dropped 0
END
As above, this shows some crucial things you may want to see, such as uptime, the number of items in the cache, and the number of client connections to the instance
You can refine the search by looking into the different Memcached slabs (partitions) of memory to return results.
Examples below:
List the slabs in the instance connected:
stats slabs
List of slabs which includes a count of the items stored within each slab:
stats items
Next, you can access and delete data using the cachedump command to list the keys.
First, execute the cachedump command:
stats cachedump [slab ID] [number of items, 0 for all items]
Example in action:
stats cachedump 1 0
Example output:
ITEM testkey [9 b; 1296857316 s]
END
As mentioned above, SLAB 1 has one item with the “testkey.” To get the actual value, you can use the “get key” command as follows:
get testkey
Example output:
VALUE testkey 0 9
test data
END
Lastly, to delete a cached item, in this case, the “testkey” uses the following command:
delete testkey
Example output:
DELETED