How do I install memcached server under Red Hat Enterprise Linux or CentOS Linux version6.x server running on IBM hardware to cache query for my php/perl/python based apps?

memcached is a high-performance, distributed memory object caching server, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load. You need to install the following packages:
Tutorial details
Difficulty level Intermediate
Root privileges Yes
Est. reading time 10m
  • memcached : High Performance, Distributed Memory Object Cache server package.
  • perl-Cache-Memcached : Perl client (library) to work with memcached server.
  • python-memcached : Python client (library) to work with memcached server.
  • php-pecl-memcache : PHP extension to work with the Memcached server.
Memcached is used by many high traffic sites including YouTube, Reddit, Facebook, Twitter, and many others.Google App Engine, AppScale and Amazon Web Services also offer a memcached service through an API. Memcached is also supported by popular open source projects and CMSs such as Drupal, Joomla, WordPress, and many more.
WARNING! These examples only works with RHEL / CentOS Linux version 6.x or above. For RHEL 5.x and older version use memcached source code installation method.

Task: Install memcached on CentOS/RHEL

Open a terminal or login to remote server using ssh client. Type the following yum command to install the same:
# yum install memcached.x86_64 php-pecl-memcache.x86_64
# yum install memcached php-pecl-memcache
Sample outputs:

Loaded plugins: product-id, rhnplugin, subscription-manager
Updating certificate-based repositories.
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package memcached.x86_64 0:1.4.4-3.el6 will be installed
--> Processing Dependency: for package: memcached-1.4.4-3.el6.x86_64
---> Package php-pecl-memcache.x86_64 0:3.0.5-3.el6 will be installed
--> Processing Dependency: /usr/bin/pecl for package: php-pecl-memcache-3.0.5-3.el6.x86_64
--> Processing Dependency: /usr/bin/pecl for package: php-pecl-memcache-3.0.5-3.el6.x86_64
--> Running transaction check
---> Package libevent.x86_64 0:1.4.13-1.el6 will be installed
---> Package php-pear.noarch 1:1.9.4-4.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
 Package              Arch      Version           Repository               Size
 memcached            x86_64    1.4.4-3.el6       rhel-x86_64-server-6     68 k
 php-pecl-memcache    x86_64    3.0.5-3.el6       rhel-x86_64-server-6     60 k
Installing for dependencies:
 libevent             x86_64    1.4.13-1.el6      rhel-x86_64-server-6     66 k
 php-pear             noarch    1:1.9.4-4.el6     rhel-x86_64-server-6    393 k
Transaction Summary
Install       4 Package(s)
Total download size: 587 k
Installed size: 0  
Is this ok [y/N]: y
Downloading Packages:
(1/4): libevent-1.4.13-1.el6.x86_64.rpm                  |  66 kB     00:00     
(2/4): memcached-1.4.4-3.el6.x86_64.rpm                  |  68 kB     00:00     
(3/4): php-pear-1.9.4-4.el6.noarch.rpm                   | 393 kB     00:00     
(4/4): php-pecl-memcache-3.0.5-3.el6.x86_64.rpm          |  60 kB     00:00     
Total                                           2.4 MB/s | 587 kB     00:00     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Warning: RPMDB altered outside of yum.
  Installing : 1:php-pear-1.9.4-4.el6.noarch                                1/4 
  Installing : libevent-1.4.13-1.el6.x86_64                                 2/4 
  Installing : memcached-1.4.4-3.el6.x86_64                                 3/4 
  Installing : php-pecl-memcache-3.0.5-3.el6.x86_64                         4/4 
Installed products updated.
  memcached.x86_64 0:1.4.4-3.el6     php-pecl-memcache.x86_64 0:3.0.5-3.el6    
Dependency Installed:
  libevent.x86_64 0:1.4.13-1.el6          php-pear.noarch 1:1.9.4-4.el6         

How do I configure Memcached server?

Edit /etc/sysconfig/memcached file, enter:
# vi /etc/sysconfig/memcached
Sample outputs:


For busy servers you need to increase the values as follows:

  1. MAXCONN: Use 1024 max simultaneous connections; the default is 1024. For busy server increase this number as per requirements or setup.
  2. CACHESIZE: Use 64 MB memory max to use for object storage; the default is 64 megabytes. For busy server you can set it to 512MB or 1GB (1024).
  3. OPTIONS: You can set server IP address (listen on address) here so that apache/php/nginx based serer can connect to the server. By default it is set to INADDR_ANY. This is an important option to consider as there is no other way to secure the installation. Binding to an internal or firewalled network interface is suggested.

Here is my updated sample config file:


Save and close the file.

How do I start / stop / restart Memcached server?

Type the following commands:
# /etc/init.d/memcached start
# /etc/init.d/memcached stop
# /etc/init.d/memcached restart
# /etc/init.d/memcached status

