Install Specific PHP Version for Mac: Beyond Major Versions on macOS
When managing a Magento project, you might find yourself locked into a specific legacy release like PHP 7.1.3. The immediate hurdle is that Homebrew's php@7.1 formula defaults to the latest patch within the major.minor series (e.g., 7.1.29). Similarly, standard installation scripts often fail because they lack pre-built binaries for every single patch version. You cannot simply append .3 to a Homebrew install command or expect a generic curl installer to magically fetch 7.1.3.
To achieve this exact version on macOS, you must bypass the Homebrew package manager's default behavior and use a static binary directly from the PHP distribution site. This approach ensures compatibility with older frameworks while avoiding the overhead of compiling from source if you don't need specific system libraries.
Why Standard Tools Fall Short
Homebrew is excellent for keeping your PHP environment current, but it treats php@7.1 as a bucket for all 7.1.x releases. When you run brew install php@7.1.3, the formula lookup fails because Homebrew doesn't maintain separate formulas for every patch version; it relies on the major.minor pair.
Third-party installers, such as the one hosted at php-osx.liip.ch, are convenient but often outdated or restricted to specific frontend configurations. The error Did or could not download package: 7.1.3-frontenddev indicates that the script looked for a build tailored to your exact OS version and hardware architecture that no longer exists in its repository. Attempting to force a curl install with a non-existent package name will always result in failure.
The Static Binary Solution
The most reliable method to get PHP 7.1.3 is to download the pre-compiled static binary directly from the PHP Distribution Site. This file contains everything needed to run PHP without requiring a local compilation or specific system dependencies like OpenSSL or cURL being present at that moment.
Navigate to the distribution archive and locate the tarball matching your architecture. For most modern Macs, this is an Intel-based machine (x86_64) running macOS. You are looking for a file named php-7.1.3-macos-x86_64.tar.gz. If you have migrated to Apple Silicon (M1/M2/M3), you would need the arm64 variant, though support for such old PHP versions on ARM is often limited or non-existent in official builds.
Download the archive and extract it into a dedicated directory outside your project root:
curl -L https://www.php.net/distributions/php-7.1.3-macos-x86_64.tar.gz | tar xz -C /usr/local/bin/
# Note: Paths may vary based on macOS version and architecture
Alternatively, you can extract it to a local folder like ~/php-env and manage your path manually. This prevents conflicts with the system PHP or any Homebrew installations of newer versions.
Configuration and Usage
Once extracted, verify the version immediately:
/usr/local/bin/php -v
You should see output confirming PHP 7.1.3. At this stage, you must ensure your shell environment points to this binary first. Add an alias to your ~/.zshrc or ~/.bash_profile:
alias php='/usr/local/bin/php'
alias phpbrew='brew' # Keep brew available for other tasks if needed
For Magento specifically, you may also need to configure the OPcache. The static binary includes this by default, but ensure your .htaccess or php.ini settings align with the requirements of version 7.1.3. Older versions of Magento have strict dependencies on specific PHP extensions that might not be included in newer Homebrew formulas.
Trade-offs and Considerations
Using a static binary isolates your project from system-wide updates. While this guarantees stability for your legacy application, it means you lose the convenience of brew update managing your PHP stack. You will need to manually handle security patches or switch back to a newer version if Magento 2 upgrades require a minimum PHP 7.4.
This method is ideal for short-term fixes or specific environments where upgrading the entire OS PHP version is not feasible. However, always check the official PHP documentation regarding memory limits and extension loading before deploying to production.