The solutions above did not work for me on Big Sur I kept on getting:
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
To fix this I used the following steps:
Step 1 | Tap into another repository of formulae
brew tap shivammathur/php
Step 2 | Install the desired PHP version
brew install shivammathur/php/
Other options are:
or known as just PHP
Step 3 | Link the PHP Version
brew link --overwrite --force
Step 4 | Restart Terminal
Step 5 | Check PHP version
php -v
You should now see the new version.
PHP 7.4.25 [cli] [built: Oct 21 2021 00:29:22] [ NTS ]
Copyright [c] The PHP Group
Zend Engine v3.4.0, Copyright [c] Zend Technologies
with Zend OPcache v7.4.25, Copyright [c], by Zend Technologies
This also solved my issues with PHP syntax in Visual Studio Code.
Photo by Caspar Rubin on UnsplashThis was written in 2018. Now, all the brew taps in this blog have been deprecated.
Using Homebrew
First, make sure that your homebrew is up to date
brew update && brew upgrade
If you didn’t have homebrew, install it right away!
Using Brew Tap command allows Homebrew to tap into another repository of formula.
brew tap homebrew/dupes
These formulae were those that duplicated software provided by macOS.
brew tap homebrew/versions
These formulae provided multiple versions of existing packages or newer versions of packages that were too incompatible to go in homebrew/core.
brew tap homebrew/homebrew-php
A centralized repository for PHP-related brews.
Checking php version using the following commands
php -v
[same asphp --version
]
To unlink the last version
brew unlink php
[depends on the current version on your computer]
For instance, your current php version is 5.5brew unlink php55
To install the new version of php
brew install php
[If you need another version just change the version number]
If you need php 7.1brew install php71
then check php version again
php -v
If everything looks fine then congratulation! you’ve finished the php updated.
But If you find the following errorsError:Could not symlink file: /urs/local/Cellar/php...
you need to run the following commands
sudo chown -R$[whoami] /usr/local/*
brew list | while read f; do brew unlink f; brew link $f; done
brew unlink php
brew link --overwrite php
check php version again
php -v
linked to the old version?
So open you .profile or .bash_profile
like nano or vim
export PATH=/usr/local/php5/bin:$PATH
Source your file with source ~/.bash_profile
or source ~/.profile
[same as close and reopen your terminal].
Using one line install
curl -s //php-osx.liip.ch/install.sh | bash -s
The latest macOS versions of Big Sur and Catalina currently ship with PHP 7.3 and have a deprecation notice that they intend to drop support for PHP altogether in a future upgrade.
You can see this by running a function in a webhosted file or running
php -v
on the command line.
[email protected] Documents % php -v 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
So moving forward the best solution is to use Homebrew to install and then choose whichever version of PHP you want including 7.4 and version 8.
So once you have Homebrew installed on your macOS a particular PHP Homebrew formulae will need to be added from this maintainer, what is great about this, is all PHP versions are available whereas only some versions are available on the default Homebrew formulas.
After Homebrew install, follow the below:
Add the PHP formulae
brew tap shivammathur/php
Choose the PHP version – example uses 7.4
brew install shivammathur/php/[email protected]
Other options are
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- [email protected] or known as just php
- [email protected]
Link the PHP Version
brew link --overwrite --force [email protected]
Change your version to match
Restart the Terminal
Run…
php -v
You should now see the new version, to change to another version just repeat the process from the brew install...
then unlink and link in the new PHP version by issuing a command like below but with your correct version:
brew unlink php && brew link --overwrite --force [email protected]
Using new PHP in macOS Shipped Apache
If you are using macOS in built shipped Apache, you can use the new PHP version by editing the Apache file:
sudo nano /etc/apache2/httpd.conf
Find the PHP module and comment it out and add in the new PHP version path:
#LoadModule php7_module libexec/apache2/libphp7.so LoadModule php7_module /usr/local/opt/[email protected]/lib/httpd/modules/libphp7.so
Restart Apache
sudo apachectl restart
Now the new PHP version will be used.
You could download all the versions and just uncomment the one you want if you intend to swap around:
#LoadModule php5_module /usr/local/opt/[email protected]/lib/httpd/modules/libphp5.so #LoadModule php7_module /usr/local/opt/[email protected]/lib/httpd/modules/libphp7.so #LoadModule php7_module /usr/local/opt/[email protected]/lib/httpd/modules/libphp7.so #LoadModule php7_module /usr/local/opt/[email protected]/lib/httpd/modules/libphp7.so #LoadModule php7_module /usr/local/opt/[email protected]/lib/httpd/modules/libphp7.so LoadModule php7_module /usr/local/opt/[email protected]/lib/httpd/modules/libphp7.so #LoadModule php_module /usr/local/opt/[email protected]/lib/httpd/modules/libphp.so #LoadModule php_module /usr/local/opt/[email protected]/lib/httpd/modules/libphp.so
PHP 8 and macOS Apache
One extra step is needed for PHP 8 and macOS bundled Apache:
sudo nano /etc/apache2/httpd.conf
Add the new PHP 8 and comment out the old one.
LoadModule php_module /usr/local/opt/[email protected]/lib/httpd/modules/libphp.so
Go to the end of the file and add:
SetHandler application/x-httpd-php
Restart Apache
Removing Older Homebrew PHP version [optional]
If you have the older PHP formulas from an older Homebrew installation, you may wish to remove these:
rm -rf $[brew --cellar]/php
Remove old PHP Launch Agents and daemons, if present:
rm -f ~/Library/LaunchAgents/homebrew.mxcl.php* rm -f /Library/LaunchAgents/homebrew.mxcl.php* rm -f /Library/LaunchDaemons/homebrew.mxcl.php*
Remove the deprecated homebrew/php
tap, if present:
brew untap homebrew/php
Run brew cleanup:
brew cleanup