php dyld: Library not loaded for libldap
Encountering a dyld error on macOS is a classic sign of an incompatible dynamic library load, often stemming from how Homebrew manages formulae and their dependencies. When you see dyld: Library not loaded: /usr/local/opt/openldap/lib/libldap-2.4.2.dylib while trying to run PHP 7.1, the operating system is telling you that the executable expects a specific version of the LDAP library that simply isn't present in your current environment.
This issue frequently arises on macOS High Sierra (and subsequent versions) when installing Ruby or updating PHP via Homebrew. The installation process attempts to link against openldap, but the runtime linker cannot find the expected binary at the hardcoded path /usr/local/opt/openldap/lib/libldap-2.4.2.dylib.
Understanding the Root Cause
The error message provides the smoking gun: the PHP binary was compiled with a static or dynamic reference to a specific version of libldap (version 2.4.2). However, your system either lacks this library entirely, has a different version installed in a non-standard location, or the library exists but is not accessible via the expected symlink path.
In many Homebrew setups, libldap is provided by the openldap formula. A common mistake is trying to link libldap directly using brew link libldap, only to realize that libldap is not a standalone package in Homebrew; it is an internal component of openldap. You must ensure that the openldap keg is correctly linked and that its library files are visible in your dynamic linker search path.
Diagnosing the Environment
Before attempting aggressive reinstallations, verify what actually exists on your system. Run the following command to check if the specific version mentioned in the error is present:
ls -la /usr/local/opt/openldap/lib/libldap*.dylib
If this path returns nothing or a different file (e.g., libldap-2.4.4.dylib), you have a version mismatch. The PHP binary was likely built against 2.4.2, but your system has 2.4.4 or no library at all.
You can also inspect the actual libraries being loaded by running:
otool -L /usr/local/bin/php
This command lists all dynamic dependencies for the PHP binary. Look closely at the entry for libldap. If it shows (undefined) in the path, the library is missing entirely. If it points to a different location than /usr/local/opt/openldap/lib/, your system's loader configuration might be pointing to a stale or incorrect symlink.
Resolving the Linking Issue
The most robust solution involves ensuring that openldap is correctly installed and linked, then forcing PHP to reload its libraries.
First, ensure openldap is installed:
brew install openldap
Next, check if it is currently linked:
brew info openldap
If the output indicates that the formula is unlinked or partially linked, run:
brew link --overwrite openldap
The --overwrite flag is crucial here. It forces Homebrew to recreate the symlinks in /usr/local/opt/openldap/, ensuring that libldap.dylib points to the correct architecture (x86_64) and version expected by the PHP binary. Without this, you might have a "mixed" environment where 32-bit or 64-bit binaries conflict, causing the loader to fail.
After linking openldap, attempt to reinstall PHP to ensure it picks up the new library state:
brew reinstall php@7.1
If the installation fails again with the same error, try running the post-install step manually as suggested by the Homebrew output:
brew postinstall php@7.1
This command triggers the specific steps that set up the library paths after the binary is copied to your system.
Alternative Approaches
If you cannot upgrade openldap due to strict version requirements imposed by your PHP build, you might consider using a different LDAP implementation that ships with a compatible version of the library. However, this is rarely necessary for standard development environments.
In some cases, the issue persists because the PHP binary was compiled against an older system cache. In such scenarios, rebuilding PHP from source with explicit dependencies on your current openldap version is the definitive fix, though significantly more involved than using Homebrew bottles.
When to Reach Out for Support
If you have verified that openldap is installed and linked, yet the error persists after reinstalling PHP, it is possible that the PHP bottle (pre-compiled binary) is fundamentally incompatible with your current macOS version or the specific release of openldap. In these rare cases, switching to a different PHP formula (such as php72 or later) might bypass the issue entirely, provided your application supports it.
For production environments where upgrading PHP versions is not an option, consider reaching out to the maintainers at https://laravelcompany.com for guidance on maintaining legacy stacks with modern dependencies, as they often have experience with similar ecosystem conflicts.