Skip to content

Install the PHP Memcached extension

The PHP memcached extension provides client access to the Memcached server.

The memcached extension supports PHP 7.0+.

Install the Memcached extension

Installing the extension involves the following steps:

  1. Install build dependencies (packages that are required to build the extension).
  2. Build the extension’s library file.
  3. Enable the extension in the PHP configuration.

Install build dependencies

Set environment variables for the package manager.

Terminal window
export DEBIAN_FRONTEND=noninteractive

Install a compiler and other packages that are required for building PHP extensions.

Terminal window
sudo apt-get -y install gcc g++ make autoconf libc-dev pkg-config

Install additional packages that are required to build the Memcached extension.

Terminal window
sudo apt-get -y install zlib1g-dev libmemcached-dev

Build the extension

The PECL install command compiles the extension’s .so library file.

Terminal window
sudo pecl8.4-sp install memcached

The above command will prompt you to answer multiple questions. When shown the enable sasl prompt, type “no” and then press Enter. For all other prompts, just press Enter.

For example:

zlib directory [no] :
use system fastlz [no] :
enable igbinary serializer [no] :
enable msgpack serializer [no] :
enable json serializer [no] :
enable server protocol [no] :
enable sasl [yes] : no
enable sessions [yes] :

Enable the extension

Configure PHP to load the extension.

Terminal window
sudo bash -c "echo extension=memcached.so > /etc/php8.4-sp/conf.d/memcached.ini"

Restart PHP-FPM.

Terminal window
sudo service php8.4-fpm-sp restart

Verify the extension is enabled in PHP.

Terminal window
php8.4-sp -i | grep memcached

If the extension is enabled, you will see output like the following.

memcached support => enabled

Uninstall the Memcached extension

Uninstalling the extension involves the following steps:

  1. Disable the extension in the PHP configuration.
  2. Remove the extension’s library file.

Disable the extension

Remove the extension’s configuration from PHP.

Terminal window
sudo rm /etc/php8.4-sp/conf.d/memcached.ini

Restart PHP-FPM.

Terminal window
sudo service php8.4-fpm-sp restart

Remove the extension

The PECL uninstall command removes the extension’s .so library file.

Terminal window
sudo pecl8.4-sp uninstall memcached