6

I'm using Mac High Sierra. I installed PHP (with GMP) using

brew install php72-gmp

Everything installed successfully, per the messages given, but it would seem I still have an old version of PHP 7.1 somewhere on my machine because when I run

localhost:php-7.2.1 davea$ which php
/usr/bin/php
localhost:php-7.2.1 davea$ /usr/bin/php -v
PHP 7.1.7 (cli) (built: Jul 15 2017 18:08:09) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies

So I'd like to figure out where to put the homebrew version of PHP ahead in my PATH so that that version gets picked up first.

Dave
  • 15,639
  • 133
  • 442
  • 830

3 Answers3

18

EDIT:

As of 2018 and the latest updates to Brew your php formula names have the following format php@7.2.

When you run brew info php@7.2 your path should be /usr/local/Cellar/php@7.2/

And there will be a symbolic link to /usr/local/bin/opt/php@7.2/

So in order to change your php installation you just need to run this in terminal:

echo 'export PATH="/usr/local/opt/php@7.2/bin:$PATH"' >> ~/.bash_profile
echo 'export PATH="/usr/local/opt/php@7.2/sbin:$PATH"' >> ~/.bash_profile

or put it manually in your .bash_profile like this:

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

===========================================================================

The location of your Homebrew installed php should be /usr/local/Cellar/php72

These are the steps you need to do to setup your PHP on macOS:

After you install php with brew run this in terminal:

export PATH="$(brew --prefix homebrew/php/php72)/bin:$PATH"

Then run:

source ~/.bash_profile

To check your current active version of php run this in terminal:

which php

EDIT:

run brew info php72 in console to get all info, hints and caveats for php, it is really useful, for example this comes from brew info:

✩✩✩✩ PHP CLI ✩✩✩✩

If you wish to swap the PHP you use on the command line, you should add the 
following to ~/.bashrc, ~/.zshrc, ~/.profile or your shell's equivalent 
configuration file:
export PATH="$(brew --prefix homebrew/php/php70)/bin:$PATH"
Sasa Blagojevic
  • 2,110
  • 17
  • 22
1

As you are familiar with linux, homebrew has its own /opt and /etc path

# > echo $(brew --prefix)
# /opt/homebrew

so the paths are (for Apple Silicon):

/opt/homebrew/opt
/opt/homebrew/etc

in linux, php is installed in /etc/php/7.4

in mac homebrew, php is installed in /opt/homebrew/etc/php/7.4.

Please find the homebrew document on the default installation path for Apple Silicon 1

ref 2

tinystone
  • 369
  • 3
  • 6
0

Check if you have two php directories in '/usr/bin/' or '/usr/local/bin/'. If yes do a link to the good directory. See this link, may be it helps you.

vodasan
  • 21
  • 7
  • I have a "/usr/bin/php" executable but in /usr/local/bin I only have "/usr/local/bin/php-cgi", "/usr/local/bin/php-config," "/usr/local/bin/phpdbg," and "/usr/local/bin/phpize." – Dave Feb 02 '18 at 17:54
  • I have installed brew and « homebrew/php/php72 ». « php -v » return « 5.6 ». You have just to open a new version of terminal. Otherwise, check your PATH with echo « $PATH ». Have you « /usr/local/bin » ? Check « /usr/local/Cellar ». Have you a php72 directory ? – vodasan Feb 04 '18 at 05:54