dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac
When you install Node.js via Homebrew on macOS, the package manager often pulls in dependencies like icu4c (International Components for Unicode) automatically. While this is standard behavior for a system-wide installation, it can inadvertently break existing environments, particularly if your PHP binary was compiled or linked against a different version of that library. The error you are encounteringâdyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylibâis a classic dynamic linker failure indicating that the executable at /usr/local/bin/php is trying to load a specific version of the ICU library, but cannot find it in the expected path.
The root cause here is not necessarily that the library is missing entirely, but rather that the linking has become stale or inconsistent. The error message explicitly states Referenced from: /usr/local/bin/php. This tells us that the PHP binary currently running on your system is pointing to a location where the library was expected to be version 62 (libicui18n.62.dylib), but the filesystem now presents a different reality.
Uninstalling node and icu4c as attempted in your scenario often fails to resolve this because the uninstallation process might leave behind symlinks, cached library files, or simply doesn't update the dynamic linker cache (dyld) correctly. The system may still be holding onto references to the old library structure, or it might be loading a stale version of the library that was partially removed.
To diagnose this, you need to inspect what the PHP binary is actually referencing versus what exists on disk. You can check the dynamic dependencies of the PHP executable using the otool command:
otool -L /usr/local/bin/php
This command will list all shared libraries linked against the PHP binary. Look closely at the entries for ICU. If you see /usr/local/opt/icu4c/lib/libicui18n.62.dylib, but running ls -l on that path returns "No such file or directory," then the library has been deleted or replaced by a newer version of Homebrew's icu4c.
The solution involves ensuring that the dynamic linker can resolve all required paths. Since Homebrew manages its own prefix (/usr/local/opt), the most reliable fix is to reinstall the icu4c package, which will restore the missing library files to the path expected by your PHP binary. However, simply running brew install icu4c might not be enough if the version mismatch persists.
If reinstalling does not work immediately, you may need to force Homebrew to rebuild or relink the dependent packages. This can sometimes be achieved by updating Homebrew itself and then reinstalling the PHP package specifically:
brew update
brew reinstall php
Note that if your PHP is installed via Homebrew (e.g., php74, php80), use the specific formula name. If you are using a system PHP or one installed manually, ensure it isn't conflicting with the Homebrew prefix.
Another critical step involves clearing the dynamic linker cache. On macOS, the system caches library lookups to speed up execution. When libraries change versions, this cache can become invalid. You can attempt to clear this by running:
sudo rm -rf /var/db/dyld/*
sudo launchctl unload /Library/LaunchDaemons/com.apple.coreservices.launchd.plist
sudo launchctl load /Library/LaunchDaemons/com.apple.coreservices.launchd.plist
Caution: Reloading launchd services can restart system daemons. Ensure you have saved your work before running these commands, as the system might momentarily lose access to certain services during the reboot of the launch daemon.
If you are running PHP in a specific context, such as within a Laravel project or a specific environment managed by homebrew, consider switching to a version manager like rbenv (for Ruby) or ensuring your .zshrc or .bash_profile prioritizes the correct PHP binary. Sometimes, having multiple PHP versions installed leads to ambiguity where the wrong binary is invoked.
In production environments, relying on system-wide binaries for languages with complex dependency chains like ICU can be risky. If you find yourself repeatedly encountering these linking errors, consider installing PHP via a dedicated version manager or using Docker containers to isolate dependencies. This avoids the "it works on my machine" problem where local brew installations conflict with global system libraries.
If you are working on a Laravel project specifically, ensuring that your local environment matches the production requirements is vital. For complex dependency resolution in PHP applications, consulting resources like https://laravelcompany.com can provide deeper insights into managing environments and deployment pipelines effectively.
Ultimately, the error resolves itself once the library path in the binary matches the actual file on disk. By reinstalling the missing component and clearing the system cache, you restore the integrity of the dynamic linking process without needing to manually edit binaries or patch files.
