There is a new version of this tutorial available for Ubuntu 24.04 (Noble Numbat).

How to Install Memcached on Ubuntu 18.04 LTS

Memcached is a free and open-source memory object caching systems that can be used to speed up your applications by temporarily storing information in memory. Memcached helps you to speed up web applications like WordPress, Drupal, Joomla and other PHP based applications.

In this tutorial, we will explain how to install Memcached on Ubuntu 18.04 server.

Requirements

  • A server running Ubuntu 18.04.
  • A root password is configured on your server.

Update Your System

First, update all the packages to the latest version with the following command:

apt-get update -y
apt-get upgrade -y

Once all the packages are updated, restart your system to apply the configuration changes.

Install and Configure Memcached

By default, Memcached is available in the Ubuntu 18.04 default repository. You can install it by just running the following command:

apt-get install memcached libmemcached-tools -y

After installing Memcached, start the Memcached service and enable it to start after system reboot with the following command:

systemctl start memcached
systemctl enable memcached

You can now check the status of Memcached service with the following command:

systemctl status memcached

You should see the output similar to the following:

? memcached.service - memcached daemon
   Loaded: loaded (/lib/systemd/system/memcached.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2019-07-13 06:13:59 UTC; 13s ago
     Docs: man:memcached(1)
 Main PID: 21776 (memcached)
    Tasks: 10 (limit: 1114)
   CGroup: /system.slice/memcached.service
           ??21776 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached.pid

Jul 13 06:13:59 ubuntu1804 systemd[1]: Started memcached daemon.

You can also check whether the Memcached service is running by typing:

ps aux | grep memcached

You should see the following output:

memcache 21776  0.3  0.2 424764  2880 ?        Ssl  06:13   0:00 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1 -P /var/run/memcached/memcached.pid
root     22035  0.0  0.0  13136  1000 pts/0    S+   06:14   0:00 grep --color=auto memcached

Next, you will need to configure Memcached for more advanced settings. To do so, open /etc/memcached.conf file as shown below:

nano /etc/memcached.conf

Change the following lines as per your need:

# Default connection port is 11211
-p 11211

# Specify which IP address to listen on.
-l 192.168.0.101
#Define the maximum number of Memory can be used by Memcached deamon.
-m 256

Save and close the file then restart Memcached services for the changes to apply.

systemctl restart memcached

Install Apache and PHP and Configure PHP to Use Memcached

Memcached is now installed and configured. Next, you will need to install and configure Apache to use Memcached.

First, install Apache and PHP with other modules with the following command:

apt-get install apache2 php7.2 libapache2-mod-php7.2 php-memcached php7.2-cli -y

Next, create a sample phpinfo.php file in Apache web root directory to test Memcached.

nano /var/www/html/phpinfo.php

Add the following code:

<?php 
phpinfo();
?>

Save and close the file. Then, open your web browser and type the URL http://your-server-ip/phpinfo.php. You should see all the PHP related modules and Memcached information in the following page:

Memcached enabled in PHP

Congratulations! you have successfully installed Memcached on Ubuntu 18.04 server. For more information read the official documentation at Memcache Doc. Feel free to ask me if you have any questions.

Share this page:

1 Comment(s)