Installing PHP 7 with Xdebug using Homebrew

Installing PHP 7 with Xdebug using Homebrew

I had so much fun updating and installing PHP 7 with Xdebug on my local machine that I decided to share the experience with you. To start with, I'm using a Macbook Pro, so anything I share will be for a Mac. So here we go. This will be short and sweet, but to the point and does the job.

If you don't already have it, I would just personally recommend installing and using Homebrew for Mac. It's great for many things, more than just updating your PHP. What does it do you ask? Well as it says on their site:

Homebrew installs the stuff you need that Apple didn’t. 

If you want to get Homebrew just head over to their site at and follow the easy install instructions. It's also an open source project so that makes it even more awesome. You can fork it on GitHub and contribute if you wish.

So, the point of this post. I just wanted to update from PHP 5.6.19 to PHP 7 and then I also wanted to install Xdebug because I'm beginning to learn how to use it more for debugging my code. To update to PHP 7, I simply followed the steps in this article, but I'll share the steps here:

Run the following brew commands, line by line:

  • brew update 
  • brew tap homebrew/dupes
  • brew tap homebrew/versions
  • brew tap homebrew/homebrew-php
  • brew unlink php56
  • brew install php70

For my machine I had 5.6 installed via a different method, so I didn't have to "unlink" 5.6 with brew. I only needed to carry on with the php70 install and it worked just fine. After you run all of the above commands, run php --version and you should see version 7.0.13 or whatever it is at the time you run the update. You might however, need to add the config to your .bashrc file or whatever file you use. You might also need to run source ~/.bashrc or again, whatever it is you use. You'll also find that Homebrew will output further instructions in the terminal as it updates. It will give you nearly exact steps that you'll need to follow in order to make sure you're using the right configuration. You will see something like this:

screenshot of homebrew cli output

As you can see it will show you exactly what you'll need to add to your config file in order to call PHP 7 from the terminal. It's pretty straightforward for sure and went very smoothly for me, but configurations vary, so hopefully it goes as smoothly for you as well. All I did was literally, run the above homebrew commands, and then added

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

to my .bashrc file and ran:

  • source ~/.bashrc

Since I have Homebrew installed, installing Xdebug was quick and painless as well. If you head on over to the Xdebug install docs, you'll find a nice section titled "Installation on Mac OS X via Homebrew". Run the one line command:

  • brew install php70-xdebug

It enables the Xdebug extension by default but you may need to follow a couple more of the recommendations in that section of the help doc. As always, don't run Xdebug in production because it will introduce security risks and hinder performance. 

Okay, so this wasn't that short, but I like to be thorough. Hopefully you found this to be somewhat helpful and as easy as I did.

Thanks for reading!