Can i install php on 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 //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

PHP [Hypertext Preprocessor] is known as a general-purpose scripting language that can be used to develop dynamic and interactive websites. It was among the first server-side languages that could be embedded into HTML, making it easier to add functionality to web pages without needing to call external files for data.

Originally PHP was developed for web development but now is also used as a general-purpose language. It was created by Rasmus Lerdorf in 1994. 

This article will help you to set up or any version on macOS.

Installing PHP on macOS :

So we start by installing HomeBrew, if you haven’t installed you can navigate to the link by clicking here.  

Step 1: Copy the installation command.

Installation command

Step 2: Next, paste the copied command in the Terminal. 

It will now ask for your ‘sudo’ password, So provide your account password. And press RETURN to continue with the installation.

After a few seconds depending upon your internet speed, you should see the output  “Installation successful!” message.

Step 3: Now, we will enter the below command to update our packages:

brew update    

In this case, we are already up-to-date.

In case if you are getting the ‘brew’ command not found error. You have to run two commands to add Homebrew to your PATH: 

echo 'eval "$[/opt/homebrew/bin/brew shellenv]"'

Note: Below, Substitute ‘’ to the username of your macOS account.

>> /Users//.zorofile eval "$[/opt/homebrew/bin/brew shellenv]"

Now you can update packages.

Step 4: Before we can install PHP, we need to install a third-party repository with a brew tap. 

Enter the following command.

brew tap shivammathur/php

Step 5: Now, we are all set and we can install PHP 8 using brew install.

brew install shivammathur/php/

Of course,  you can change the version to another one, if you are interested in a different version.

If everything worked, you should now see the selected output below.

And PHP 8.0 is now installed on your MacBook.

Step 6: You need to have first in your PATH, to do that run the two following commands:

echo 'export PATH="/opt/homebrew/opt//bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/opt/homebrew/opt//sbin:$PATH"' >> ~/.zshrc

For compilers to find php8.0 you may need to set:

export LDFLAGS="-L/opt/homebrew/opt//lib"
export CPPFLAGS="-I/opt/homebrew/opt//include"

Close and restart Terminal to load new settings.

Check the Version of PHP Installed on your MacOS:

Run the following command to check.

php -v

You can see the version of PHP installed on your MacOS.

Congratulations! You now have installed PHP with associated extensions running on your Mac development environment.

How do I know if PHP is installed on my Mac?

2 Answers.
Go to File > Preferences > User Settings > Settings.json..
Change the value of php. validate. executablePath according to the installed directory of php7. "php.validate.executablePath": "/Applications/MAMP/bin/php/php7.0.14/bin/php".
Relaunch VM Code..

How do I run PHP on Mac m1?

ini file type php --ini in your terminal..
Go to DBngin and download the installer..
Install on Mac like any other Mac app..
Run DBngin and choose to create a new server..
Choose MySQL, its version and create a server with a custom name..
Start the server..

How do I install the latest version of PHP for Mac?

To fix this I used the following steps:.
Step 1 | Tap into another repository of formulae. brew tap shivammathur/php..
Step 2 | Install the desired PHP version. brew install shivammathur/php/php@7.4. ... .
Step 3 | Link the PHP Version. brew link --overwrite --force php@7.4..
Step 4 | Restart Terminal..
Step 5 | Check PHP version. php -v..

How do I use PHP 8 on Mac?

How to install PHP 8 on macOS Big Sur using Homebrew.
Step 1: Install Homebrew. ... .
Step 2: Install PHP. ... .
Step 3: The php. ... .
Step 4: Check if PHP is running. ... .
Step 5: Checking PHP Version..

Chủ Đề