DEV Community

Cover image for Install PHP on Mac
Sara Ounissi
Sara Ounissi

Posted on • Updated on • Originally published at thetrendycoder.com

Install PHP on Mac

Using a Mac for the first time, I faced a problem installing PHP. I would install it over and over, and every time the terminal would tell me that I don’t have PHP on my laptop. Here are the steps I had to follow to be able to install multiple versions of PHP.

Uninstall PHP

The first step is to update brew before using it to install PHP

brew update
brew upgrade
brew cleanup
Enter fullscreen mode Exit fullscreen mode

Then you need to check the current PHP installed

brew list | grep php
Enter fullscreen mode Exit fullscreen mode

Then uninstall all the PHP that the grep return, here is an example :

brew uninstall --force php71 php71-apcu php71-opcache php71-xdebug php71-yaml
brew uninstall --force php72 php72-apcu php72-opcache php72-xdebug php72-yaml
brew cleanup
Enter fullscreen mode Exit fullscreen mode

Check again with a grep if you have anything related to PHP left :

brew list | grep php
Enter fullscreen mode Exit fullscreen mode

If it returns nothing, then you are all good. Otherwise, try to uninstall what is left.

Then we want to remove the old PHP configuration :

rm -Rf /usr/local/etc/php/*
Enter fullscreen mode Exit fullscreen mode

Once everything is clean, you can jump into the PHP installation.

Install PHP

We start by installing the tap that will

brew tap shivammathur/php
Enter fullscreen mode Exit fullscreen mode

Then you can install the different versions of PHP you want. I need versions 7.1, 7.2 and 7.3:

brew install shivammathur/php/php@7.1
brew install shivammathur/php/php@7.2
brew install shivammathur/php/php@7.3
Enter fullscreen mode Exit fullscreen mode

Now you can go in the php.ini of every version and change the options you need like the memory settings, the date.timezone etc… You can find the files of each version here:

/usr/local/etc/php/7.1/php.ini
/usr/local/etc/php/7.2/php.ini
/usr/local/etc/php/7.3/php.ini
Enter fullscreen mode Exit fullscreen mode

We have installed but not linked these PHP versions. To switch to PHP 5.6 for example, we can type:

brew unlink php && brew link --overwrite --force php@7.3
Enter fullscreen mode Exit fullscreen mode

Now you can test if you are in the correct version using :

php -v
Enter fullscreen mode Exit fullscreen mode

If you want to switch versions, use this command :

brew unlink php && brew link --overwrite --force php@7.2
Enter fullscreen mode Exit fullscreen mode

Don’t forget to check if the version correctly changed:

php -v
Enter fullscreen mode Exit fullscreen mode

Sources :

Top comments (1)

Collapse
 
leslieeeee profile image
Leslie

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.
Try it! It would never let you down.