5

I've installed an updated version of PHP 7.4 using homebrew.

But still, when i run php -v on the command line, i get :

WARNING: PHP is not recommended
PHP is included in macOS for compatibility with legacy software.
Future versions of macOS will not include PHP.
PHP 7.3.24-(to be removed in future macOS) (cli) (built: Dec 21 2020 21:33:25) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.24, Copyright (c) 1998-2018 Zend Technologies

As you can see, it's still using the one that comes with Mac Os.

When i run phpinfo from a php script, i can see it's using the correct one, as i configured on Apache, as shown below.

What i need to do to make the command line use the brew installed version of PHP ?

enter image description here

delphirules
  • 6,443
  • 17
  • 59
  • 108
  • If you'd like to use a different version of `php`, then use it. Reference the path of your desired binary, or tweak your PATH to find that binary first. – Emile Pels Feb 15 '21 at 18:47
  • Try what this user suggested: https://stackoverflow.com/a/53785784/14612433 Good Luck! – Carbon Feb 16 '21 at 13:06

1 Answers1

0

After installing a package, Homebrew shows you some helpful information you have to notice that information.

Anyway, you can access that information after that, by running below command.

brew info php@7.4

If you notice there is a Caveats section like below, it contains some configurations you have to do manually.

==> Caveats
To enable PHP in Apache add the following to httpd.conf and restart Apache:
    LoadModule php7_module /usr/local/opt/php@7.4/lib/httpd/modules/libphp7.so

    <FilesMatch \.php$>
        SetHandler application/x-httpd-php
    </FilesMatch>

Finally, check DirectoryIndex includes index.php
    DirectoryIndex index.php index.html

The php.ini and php-fpm.ini file can be found in:
    /usr/local/etc/php/7.4/

php@7.4 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have php@7.4 first in your PATH, run:
  echo 'export PATH="/usr/local/opt/php@7.4/bin:$PATH"' >> ~/.zshrc
  echo 'export PATH="/usr/local/opt/php@7.4/sbin:$PATH"' >> ~/.zshrc

For compilers to find php@7.4 you may need to set:
  export LDFLAGS="-L/usr/local/opt/php@7.4/lib"
  export CPPFLAGS="-I/usr/local/opt/php@7.4/include"


To restart php@7.4 after an upgrade:
  brew services restart php@7.4
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/php@7.4/sbin/php-fpm --nodaemonize

In your case, you have to add bellow lines to your shell runcom file by runing these commands in your terminal, if you using Zsh it whould be ~/.zshrc if you are using Bash, it would be ~/.bashrc.

echo 'export PATH="/usr/local/opt/php@7.4/bin:$PATH"' >> path/to/runcom/file
echo 'export PATH="/usr/local/opt/php@7.4/sbin:$PATH"' >> path/to/runcom/file

This would add your PHP installed by Hombrew in the path, and prefer it over macOS bundled PHP.

nekofar
  • 1,497
  • 1
  • 18
  • 25