Mac无法卸载PHP 7.2求助:本地仍检测到该版本
Hey there, let's work through this PHP problem—Macs love hiding PHP instances in unexpected places, which is why you're seeing that weird mismatch between php -v and Magento's detection. Here's a step-by-step breakdown to get this sorted:
Step 1: Locate All Hidden PHP Executables
First, we need to find every copy of PHP on your system, because Magento might be picking up one that's not in your terminal's PATH. Run these commands in your terminal:
- Find all PHP files (ignoring permission errors):
Look for paths likefind / -name php -type f 2>/dev/null/usr/bin/php(system default),/usr/local/bin/php(Homebrew), or even custom install locations like/opt/homebrew/bin/php(for Apple Silicon Macs). - Check your current PATH to see which directories terminal uses to find PHP:
echo $PATH
Step 2: Clean Up Homebrew PHP Residues
If you installed PHP via Homebrew, make sure you've fully removed the old version:
- List any remaining PHP packages:
brew list | grep php - Force-uninstall PHP 7.2 (and any related packages):
brew uninstall --force php@7.2 - Clean up Homebrew's cache and leftover files:
brew cleanup
Step 3: Handle System-Supplied PHP
MacOS includes a default PHP version that's often tucked away in /usr/bin/php. Warning: Deleting system PHP can break some built-in tools, so try these safer options first:
- Hide it from your terminal and Magento by modifying your shell config:
Open~/.zshrc(for Zsh) or~/.bash_profile(for Bash) in a text editor, and add this line to prioritize other directories over the system PHP path:
Save the file and reload it withexport PATH="/usr/local/bin:/opt/homebrew/bin:$PATH"source ~/.zshrcorsource ~/.bash_profile. - If you absolutely need to disable the system PHP (use with caution), rename it instead of deleting:
sudo mv /usr/bin/php /usr/bin/php.old
Step 4: Fix Magento's PHP Detection
Magento doesn't always rely on your terminal's PATH—it might have scanned your system for PHP during initial setup. Here's how to fix that:
- Check if Magento is using a hardcoded PHP path: Look in
app/etc/env.phpfor aphp_binaryentry. If it exists, update it to your desired PHP version's path (e.g.,/usr/local/opt/php@7.0/bin/phpif you install 7.0). - When running Magento commands, explicitly call your target PHP version:
/path/to/your/php7.0 bin/magento setup:install [your-install-flags]
Step 5: Install PHP 7.0 (If You Want to Downgrade)
If you'd rather downgrade to PHP 7.0 instead of fully removing PHP, use Homebrew:
- Update Homebrew first:
brew update - Install PHP 7.0:
brew install php@7.0 - Add it to your shell PATH (edit
~/.zshrcor~/.bash_profile):export PATH="/usr/local/opt/php@7.0/bin:$PATH" export PATH="/usr/local/opt/php@7.0/sbin:$PATH" - Reload your shell config and verify the version:
source ~/.zshrc php -v
Step 6: Verify the Fix
After following these steps, double-check:
- Run
which phpin terminal—it should either point to your new PHP 7.0 or return "php not found" if you fully uninstalled. - Run the
findcommand again to make sure no stray PHP executables are left:find / -name php -type f 2>/dev/null
Just a heads-up: Be careful with sudo commands—always double-check the paths you're modifying to avoid breaking your system.
内容的提问来源于stack exchange,提问作者Stefan Muntean

