I've got a brand new Mac yesterday and noted that it states PHP will be removed from future Mac OS versions by default. Show
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:
However, don't be scared. It's pretty easy to install PHP and even install multiple versions if you like. Installing HomebrewWhen 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:
Installing PHP with Homebrew on MacTo install PHP, we can run the following command:
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.
Installing PHP 7.4 with HomebrewIn 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.
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 To fix this, we need to link the correct PHP version. Switching PHP Versions with Homebrew on MacNow that we installed versions, we can easily switch between them using the First, check which version of PHP is currently running:
Then we can unlink that version by using:
The next step is to link the version we want:
It will tell you to run a script to add the path:
This will make sure the right PHP version is loaded,
now if you run
And there we go. We switched to the PHP version. Php -v is still showing the wrong versionI had the issue when upgrading from 7.4 to 8.0 for my demo that I kept seeing 7.4 when running
Remove the line that points to your old instance of PHP.
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 remove --force --ignore-dependencies httpd Next re-install apache and PHP: brew install httpd php72 It will take some time for PHP to compile. Next it’s time to check your Apache is running under your user now, but if you need it to bind to port Create a PHP file containing Running Apache as a service To start apache automatically during boot, we will need to install apache servic: apachectl stop You’ll see 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 install php72-xdebug php72-imagick If you run into any problems, execute 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.
|