Install PHP (with extensions) on MacOS using Homebrew


This article has detailed steps on doing clean install of PHP 7.2 (latest PHP version as of August 2018). If you already have PHP installed via homebrew, then it requires you to first remove those PHP installations and then do the fresh PHP install. Reason of doing so, is to avoid conflicts that arise because of Homebrew March 2018 updates. Further details within blog!

Problem Background

Recently, I wanted to include a new module (visualception) in my test automation project that is built on top of codeception. In order to successfully make it work, there was a requirement to have php-imagick extension already installed in your system.

Initial search showed that, in past you could have installed php-imagick extension using command like brew install php71-imagick, but this does not work any more of Homebrew updates. With the migration to Homebrew-core, the php formula has stopped taking over the role of PECL. So now for installation of php extensions (like imagick or x-debug) you have to use PECL.

Due to older php installations, I was getting lot of issues while trying to install php-imagick extension. Ultimately, I was able to resolve all the problems by installing PHP from scratch. In below, I have listed all the steps for fresh PHP installation.

1. Prerequisite Steps For First Time PHP-Installation

In order to install PHP via Homebrew, we first need to install xcode-command-line tools and Homebrew. If these tools are already setup in your machine, then skip to next section.

1.1 First install xcode-command-line tools by running below command in terminal. If any popup message appears during installation, that needs to be confirmed. For further details on xcode tools, you may check this link.

1.2 As we are going to do all PHP related installations through macOS package manager Homebrew. So next we need to set it up by running below command:

Installation process will will show several prompts and may also ask for mac password. This may take a few minutes. Once it is done, you can verify the installation was successful by running below commands:


If you already have any PHP-related installations (php71 or older) done by homebrew then it is best to remove and clean all those installations. For me, they gave lot of conflicts and errors. Removing all of existing PHP-related installations and installing fresh was much more simpler. In case you donot have any existing php-homebrew installations OR want to keep them, then skip to next section.

2.1 Removing all PHP-Homebrew related installations

First run below brew commands to make sure brew is updated and has no configuration issues.

If brew doctor or cleanup throws any errors then fix them as per instructions returned by brew-doctor or ask google 🙂

Now onto removing existing PHP-related installations (that were done by homebrew).

After above, again run brew cleanup and brew doctor to ensure everything working fine.


3.1 Run PHP Installation commands

Once done with above, now verify that you are running the correct homebrewPHP by running type php command. If it returns  usr/local/...anything.../php then it means you are  running homebrew-PHP. If it returns something like  /usr/bin/php then it means you are running apple-supplied-PHP. In that case you need to fix paths, as we want to use homebrew-PHP! Finally run brew doctor to make sure everything is fine and no configuration errors are thrown.

3.2 Switching between PHP versions

If you have multiple php versions and want to switch between them, then you can do so by using brew link and brew unlink commands.

3.3 Install PHP extensions

As mentioned in start, php extensions such as php-imagick or php-xdebug which were previously installed by commands like ( brew install php53-imagick) now should be installed by PECL (for details on PECL check this link). Below are example commands for installation of xdebug and imagick extension.

Now you may run php -v to verify php version and php -m to check enabled extensions.

Arif Masood

Software Quality Professional. Perfectionist. Love to explore new tools, technologlies & processes. Worked on several web & mobile projects in last 7+ years. Mostly in areas of Functional, API, Automation & Performance testing. Ocassional blogger - trying to be more regular one :)



Show Buttons
Hide Buttons