63

I have been using php with mamp on mac for a year even with old versions of MacOS, since I installed MacOS Monterrey if I type php on the terminal I get a message:zsh: command not found: php

Using older versions of MacOS I have never had this problem.

How can I solve the problem?

leymannx
  • 5,138
  • 5
  • 45
  • 48
Alex
  • 761
  • 1
  • 4
  • 8

8 Answers8

108

When I update MacOS Monterey, PHP was remove. I found this article it useful and solve this problem for me. https://wpbeaches.com/updating-to-php-versions-7-4-and-8-on-macos-12-monterey

Add the PHP formulae

brew tap shivammathur/php

Choose the PHP version – this example uses 7.4

brew install shivammathur/php/php@7.4

Link the PHP Version

brew link --overwrite --force php@7.4

Restart the Terminal

php -v
PTucky Eagle
  • 1,121
  • 1
  • 6
  • 3
74

I had the same issue after updating to Monterry. After some googling, I find out MacOS doesn't include PHP. You need Homebrew to install PHP again.

brew install php

https://daily-dev-tips.com/posts/installing-php-on-your-mac/

MetalBearSolid
  • 902
  • 8
  • 8
  • 1
    I cannot install brew following documentation... Is there anything specific to do? – Alex Nov 03 '21 at 11:07
  • 2
    Did you enter this into your terminal? `/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"` https://brew.sh/ – MetalBearSolid Nov 04 '21 at 15:11
  • Finally it's work, really thank you! – Alex Nov 06 '21 at 21:04
  • 3
    nobody mentioned that installing both homebrew and brew are quite slow tasks, and both will download and install a lot of big files! "This will take a while!!" :D – j.c Nov 29 '21 at 11:11
16

Homebrew users:

This can happen simply because your php version is not linked.

Goto /usr/local/Cellar, list out the content and see what versions of php you have installed. You should see directories and symlinks as so:

lrwxr-xr-x    1 kazajhodo  admin      7 May 22  2019 php72 -> php@7.2
lrwxr-xr-x    1 kazajhodo  admin      7 Feb 26  2020 php73 -> php@7.3
lrwxr-xr-x    1 kazajhodo  admin      7 Jun 12  2020 php74 -> php@7.4
lrwxr-xr-x    1 kazajhodo  admin      7 Jan 22  2021 php80 -> php@8.0
lrwxr-xr-x    1 kazajhodo  admin      7 Jan  4 11:58 php81 -> php@8.1
drwxr-xr-x    3 kazajhodo  staff     96 Jan  4 11:34 php@7.2
drwxr-xr-x    3 kazajhodo  staff     96 Aug  8 19:32 php@7.3
drwxr-xr-x    4 kazajhodo  staff    128 Jan  4 11:55 php@7.4
lrwxr-xr-x    1 kazajhodo  admin     18 Jan 22  2021 php@8.0 -> /usr/local/bin/php
lrwxr-xr-x    1 kazajhodo  admin     18 Jan  4 11:58 php@8.1 -> /usr/local/bin/php

If the version you want is there, you can link it with brew link php@7.4.

Then your bash should have php defined.

Installing php again will also work, because it will add the symlink in the install process; but you only likely actually need the symlink.

Adam Cornell
  • 161
  • 1
  • 3
  • This is the one. I downloaded php@7.4 via brew without using a php formulae (see other responses). Using this I was able to locate and create a symlink. After that, 'php -v' works perfectly. – leonelaguzman Sep 08 '22 at 16:49
  • Works! Thank you. I used this command after downloading PHP view homebrew – E Allison Nov 08 '22 at 12:14
  • I installed homebrew but don't have a /Celler folder in /usr/local – Drewdavid Apr 11 '23 at 15:54
9

I have same problem and this is the solution that I use for this.

  1. install Xcode using your terminal

     xcode-select --install
    
  2. install homebrew using your terminal

     /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    

    when it finish the process you need to add the homebrew in your path and for this put this code

     echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/**xxxxxxx**/.zprofile
     eval "$(/opt/homebrew/bin/brew shellenv)"
    

    xxxxxxx write your user name

    now you can check if homebrew is run correctly, use the next code.

     brew --version 
    

    you can see in the terminal the version that you have install in your pc

  3. now is time for install php for this

     brew install php
    

    finally, you can try see the version of php installed

     php --version
    
Dharman
  • 30,962
  • 25
  • 85
  • 135
7

If you want the latest PHP release in macOS Monterey then follow these steps:

1: brew tap shivammathur/php

2: brew install shivammathur/php/php@8.2

3: brew link --overwrite --force php@8.2

to check if its working, write this in the terminal: php -v

if you get something along the lines of PHP 8.2.0-dev (cli) then its working

3

Just using brew install php will install the latest php in mac. You can then enable php in Apache by adding the following to httpd.conf and restart Apache:

LoadModule php_module/usr/local/opt/php/lib/httpd/modules/libphp.so

1

You have to edit the file .zshrc

vim ~/.zshrc

and Then you update the path for your php

type on "?" search "php" ... type enter, it will show you the line where php is. If not you have to add the path of your PHP.

You use Mamp, then it should on the root of mamp. set it on your .zshrc

This my .zshrc related to php :

#export PATH="/usr/local/opt/php@7.4/bin:$PATH"
#export PATH="/usr/local/opt/php@7.4/sbin:$PATH"
#export PATH="/usr/local/sbin:$PATH"

export PATH="/usr/local/opt/php@8.1/bin:$PATH"
export PATH="/usr/local/opt/php@8.1/sbin:$PATH"
export PATH="/usr/local/sbin:$PATH"
Kam Dane
  • 50
  • 1
  • 9
-1

You probably need to fix it in the .bashrc file.

Do this:

Open the terminal and run this command:

nano ~/.bashrc 

Then add this line in the .bashrc file:

export PATH=$PATH:/usr/share/php/bin

Save and exit (ctrl + x)

Nabeel Khan
  • 3,715
  • 2
  • 24
  • 37