Call + (44) 0843 289 4539

Setting PHP7 Development on mac osx

Recently there have been a lot of changes to Homebrew in that there have been a number of packages renamed and improved. One of the packages that have been affected it PHP.Specifically renaming the php71 to php@7.1, this has caused some confusion amongst the community on how to install PHP on Mac OSX and to enable xdebug.

In this post we will walk through the process of cleaning up a current installation of PHP on Mac OSX using Homebrew and enabling xdebug

If you have previously installed PHP on your Mac OSX using Homebrew you may want to take this opportunity to clean up your installation and reconfigure your machine with the latest packages.

Clean up old installs of PHP

We will use HomeBrew to install PHP 7.2 using the new Taps available. We will go ahead and uninstall all versions of PHP to ensure we only will have PHP installed via Homebrew.

First lets examine what PHP packages have been installed

brew list | grep php

We can now uninstall any packages that have been installed

brew list | grep php | while read x; do brew uninstall --force $x; done

Ensure we remove any old casks in Homebrew

rm -rf /usr/local/Cellar/php

 

Clean up all the launch agents to ensure there is no confusion later on.

rm ~/Library/LaunchAgents/homebrew.mxcl.php*
sudo rm /Library/LaunchDaemons/homebrew.mxcl.php*

We can now ensure Homebrew is cleaned up and updated

brew untap homebrew/php
brew cleanup
brew update
brew doctor

 

Check to see if there are any daemons running. If there are then reboot

ps ax | grep php



Install PHP using Homebrew

brew install php

By default this will install PHP 7.2.x (or whatever the latest edition of PHP is when you install it)

You will now be able to write and run applications using PHP. However, if you would like to be able to debug your PHP applications there are some additional steps you need to complete to install xdebug.

What is xdebug ?

Xdebug is an extension for PHP to assist with debugging and development. It contains a single step debugger to use with IDEs; it upgrades PHP’s var_dump() function; it adds stack traces for Notices, Warnings, Errors and Exceptions; it features functionality for recording every function call and variable assignment to disk; it contains a profiler; and it provides code coverage functionality for use with PHPUnit.

Prior to moving the PHP taps in Homebrew it used to be possible to install xdebug and PHP7.2 at the same time using brew install php72-xdebug, sadly this is not the case, and from what I understand this won’t be happening anytime soon.

PECL (PHP Extension Community Library) is now the source of xdebug and all future updates will available via PECL



How to install xdebug on Mac OSX

Installing xdebug via PECL is fairly straight forward, because if you have PHP7.2 installed you should be able to update your channel list first

pecl update-channels

This will check whether you already have xdebug installed

pecl list-all

If xdebug is not install you can simply install it using

pecl install xdebug

if you want to upgrade xdebug to the latest version  simply

pecl upgrade xdebug

Configure PHP to use xdebug

We need to add the following line to php.ini:zend_extension="/usr/local/php/modules/xdebug.so".

To find out where your php.ini file is

php -i | grep php.ini  

Which should return something similar too:

Configuration File (php.ini) Path => /usr/local/etc/php/7.2
Loaded Configuration File => /usr/local/etc/php/7.2/php.ini



Configure PHP.ini

You can open the file using

sudo nano  /usr/local/etc/php/7.2/php.ini

The search for the

zend_extension="xdebug.so"

Then change it too zend_extension="/usr/local/Cellar/php/7.2.9_1/pecl/20170718/xdebug.so"

 

The additionally update the following

[xdebug]
zend_extension="/usr/local/Cellar/php/7.2.9_1/pecl/20170718/xdebug.so"
xdebug.collect_vars=1
xdebug.remote_autostart=1
xdebug.remote_enable=1
xdebug.remote_port=9005

Configure PHPStorm

We predominantly use PHPStorm – Lightning-smart PHP IDE as our default PHP Development editor.
So it is worhtwhile ensuring you have enabled xdebug in PHP storm as your debugger – Xdebug Installation Guide



Summary

You should now have a complete PHP Development Environment configured on your Macbook.

Follow Me

Gary Woodfine

Helps businesses by improving their technical proficiencies and eliminating waste from the software development pipelines.

A unique background as business owner, marketing, software development and business development ensures that he can offer the optimum business consultancy services across a wide spectrum of business challenges.
Follow Me
%d bloggers like this: