How to enable the memcached PHP extension after installing with homebrew?

How to enable the memcached PHP extension after installing with homebrew?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Path from Homebrew Memcached to PHP's Extension List

Many developers encounter friction when trying to integrate a system service like Memcached with their PHP runtime, specifically after installing via Homebrew on macOS. A common scenario involves running brew install memcached and then manually editing the PHP configuration file at /etc/php.ini to append extension=memcached.so. Despite restarting Apache or Nginx, calling get_loaded_extensions() in a script yields no results. This failure usually stems from a fundamental mismatch between how Homebrew manages library paths and where PHP expects to find compiled extensions.

When you install software with Homebrew, the resulting libraries are placed in non-standard directories, typically under /usr/local or /opt/homebrew. However, the extension=memcached.so directive in your php.ini file is looking for a specific shared object file that likely does not exist at that location. The core issue is that the PHP extension compiled against Homebrew's Memcached binary requires the corresponding .so file to be present in a directory that PHP can actually find during its initialization phase.

To resolve this, you must first identify the exact path where the memcached.so file resides after installation. Since Homebrew isolates its software, the extension is almost certainly not located at /usr/lib or /usr/local/lib. Instead, it will be found within the Homebrew Cell, usually in a path resembling /opt/homebrew/Cellar/memcached/{version}/lib/php/ or /usr/local/Cellar/memcached/{version}/lib/php/, depending on your macOS architecture (Apple Silicon vs. Intel) and PHP version.

You can locate this file by running the following command in your terminal:

find /opt/homebrew -name "memcached.so" 2>/dev/null || find /usr/local -name "memcached.so" 2>/dev/null

Once you have identified the absolute path to the .so file, you cannot simply add extension=memcached.so to your global php.ini. You must instead specify the full path. For example:

extension=/opt/homebrew/Cellar/memcached/1.6.28/lib/php/extension_memcached.so

However, a more robust approach involves checking how your PHP installation is configured to search for extensions. If you are using a managed PHP version (like those provided via Homebrew's php formula itself), the binary might be dynamically linking against libraries in /opt/homebrew. In these cases, simply adding the extension line might work if the linker can resolve the dependencies. If it fails, you may need to run the command brew install php-memcached, which provides a pre-compiled and linked extension specifically for your PHP version. This is often safer than manually compiling or guessing paths.

Another critical pitfall involves the order of operations in php.ini. The extension directive must appear before any logic that might trigger an error if the file is missing, though this is rare with standard extensions. More importantly, ensure you are editing the correct php.ini. On macOS, PHP can be installed in multiple locations (system-wide, user-space, or via Homebrew), and each may have its own configuration file. Using php --ini from the command line reveals which php.ini is actually being loaded. Modifying a system config at /etc/php.ini while running a PHP binary from /usr/local/bin often leads to confusion because they are effectively different environments.

If you are building a Laravel application or working within a shared hosting environment, manual edits to global php.ini files can be overwritten by deployment tools or package managers. In such scenarios, placing the extension configuration in a user-specific config file (like ~/.php.ini) or using an Apache virtual host configuration with custom LoadModule directives might be necessary.

Finally, verify that the PHP binary and the Memcached client library share the same architecture. On Apple Silicon Macs, attempting to load an x86_64 extension on an arm64 system will result in a fatal error. Always ensure your Homebrew installation is up to date (brew update && brew upgrade) before attempting to load the extension, as older versions might lack compatibility with newer PHP runtimes.

By aligning the extension path with the actual installation location provided by Homebrew and ensuring the correct php.ini is targeted, the memcached extension will appear correctly in your loaded extensions list, allowing seamless interaction between your PHP application and the cache server.

Our network