zsh: command not found: php
Published ยท Updated
zsh: command not found: php on a Mac usually means there is no PHP on the
machine at all. Apple bundled PHP up to macOS 11 Big Sur and removed it in
macOS 12 Monterey. Old tutorials that say "PHP comes with your Mac" no longer
apply, and a macOS upgrade can make a working php disappear.
Check what you have
command -v php
ls /opt/homebrew/bin/php /usr/local/bin/php 2>/dev/null
brew list --versions | grep '^php'
- Nothing at all: install PHP (next section).
- A Homebrew PHP shows up in
brew listbutcommand -v phpis empty: it is a PATH or linking problem, see below. - The same error in bash reads
-bash: php: command not found. The fix is the same; only the startup file differs (~/.bash_profile).
Install PHP with Homebrew
brew install php
rehash
php --version
Homebrew installs php into /opt/homebrew/bin on Apple silicon and
/usr/local/bin on Intel Macs. If brew itself is missing, start with
zsh: command not found: brew.
A specific PHP version: [email protected] is keg-only
brew install php gives you the newest release. Older versions come as
versioned formulas, for example [email protected]. Homebrew installs those
"keg-only": the files are on disk, but nothing is linked into
/opt/homebrew/bin, so zsh still cannot find php. You have two options:
brew install [email protected]
# Option 1: link it (unlink the current php first if you have one)
brew unlink php
brew link --force --overwrite [email protected]
# Option 2: keep it unlinked and put it first on PATH, in ~/.zprofile
export PATH="$(brew --prefix [email protected])/bin:$(brew --prefix [email protected])/sbin:$PATH"
Open a new terminal tab and check php --version. Homebrew prints the exact
lines for your machine after the install; brew info [email protected] shows them
again.
PHP is installed but zsh still cannot find it
print -l $path
If /opt/homebrew/bin (Apple silicon) or /usr/local/bin (Intel) is not in
the list, Homebrew's shell setup is not loaded. Add it to ~/.zprofile:
eval "$(/opt/homebrew/bin/brew shellenv)"
Then open a new tab. Editing the file does nothing to tabs that are already
open. How PATH works in zsh on macOS explains
which file zsh reads when, and why /usr/local/bin can win over
/opt/homebrew/bin on a Mac that was migrated from Intel.
Herd, MAMP and other PHP bundles
Laravel Herd, MAMP and similar apps ship their own PHP binaries outside
Homebrew's folders. If you use one, its php is only on PATH when the
app's own folder is on it. Herd sets this up for zsh during installation
(its folder is ~/Library/Application Support/Herd/bin), so open a new tab
after installing it. For MAMP you add the folder of the PHP version you
want, for example
/Applications/MAMP/bin/php/<version>/bin. Pick one source of PHP. Two on
PATH means php on the command line and PHP in your web server can be
different versions, which shows up later as "extension not found" errors
that make no sense.
After PHP works
composer and laravel are separate tools. If they fail next, see
zsh: command not found: composer,
which also covers Composer's global vendor/bin folder.
Sources
related.
Ruslan Osipov
About the author