Install / Update a new php version (7.4.12) on MAC Mojave using Homebrew
201116-17
A. Check/inspect preinstalled php version on MAC
The macOS Mojave comes with a pre-installed php version.
You can find information about the preinstalled php version for [macOS Mojave 10.14.5 (18F132)]:
Check php version using the commands:
$ php -v $ which php
➜ ~ ➜ ~ php -v PHP 7.1.23 (cli) (built: Feb 22 2019 22:19:32) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies ➜ ~ ➜ ~ which php /usr/bin/php ➜ ~
You can also check about the php.ini file:
$ php -i | grep "php.ini"
➜ ~ php -i | grep "php.ini" Configuration File (php.ini) Path => /etc ➜ ~
B. Installation of a new php version via brew – Homebrew
Prerequisites and obtaining initial information
Prerequisites
- Updated XCODE version for the MAC OS X version being installed
- Xcode Command line tools
e.g.
$ xcode-select –install
NB: If you miss updating xcode tools you will be informed during the installation
e.g.
- Updated brew (Homebrew) version
e.g.
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
We are going to the Homebrew to install the newest php version.
Installation
$ brew install php@7.4
In case, you face error warnings about ‘brew link’, similar to:
Then you have to run:
$ brew link php
Running it, you might also face another issue:
Then you have to create the necessary ‘sbin‘ folder and take its ownership:
$ sudo mkdir -p /usr/local/sbin $ sudo chown -R $(whoami) /usr/local/*
➜ ~ sudo mkdir -p /usr/local/sbin ➜ ~ ls -l /usr/local total 0 drwxrwxr-x 2 zp admin 64 Nov 16 11:44 Caskroom drwxrwxr-x 73 zp admin 2336 Nov 16 10:09 Cellar drwxrwxr-x 4 zp admin 128 Nov 21 2019 Frameworks drwxrwxr-x 20 zp admin 640 Nov 16 11:44 Homebrew drwxrwxr-x 298 zp admin 9536 Nov 16 11:44 bin drwxrwxr-x 16 zp admin 512 Nov 16 10:02 etc drwxr-xr-x 10 zp wheel 320 Jan 8 2019 git drwxrwxr-x 196 zp admin 6272 Nov 16 00:16 include drwxrwxr-x 256 zp admin 8192 Nov 16 10:02 lib drwxrwxr-x 92 zp admin 2944 Nov 16 10:54 opt drwxr-xr-x 3 zp wheel 96 Jan 8 2019 remotedesktop drwxrwxr-x 2 zp admin 64 Nov 16 11:44 sbin drwxrwxr-x 29 zp admin 928 Nov 16 10:09 share drwxrwxr-x 7 zp admin 224 Nov 15 22:08 var ➜ ~ ➜ ~ sudo chown -R $(whoami) /usr/local/* ➜ ~ ➜ ~ ls -l /usr/local total 0 drwxrwxr-x 2 zp admin 64 Nov 16 11:44 Caskroom drwxrwxr-x 73 zp admin 2336 Nov 16 10:09 Cellar drwxrwxr-x 4 zp admin 128 Nov 21 2019 Frameworks drwxrwxr-x 20 zp admin 640 Nov 16 11:44 Homebrew drwxrwxr-x 298 zp admin 9536 Nov 16 11:44 bin drwxrwxr-x 16 zp admin 512 Nov 16 10:02 etc drwxr-xr-x 10 zp wheel 320 Jan 8 2019 git drwxrwxr-x 196 zp admin 6272 Nov 16 00:16 include drwxrwxr-x 256 zp admin 8192 Nov 16 10:02 lib drwxrwxr-x 92 zp admin 2944 Nov 16 10:54 opt drwxr-xr-x 3 zp wheel 96 Jan 8 2019 remotedesktop drwxrwxr-x 2 zp admin 64 Nov 16 11:44 sbin drwxrwxr-x 29 zp admin 928 Nov 16 10:09 share drwxrwxr-x 7 zp admin 224 Nov 15 22:08 var ➜ ~ ➜ ~
After that, you have to run again:
$ brew link php
In order to be sure that there are no any other issues with brew installation you can run:
$ brew doctor
Which should respond with:
➜ ~ ➜ ~ brew doctor Your system is ready to brew. ➜ ~
Finally, please note the ‘Caveats’ section, towards the end of installation messages:
Those are some important instructions for further steps to be taken, when necessary:
Caveats To enable PHP in Apache add the following to httpd.conf and restart Apache: LoadModule php7_module /usr/local/opt/php/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/ To have launchd start php now and restart at login: brew services start php Or, if you don't want/need a background service you can just run: php-fpm ==> Summary ? /usr/local/Cellar/php/7.4.12: 497 files, 72.2MB ==> Caveats ==> php To enable PHP in Apache add the following to httpd.conf and restart Apache: LoadModule php7_module /usr/local/opt/php/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/ To have launchd start php now and restart at login: brew services start php Or, if you don't want/need a background service you can just run: php-fpm ➜ ~
For instance: To enable PHP in Apache add the following to httpd.conf and restart Apache:
LoadModule php7_module /usr/local/opt/php/lib/httpd/modules/libphp7.so <FilesMatch \.php$> SetHandler application/x-httpd-php </FilesMatch>
Another step is to ensure (or update) your user profile for bash shell (or the .zshrc, which is the zsh user profile file, in case you use it) includes the /usr/local/bin, and /usr/local/sbin e.g. :
export PATH=$HOME/bin:/usr/local/bin:$PATH export PATH="/usr/local/sbin:$PATH"
As you might also notice above, the command to start/run the php in your macOS is:
$ brew services start php
Or, if you don’t want/need a background service you can just run:
$ php-fpm
Also if you want to see the running php version, you can just run:
$ php -v
➜ ~ ➜ ~ brew services start php ==> Tapping homebrew/services Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-services'... remote: Enumerating objects: 55, done. remote: Counting objects: 100% (55/55), done. remote: Compressing objects: 100% (54/54), done. remote: Total 967 (delta 25), reused 12 (delta 1), pack-reused 912 Receiving objects: 100% (967/967), 275.71 KiB | 1.14 MiB/s, done. Resolving deltas: 100% (399/399), done. Tapped 1 command (39 files, 357.7KB). ==> Successfully started `php` (label: homebrew.mxcl.php) ➜ ~ ➜ ~ ➜ ~ php -v PHP 7.4.12 (cli) (built: Oct 29 2020 18:28:50) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.12, Copyright (c), by Zend Technologies ➜ ~ ➜ ~
That’s it!
Thank you for reading!
Hi, thanks for tutorial but Im facing
“Error: Your Xcode (10.1) is too outdated.
Please update to Xcode 11.3.1 (or delete it).
Xcode can be updated from the App Store.”
On MOjave I’m unable to update to Xcode 11 – is there way to fix it?
For macOS 10.14.4, Xcode versions compatible are: Xcode 10.2 – 11.3.1.
Xcode 11.4 onwards requires macOS Catalina, 10.15.2
So, you have to install the 11.3.1 version manually, at:https://developer.apple.com/download/more/
Please note that it requires Sign-in with Apple ID. Also, note that the download Xcode_11.3.1.xid file size is currently 7.84 gigabytes (GB). You’ll need at least twice that size of free storage on your Mac to install Xcode.
And of course, you have to uninstall -before updating to 11.3.1.- the previous version. Be careful and proceed at your own risk! Good luck!
I’ve found a tool called ServBay.dev can provide much easier way for PHP developers, especially the beginners. It includes all versions of PHP, MariaDB, and PostgreSQL, as well as Redis and Memcached. Users can run multiple versions of PHP instances all at once.
Trust me, it’s made my PHP dev life a whole lot smoother. Might be worth a shot for you to check it out!