Installing the PHP "memcached" Extension

Please Note

This tutorial only covers the default PHP installation that ships with Mac OS X / macOS. If you have installed a new installation this does not cover you.

For those using brew, you can simply brew install the appropriate brew formula for the extension.

These directions are verified to work with Mac OS X 10.9 Mavericks through macOS 10.14 Mojave.

System Integrity Protection

If you are running OS X El Capitan or newer you will need to disable System Integrity Protection to modify system files and directories.

To disable System Integrity Protection, boot into recovery mode by restarting and then holding ⌘R as you hear the startup chime. Then start the Terminal from the Utilities menu.

Run the following command

$ csrutil disable

Then reboot. You are good to go.

If you wish to turn it back on, follow the instructions above but instead use:

$ csrutil enable
Click here for a full post on the topic.

The first step is to install the latest version of Xcode. On modern versions of MacOS this can be done from the App Store.

After this, install the Xcode developer tools.

$ xcode-select --install

Special note to Mojave users if the following steps fail, you may need to reinstall your Xcode header files. Instructions for that can be found here.

Next we will need to install the required dependencies before we can build the extension. If you are not already using Homebrew you should be.

$ brew install wget autoconf pkg-config libmemcached

You will also want to make sure you have PEAR installed; full instructions can be found here but can be summarized as follows:

$ wget http://pear.php.net/go-pear.phar
$ php go-pear.phar

Then in the /tmp directory we will create a folder we can work in.

$ cd /tmp
$ mkdir memcached-work
$ cd memcached-work

Next we will use pecl, part of pear, to fetch the current version of the extension.

The version of the memcached extension will vary so you will need to update the paths accordingly.

$ pecl download memcached
$ open memcached-{{version}}.tgz
$ cd memcached-{{version}}/memcached-{{version}}
$ phpize
$ ./configure
$ make
$ sudo make install

Finally you will need to add the following line to your php.ini

extension = memcached.so

You can verify your installation with the following:

$ php --info | grep memcached\\.

Depending on your setup now you may want to restart apache.

$ sudo apachectl restart

You should be all set to go!