How to install php 7.2 in mac

I've got a brand new Mac yesterday and noted that it states PHP will be removed from future Mac OS versions by default.

I'm pretty surprised they go this way. By default, it comes with PHP 7.3, and I needed 7.4 for my project so let me guide you through setting up PHP on your Mac!

Mac's warning looks like this:

WARNING: PHP is not recommended
PHP is included in macOS for compatibility with legacy software.
Future versions of macOS will not include PHP.

However, don't be scared. It's pretty easy to install PHP and even install multiple versions if you like.

Installing Homebrew

When it comes to installing software on your Mac, we need only one package manager, and it's Homebrew.

It can install any package or software you want and even install specific versions.

Read more on Homebrew - Package manager for Mac.

Quick guide: Run the following command in your terminal:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Installing PHP with Homebrew on Mac

To install PHP, we can run the following command:

brew install php

This will install the latest stable version of PHP (At the moment of writing, this is PHP 8.0).

Before running any brew commands, it's a good habit to run the following commands first. These will check if Homebrew is all up to date and running the latest versions.

brew update
brew doctor

Installing PHP 7.4 with Homebrew

In my case, I wanted to install PHP 7.4 since it's the version our server is running.

To install a specific version, we can use the @ notation.

brew install [email protected]

This will run the installer, and it should end with a success notice in your terminal.

However, even though this installed PHP, it didn't change our running instance yet.

So if we run the php -v command, we might still see a different version like PHP 7.3.14 (CLI) or whatever you have installed.

To fix this, we need to link the correct PHP version.

Switching PHP Versions with Homebrew on Mac

Now that we installed versions, we can easily switch between them using the link command.

First, check which version of PHP is currently running:

php -v

# PHP 8.0.1 (cli) (built: Jan  8 2021 01:27:28) ( NTS )
# Copyright (c) The PHP Group

Then we can unlink that version by using:

brew unlink [email protected]

The next step is to link the version we want:

brew link [email protected]

It will tell you to run a script to add the path:

echo 'export PATH="/opt/homebrew/opt/[email protected]/bin:$PATH"' >> ~/.zshrc

This will make sure the right PHP version is loaded, now if you run php -v again it should show:

# PHP 7.4.14 (cli) (built: Jan  8 2021 01:35:35) ( NTS )
# Copyright (c) The PHP Group

And there we go. We switched to the PHP version.

Php -v is still showing the wrong version

I had the issue when upgrading from 7.4 to 8.0 for my demo that I kept seeing 7.4 when running php -v. Remove the old line in your .zshrc file to fix this manually.

nano ~/.zshrc

Remove the line that points to your old instance of PHP.

export PATH="/opt/homebrew/opt/[email protected]/bin:$PATH"

Note: This is an example of my version. It might differ from what you installed before.

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Recently PHP releases with homebrew have changed some of the default settings. If you are upgrading to new PHP you may run into some problems. I did, so I wanted to share some of my work-arounds.

Sometimes built-in PHP is enough

MacOS High Sierra comes with a built-in PHP (7.1) and quite often that’s enough. Maybe you don’t even have to install anything. Run this in your project folder:

/usr/bin/php -S localhost:80 

If you want to use a built-in PHP, I recommend this guide on using built-in PHP / Apache.

For PHP 7.2 from homebrew

Quite a few things has changed in homebrew. PHP used to work with built-in apache, and some folder location shave changed. If you have changed config files or added virtual hosts, you should back them up now.

I recommend that you start by cleaning up some of the old packages from brew. You can see what versions are installed with brew list | grep php

brew remove --force --ignore-dependencies httpd
brew remove --force --ignore-dependencies php70-xdebug php71-xdebug
brew remove --force --ignore-dependencies php70-imagick php71-imagick
brew remove --ignore-dependencies --force php70 php71 php72

Next re-install apache and PHP:

brew install httpd php72
brew install php72 --with-httpd --with-thread-safety

It will take some time for PHP to compile. Next it’s time to check your httpd.conf file. Note that it has moved into new location at: /usr/local/etc/httpd/httpd.conf. It should already have corresponding line for LoadModule, but do double-check (remove LoadModule lines that attempt to load older verisons of PHP)

Apache is running under your user now, but if you need it to bind to port 80, then find and updateListen directive. You will also have to use sudo in front of apachectl start and brew services below.

Create a PHP file containing phpinfo(); to double-check your version and extensions. Open it in your browser.

Running Apache as a service

To start apache automatically during boot, we will need to install apache servic:

apachectl stop
# or if you use port 80,
# sudo apachectl stop
brew tap homebrew/services
brew services list

You’ll see httpd service listed here in a stopped state. You can register it to launch automatically.

For high ports such as 8080 or 8000:

brew services start httpd

or if you prefer to use port 80:

sudo brew services start httpd

Refresh your browser once again to check.

Re-install other modules

You might have removed some dependencies and it’s time to re-install them (use brew search php72 to see what’s available)

brew install php72-xdebug php72-imagick

If you run into any problems, execute php from command-line to see the errors.

How do I install PHP on my Mac?

Install PHP on macOS Monterey.
brew --version..
brew install php..
command -v php..
/usr/local/bin/php..
/opt/homebrew/bin/php..
#!/usr/bin/env php..
b) You can include inline scripts in expansions. A simple inline script in PHP would be: {/PHP. echo "Hello"; }.

How do I upgrade PHP to 7.4 on Mac?

6 Answers.
brew install [email protected]..
brew link --force --overwrite [email protected]..
brew services start [email protected]..
export PATH="/usr/local/opt/[email protected]/bin:$PATH".
export PATH="/usr/local/opt/[email protected]/sbin:$PATH".

How do I change PHP version on Mac?

Switch Between PHP Versions.
PHP 8.1 brew install php. Update the path: echo 'export PATH="/opt/homebrew/opt/php/bin:$PATH"' >> ~/.zshrc echo 'export PATH="/opt/homebrew/opt/php/sbin:$PATH"' >> ~/.zshrc..
PHP 8.0 brew install [email protected]. ... .
PHP 7.4 arch -arm64 brew install [email protected]..

Can you download PHP on Mac?

PHP is bundled with macOS since macOS X (10.0. 0) prior to macOS Monterey (12.0. 0). Compiling is similar to the Unix installation guide.