AbstractBrain Answers About us

php: command not found on MacOS and Homebrew

Question

I have installed PHP on MacOS with Homebrew using this command:

$ brew install php

However if I run php from command line (e.g. php --version) I get this error:

-bash: php: command not found

How can I fix this?

Answer

You need to use this command:

$ brew link php

However on some MacOS versions you can get this error when you try to link php:

Linking /usr/local/Cellar/php/8.1.13... 
Error: Could not symlink sbin/php-fpm
/usr/local/sbin is not writable.

That directory doesn’t even exist on some MacOS versions.

As a workaround you can call the PHP executable directly from /usr/local/Cellar where it is installed (using the full path to the executable).

For example:

/usr/local/Cellar/php/8.1.13/bin/php myscript.php