OR run a System V init script using service command
# /sbin/service memcached start
# /sbin/service memcached stop
# /sbin/service memcached restart
# /sbin/service memcached status

How do I verify that Memcached is running and working correctly on Linux?

Type the following command to see if it is running or not:
# pgrep memcached
# netstat -tulpn | grep :11211

Sample outputs:

tcp        0      0 *                   LISTEN      24964/memcached     
udp        0      0 *                               24964/memcached

Use the memcached-tool to get general stats about the server:
# memcached-tool stats
Sample outputs:

# Field       Value
         accepting_conns           1
               auth_cmds           0
             auth_errors           0
                   bytes     5481902
              bytes_read     8242409
           bytes_written    26023492
              cas_badval           0
                cas_hits           0
              cas_misses           0
               cmd_flush           0
                 cmd_get        9042
                 cmd_set        4469
             conn_yields           0
   connection_structures          48
        curr_connections          47
              curr_items        3197
               decr_hits           0
             decr_misses           0
             delete_hits           6
           delete_misses         112
               evictions           0
                get_hits        7490
              get_misses        1552
               incr_hits           0
             incr_misses           0
          limit_maxbytes   536870912
     listen_disabled_num           0
                     pid       24964
            pointer_size          64
           rusage_system    0.149977
             rusage_user    0.043993
                 threads           4
                    time  1327395688
       total_connections         171
             total_items        3226
                  uptime         198
                 version       1.4.4

How do I configure Firewall to protect my Memcached server?

Make sure iptables only allows access to your own servers. Edit /etc/sysconfig/iptables and append the following rules or add it as follows to your shell scripts:

## add to your shell script ##
## assumption default INPUT policy is set to DROP ##
## only accept connection to tcp/udp port 11211 if ip is between and ##
iptables -A INPUT -p tcp --destination-port 11211 -m state --state NEW  -m iprange --src-range -j ACCEPT
iptables -A INPUT -p udp --destination-port 11211 -m state --state NEW  -m iprange --src-range -j ACCEPT

See our RHEL / CentOS firewall tutorial here and here for more information about setting up firewall.

How Do I Cache MySQL Queries Using Memcached?

You need to modify your php code using APIs. You can find examples and code samples here. Alternately, you can install memcached extensions such as WordPress extension/plugins if you are using worpdress as CMS.


  • memcached project web site
  • Man pages: memcached(1)

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 9 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
9 comments… add one
  • unwiredtech Jan 24, 2012 @ 10:22

    Thank you for this tut. I am going to implement this one on our site.

  • Ramy Allam Jan 28, 2012 @ 11:31

    Thanks a lot , I hope to add a tutorial for repcached and implementation of master slave replication for memcache

  • Santi Saez Feb 23, 2012 @ 7:10

    Hey guys,

    Actually RHEL-6 installs and outdated version of memcached (1.4.4, released ~2 years ago), I suggest using at least memcached 1.4.10, starting this release there is a big performance and scalability improvements (see developers release notes).

    Using PowerStack repository you can easily run latest memcached stable version in your CentOS and RHEL boxes (4, 5 and 6 releases supported, i686 + x86_64), last LAMP stack stable versions and other key packages like: nginx, HAproxy, node.js, redis, Varnish, etc.

    All code + .spec files are available on GitHub, check out the wiki page for installation instructions:

    BTW: we’re planning to upgrade to libevent 2 with the upcoming memcached releases :)


  • BiBi Apr 4, 2012 @ 18:03

    Thanks very much, this website is very usefull. Thanks again.

  • Mac Jan 24, 2013 @ 13:45

    The powerstack repo what other packages are available?

    Will the powerstack repo have updates for the installed packages as well, so you can keep your server(s) up to date?


  • Alper Jun 2, 2013 @ 21:57


    checking build system type… Invalid configuration `x86_64-unknown-linux-‘: machine `x86_64-unknown-linux’ not recognized
    configure: error: /bin/sh ./config.sub x86_64-unknown-linux- failed

  • Shane Nov 14, 2013 @ 5:46

    Thanks for the tutorial.

    The version I have running is 1.4.4, how do I upgrade to latest version? What commands would I need to type in ssh to make this happen?

    • 🐧 Nix Craft Dec 16, 2013 @ 12:23

      You can not update to new version. Your best choice to to uninstall package provided by Red hat and install the latest memcached using source code.

  • Friend Nov 1, 2015 @ 16:39

    Shouldn’t it be the memcached php module installed if your using memcached? Your install command reflects the php-memcache module not memcached and there are differences so it would be smart to install the proper php module.

    sudo yum search php-pecl-memcache

    php-pecl-memcache.x86_64 : Extension to work with the Memcached caching daemon
    php-pecl-memcached.x86_64 : Extension to work with the Memcached caching daemon

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum