{"id":3939,"date":"2021-01-28T20:26:10","date_gmt":"2021-01-28T20:26:10","guid":{"rendered":"https:\/\/linuxways.net\/?p=3939"},"modified":"2021-01-28T20:26:12","modified_gmt":"2021-01-28T20:26:12","slug":"how-to-install-memcached-on-ubuntu-20-04","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/how-to-install-memcached-on-ubuntu-20-04\/","title":{"rendered":"How to Install Memcached on Ubuntu 20.04"},"content":{"rendered":"<p>One of the issues that impact the performance of web applications is high database loads. A high load increases the latency and causes slowdowns in your web applications. On production servers, this is highly undesirable and can make you lose potential customers. One of the ways of speeding up your applications is investing in a solid caching system. Memcached is an open-source high-performance distributed caching system that helps speed up your web applications by alleviating the database load. It stores data objects in dynamic memory, which is a temporary memory storage location &#8211; instead of applications always accessing the data afresh from the database. The cached data alleviates the workload and make it much easier for the application to retrieve the required data.<\/p>\n<p>In this guide, we walk you through the installation of Memcached on Ubuntu 20.04 LTS.<\/p>\n<h2><strong>Step 1: Install Memcached<\/strong><\/h2>\n<p>Installing Memcached is quite easy and straightforward. This is largely due to the fact that it is available in Ubuntu 20.04 repositories, and it\u2019s, therefore, possible to install it via the APT package manager.<\/p>\n<p>But first, let update the package lists on our system:<\/p>\n<pre><strong>$ sudo apt update<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"735\" height=\"246\" class=\"wp-image-3940\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-496.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-496.png 735w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-496-300x100.png 300w\" sizes=\"auto, (max-width: 735px) 100vw, 735px\" \/><\/p>\n<p>Next, we are going to install the Memcached package along with a CLI utility known as <strong>libmemcached-tools <\/strong>as follows:<\/p>\n<pre><strong>$ sudo apt install memcached libmemcached-tools<\/strong><\/pre>\n<p>When prompted, hit \u2018Y\u2019 to get along with the installation of Memcached.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"358\" class=\"wp-image-3941\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-497.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-497.png 739w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-497-300x145.png 300w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/p>\n<h2><strong>Step 2: Check the status of Memcached<\/strong><\/h2>\n<p>By default, Memcached listens on port 11211. You can verify this by using the netstat command as shown:<\/p>\n<pre><strong>$ sudo netstat -pnltu<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"930\" height=\"283\" class=\"wp-image-3942\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-498.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-498.png 930w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-498-300x91.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-498-768x234.png 768w\" sizes=\"auto, (max-width: 930px) 100vw, 930px\" \/><\/p>\n<p>Additionally, you can use systemd to confirm if the Memcached daemon is running:<\/p>\n<pre><strong>$ sudo systemctl status memcached<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"736\" height=\"331\" class=\"wp-image-3944\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-499.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-499.png 736w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-499-300x135.png 300w\" sizes=\"auto, (max-width: 736px) 100vw, 736px\" \/><\/p>\n<p>From the output above, we can clearly observe that the Memcached service is active and running. If by any chance, Memcached is not running, you can start the service as shown.<\/p>\n<pre><strong>$ sudo systemctl start memcached<\/strong><\/pre>\n<p>To make it autostart on boot time, enable Memcached as follows:<\/p>\n<pre><strong>$ sudo systemctl enable memcached<\/strong><\/pre>\n<p>To view server statistics, execute:<\/p>\n<pre><strong>$ memcstat --servers localhost<\/strong><\/pre>\n<p>You will get a flurry of information such as the PID, uptime ( seconds ) time, version and so much more.<\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"815\" height=\"339\" class=\"wp-image-3945\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-500.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-500.png 815w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-500-300x125.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-500-768x319.png 768w\" sizes=\"auto, (max-width: 815px) 100vw, 815px\" \/><\/strong><\/p>\n<h2><strong>Step 3: Configure Memcached<\/strong><\/h2>\n<p>If Memcached is sitting on the webserver where your web application is running, no configuration is needed since Memcached, by default, is set to run on the localhost system.<\/p>\n<p>However, if Memcached is installed on a separate server, some added configuration is needed for the webserver to make a remote connection to the Memcached server.<\/p>\n<p>Let\u2019s assume that the client IP &#8211; where the web application is hosted &#8211; is 192.168.2.10 and the Memcached server IP is 192.168.2.20.<\/p>\n<p>Firstly, we need to edit the Memcached configuration file which is the <strong>\/etc\/memcached.conf<\/strong> file.<\/p>\n<pre><strong>$ sudo vim \/etc\/memcached.conf<\/strong><\/pre>\n<p>Scroll and be sure to locate this line:<\/p>\n<pre><strong>-l 127.0.0.1<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"882\" height=\"254\" class=\"wp-image-3946\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-501.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-501.png 882w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-501-300x86.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-501-768x221.png 768w\" sizes=\"auto, (max-width: 882px) 100vw, 882px\" \/><\/p>\n<p>Replace the localhost address with Memcached server IP<\/p>\n<pre><strong>-l 192.168.2.20<\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"881\" height=\"247\" class=\"wp-image-3947\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-502.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-502.png 881w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-502-300x84.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/01\/word-image-502-768x215.png 768w\" sizes=\"auto, (max-width: 881px) 100vw, 881px\" \/><\/p>\n<p>To effect the changes, restart Memcached as follows:<\/p>\n<pre><strong>$ sudo systemctl restart memcached<\/strong><\/pre>\n<p>Thereafter, allow traffic from the client IP to the Memcached server through the UFW firewall.<\/p>\n<pre><strong>$ sudo ufw allow from 192.168.2.10 to any port 11211<\/strong><\/pre>\n<h2><strong>Step 4: Use Memcached server<\/strong><\/h2>\n<p>Memcached requires a language-specific client for our web application to communicate with it. Thankfully, Memcached supports a wide range of popular programming languages such as PHP and Python. A good number of web apps run on PHP and Memcached is a popular caching system used by web developers to boost the performance of web applications.<\/p>\n<p>To enable Memcache support for PHP, invoke the command:<\/p>\n<pre><strong>$ sudo apt install php-memcached<\/strong><\/pre>\n<p>For Python support, use the pip package manager to install the required tools as shown:<\/p>\n<pre><strong>$ pip install python-memcached<\/strong><\/pre>\n<pre><strong>$ pip install pymemcache<\/strong><\/pre>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>Memcached has proved to be a very reliable caching system since its inception in 2003 and continues to be a favorite among developers in speeding up web applications. We do hope that this guide provided valuable insights on how you can install Memcached on Ubuntu 20.04.<\/p>","protected":false},"excerpt":{"rendered":"<p>One of the issues that impact the performance of web applications is high database loads. A high load increases the latency and causes slowdowns in your web applications.&hellip;<\/p>","protected":false},"author":32,"featured_media":3943,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-3939","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/3939","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/32"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=3939"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/3939\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/3943"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=3939"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=3939"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=3939"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}