Skip to content
Related Articles

Related Articles

How to Install PHP on MacOS?

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 21 Dec, 2021

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 PHP@8.0 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.

 Homebrew 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.

installing home brew 2

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 ‘<username>’ to the username of your macOS account.

>> /Users/<username>/.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/php@8.0

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.

install PHP 8 using brew

And PHP 8.0 is now installed on your MacBook.

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

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

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

export LDFLAGS="-L/opt/homebrew/opt/php@8.0/lib"
export CPPFLAGS="-I/opt/homebrew/opt/php@8.0/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

Check the version of PHP installed on your Mac

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!