PHP: Setting up PHP With Brew on Mac

An abstract conceptual representation of setting up PHP using Brew on a Mac operating system. The image could contain a Mac computer without any logo or brand name, framed in an environment that's warm and inviting like a well-lit workspace. The illustration might also include a depiction of the Brew package manager, visually interpreted as a bubbling pot or a brewer's barrel. Two unbranded bifold squares, symbolizing PHP, are installed into the computer via a series of arrows, symbolizing the installation process. The entire composition is devoid of any human figures, text, and brand names or logos.

Introduction to Installing PHP with Brew on Mac

Setting up PHP on your Mac with Brew can be a straightforward process, ensuring that you have a reliable and flexible development environment.

Essentially, we want to install Homebrew first, and then use it to install PHP.

TLDR: Quick Guide to PHP Installation via Homebrew

To set up PHP using Brew, you need to have Homebrew installed, update it, and then use the brew install command to add PHP.


/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew update
brew install php

What You Need Before Starting

Your Mac should be running macOS High Sierra or later for the most compatible experience.

Ensure command-line tools for Xcode are installed for necessary compilers.

Step by Step Installation Process

First, you need to install Homebrew, the package manager for macOS.

Open the Terminal app and paste the installation command.

After installing Homebrew, run the update command to get the latest package configurations.

Next, install PHP using Homebrew with the brew install php command.

You might also want to tap into additional repositories for more PHP extensions.


brew tap shivammathur/php
brew install shivammathur/php/php@7.4

Validation and Environment Configuration

Verify your PHP installation by checking the version using php -v.

Configure your path to prioritize the Homebrew PHP installation over the macOS default.

Edit your shell configuration file, like .bash_profile or .zshrc, adding the brew PHP path.


echo 'export PATH="/usr/local/opt/php/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/usr/local/opt/php/sbin:$PATH"' >> ~/.zshrc
source ~/.zshrc

Switching Between PHP Versions

Homebrew makes it easy to switch between different PHP versions.

For switching, just link the wanted version with brew link php@version.

For instance, swapping to PHP 7.4 would require unlinking the current version and linking PHP 7.4.


brew unlink php
brew link php@7.4

Installing Extensions and Composer

Homebrew allows the installation of PHP extensions easily.

Use brew install for needed extensions, such as Xdebug or Imagick.

Installing Composer, the PHP dependency manager, is also simple with Homebrew.


brew install composer

Common Issues and Troubleshooting

One common issue involves linking errors when trying to switch PHP versions.

Always ensure you have properly unlinked the previous version before linking a new one.

If encounter permissions issues, Homebrews doctor command can diagnose the problems.

Another potential problem is conflicts with pre-existing system PHP; ensure your path is properly configured.


brew doctor
brew link --overwrite php@7.4

Frequently Asked Questions

How do I uninstall a PHP version from Brew?

To uninstall PHP from Brew, use the following command brew uninstall php@version.

What should I do if Brew does not have the PHP version I need?

You can tap third-party repositories or compile PHP manually if Brews official sources lack a specific version.

How do I update PHP with Homebrew?

Use brew upgrade php to update to the latest PHP version available in Homebrew.

Is Homebrew PHP installation different from the native macOS PHP?

Yes, Homebrew PHP is managed separately and often newer than macOS’s bundled PHP.

Can I run multiple PHP versions at the same time with Homebrew?

You can install multiple versions but can only link one to be the default in the command line.

Managing PHP Extensions Through Homebrew

Adding functionality with PHP extensions is straightforward with Homebrew.

Search for available extensions using brew search php-extension-name.

Installing extensions works similar to installing PHP itself.

For example, to install the PHP 8.0 version of Xdebug:


brew install php@8.0-xdebug

Remember to restart your web server to apply changes after any installation.

Resolving Conflicts with the Default macOS PHP Installation

Mac users may face conflicts with the system’s pre-installed PHP.

Use which php to determine the PHP version your system is using.

To use Homebrews PHP, update your $PATH as shown earlier.

Ensure Homebrew’s PHP is loaded first to avoid conflicts.


export PATH="/usr/local/opt/php/bin:$PATH"

Edit your shell configuration file appropriately with the export command.

Securing Your PHP Installation

Securing your PHP installation is critical to safe development and deployment.

Regularly check for updates using brew update and brew upgrade php.

Review your php.ini file for any insecure settings and adjust as necessary.

Disable functions that you do not require in your development or production environment.


echo 'disable_functions = exec,passthru,shell_exec,system' >> /usr/local/etc/php/php.ini

Restart your web server to enforce new security settings.

Using Composer with Homebrew-installed PHP

Composer works seamlessly with your Homebrew-installed PHP.

After installation, run composer to manage dependencies within your PHP projects.

It’s advisable to update Composer regularly with composer self-update.

Setting Up a Local PHP Development Environment

Combine PHP with a web server such as Apache or Nginx for a full development setup.

Install services using Homebrew, e.g., brew install nginx or brew install httpd for Apache.

Configure the web server to use the Homebrew-installed PHP as the processor for PHP files.

An example configuration line for Nginx would involve setting the fastcgi_pass directive:


fastcgi_pass 127.0.0.1:9000;

This tells Nginx to send PHP requests to port 9000 where PHP-FPM listens.

Advanced Use: Customizing php.ini for Development

For a tailored development environment, tweak settings in php.ini.

Access this file at /usr/local/etc/php/php.ini or the equivalent path for your PHP version.

Change directives like max_execution_time or memory_limit based on requirement.

For local development, error reporting should be verbose:


error_reporting = E_ALL
display_errors = On

Remember to revert these settings for production environments.

Integrating PHP with Databases

Linking PHP with databases like MySQL or PostgreSQL can be done with Homebrew.

Install databases using commands like brew install mysql or brew install postgresql.

Ensure the required PHP extensions for your database are installed, for MySQL it is pdo_mysql.


brew install php@8.0-pdo_mysql

Then, configure your PHP applications to connect to the database server you’ve installed.

Automating Tasks with PHP and Homebrew

PHP scripts can automate many tasks, with Homebrew easing the installation of necessary tools.

Use crontab or other scheduling services to run PHP scripts at intervals.

Remember, the scripts will run with the PHP version linked globally at that time.

Backing Up and Migrating Your PHP Setup

It’s wise to have a backup of your PHP configuration, especially before major changes.

Backup php.ini and any other custom configuration files.

Also, note down installed extensions and their settings for efficient replication or migration.

Frequently Asked Questions

How can I keep track of what I have installed with Homebrew?

You can list all installed packages with brew list and for PHP-specific queries, brew list | grep php.

Is it possible to install a web server like Apache or Nginx using Homebrew alongside PHP?

Yes, Homebrew can manage the installation of web servers which can then be configured to work with PHP.

Are there any security considerations I should be aware of when using Homebrew for PHP?

Always check for the latest updates and monitor for any reported vulnerabilities. Review your php.ini file and disable unused or risky PHP functions.

Can I use XAMPP or MAMP with Homebrew-installed PHP?

While you can use XAMPP or MAMP, they come with their own PHP binaries. If you wish to use Homebrew’s PHP, you might need to adjust system $PATH settings or configure these tools to point to Homebrew’s PHP.

If I encounter an error during installation, where can I get support?

Homebrew has a robust documentation and community support on GitHub and Stack Overflow where many common issues have been addressed.

Shop more on Amazon
Ian Hayes

Ian Hayes

Ian Hayes

Read more articles by Ian Hayes