Tips / Nginx

How to install Memcached on Ubuntu


Memcached is a system daemon that allows you to cache information on RAM memory, this allows you better app speeds and avoid bad performance on your dedicated or vps server. Memcached works at system level, however you can use Memcached with many programming languages in order to optimize your apps. On this example I will show you how to install memcached on Ubuntu with the php-memcache extension.

Memcached Pre requisites

VPS/Dedicated server is a must, otherwise you won’t be able to install Memcached on your system. This are two great hosting providers that I can fully recommend:

  • A Small Orange
  • A2 Hosting

Let’s begin with the tutorial now.

Ensure all packages are up to date

sudo apt-get update

Installing PHP basic modules, along with compilers

sudo apt-get install mysql-server php5-mysql php5 php5-memcache build-essential

Install Memcached

sudo apt-get install memcached

Installing php-pear (this is where we will get memcache module)

sudo apt-get install php-pear

Install memcache php extension using PECL

sudo pecl install memcache

If the PECL installation asks this question:

"Enable memcache session handler support? [yes]"

Just type enter in order to continue without issues.

Last step: add your new module to PHP memcache.ini file:

touch /etc/php5/conf.d/memcache.ini
echo "" >> /etc/php5/conf.d/memcache.ini

Start memcached

sudo service memcached start

Reload your PHP server to load the memcached extension

If you use php-fpm:

sudo service php5-fpm reload

If you use Apache:

sudo service apache2 reload

Test memcached

ps aux | grep memcache

You should see something like this:

memcache 14289 0.0 0.0 38358 1172 ? Sl 06:15 0:00 /usr/bin/memcached -m 64 -p 11211 -u memcache -l

Remember to configure memcached at system level from:


Popular search terms:

  • memchached windows nginx
  • nginx pecl memcached
  • install memcache c on ubuntu
  • memcached nginx ubuntu

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking

  • Don’t forget to install the memcached handler for PHP too. The memcached is faster than the old memcache and can deliver better results. In php.ini you can put session.save_handler = memcached and session.save_path = “” to speedup sessions by using the memcached php extension. You can speedup and compact session data even more, by installing the igbinary extension and by putting session.serialize_handler = igbinary (and apc.serializer=igbinary when using APC or APCu too) in php.ini.

  • dongpt

    Could you plz help to clarify what difference between two below packages:

    Package Stable/(Latest) Local
    memcache 3.0.8 (beta) memcached extension
    memcached 2.2.0 (stable) PHP extension for interfacing with memcached via libmemcached library

    Thank you!