How to Install Memcached on Ubuntu 22.04 LTS

Memcached is a free, open-source project that anyone can use to speed up response times for dynamic websites. As the software lives in RAM and not on external storage devices like hard drives or SSDs, does it allow you to take advantage of when your website needs information quickly without having to wait anywhere from seconds until to almost minutes on high traffic websites that are not optimized correctly with software such as Memcached.

In the following tutorial, you will know how to install and configure Memcached on your Ubuntu 22.04 Jammy Jellyfish and some basic configuration examples.

Update System

First, before you begin, update your system to avoid any conflicts during the installation.

sudo apt update && sudo apt upgrade

Install Memcached

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 by verifying using the –version input command.

memcached --version

Example output:

How to Install Memcached on Ubuntu 22.04

Enable Memcached Service

Memcached should be activated by default. To verify this, use the systemctl status command as follows.

systemctl status memcached

Example output:

How to Install Memcached on Ubuntu 22.04

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:

How to Install Memcached on Ubuntu 22.04

Configure Memcached

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 and 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

Optional – Allow Memcached UFW Rules

If you have UFW active, you need to 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

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

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

Memcached stats can be gathered by much additional software and WEB UI’s that it works in conjunction with. However, a better way to check is to interact with Memcached directly using the command line.

First, TELNET into your service:

telnet localhost 11211

Example output:

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

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

Comments and Conclusion

The tutorial has shown you how to install Memcached on your Ubuntu 22.04 Jammy Jellyfish operating system, install the additional libraries, and access the terminal command list.

For further documentation, the project’s Github Wiki page explains further in great detail for server admins any specifications or information they require.



Follow LinuxCapable.com!

Like to get automatic updates? Follow us on one of our social media accounts!