Articles

How to Install the PHP Memcached Extension

In the examples shown, replace "X.Y" with your app's PHP version (for example, "7.1" or "5.6"). To install a PECL extension for multiple PHP versions, repeat the PECL extension installation for each PHP version.
These PHP extensions provide client access to the Memcached server. They are not the Memcached server itself.

You also need to install the Memcached server to use Memcached with your PHP apps.

Memcached

Installing Memcached on PHP 7.0, 7.1, and 7.2

To install this extension for PHP 7.X, SSH in to your server as root and run the following commands:

sudo apt-get -y install gcc make autoconf libc-dev pkg-config
sudo apt-get -y install zlib1g-dev
sudo apt-get -y install libmemcached-dev
sudo pecl7.X-sp install memcached

When you are shown the prompt

libmemcached directory [no] :

type or paste the following text exactly as shown and press Enter.

no --disable-memcached-sasl

That is, the entire line you'll see on your screen will be as follows once you press Enter.

libmemcached directory [no] : no --disable-memcached-sasl

Once installed, create a configuration file for the extension and restart PHP by running the following command as root

sudo bash -c "echo extension=memcached.so > /etc/php7.X-sp/conf.d/memcached.ini"
sudo service php7.X-fpm-sp restart

Installing Memcached on PHP 5.4, 5.5, and 5.6

To install this extension, SSH in to your server as root and run the following command:

sudo apt-get -y install gcc make autoconf libc-dev pkg-config
sudo apt-get -y install zlib1g-dev
sudo apt-get -y install libmemcached-dev
sudo pecl5.X-sp install memcached-2.2.0

When you are shown the prompt

libmemcached directory [no] :

type or paste the following text exactly as shown and press Enter.

no --disable-memcached-sasl

That is, the entire line you'll see on your screen will be as follows once you press Enter.

libmemcached directory [no] : no --disable-memcached-sasl

Once installed, create a configuration file for the extension and restart PHP by running the following command as root

sudo bash -c "echo extension=memcached.so > /etc/php5.X-sp/conf.d/memcached.ini"
sudo service php5.X-fpm-sp restart

Verifying the Installation

You can verify the memcached extension is installed correctly by running this command:

phpX.Y-sp -i | grep -i "memcached support"

You should see output like the following:

memcached support => enabled

Memcache

Two different extensions are available that provide access to Memcached from within your PHP scripts. One is named memcache and the other is named memcached. This extension (the one named memcache with no d on the end) is no longer maintained.
For instructions on the memcached extension (with a d on the end), see the section above.

To install this extension, SSH in to your server as root and run the following command:

sudo apt-get install gcc make autoconf libc-dev pkg-config
sudo apt-get install zlib1g-dev
sudo pecl5.X-sp install memcache

Once installed, create a configuration file for the extension and restart PHP.

sudo bash -c "echo extension=memcache.so > /etc/php5.X-sp/conf.d/memcache.ini"
sudo service php5.X-fpm-sp restart

The memcache extension does not support PHP 7. For PHP 7 support, use the memcached extension shown above.


Last updated: August 31, 2017