Setting up PHP on Mac can feel overwhelming, but don’t worry—you don’t need to be a terminal wizard to get this done. We will cover three methods to install PHP on macOS, whether you’re using an older Intel chip or the latest Apple Silicon (M1/M2/M3).
We start with Homebrew, the easiest method, then move on to manual installation for full control. Finally, we’ll explore built-in PHP (only available on older macOS versions before Monterey).
This guide includes solutions for common problems and tips on managing multiple PHP versions for your projects.
Prerequisites to Install PHP on macOS
Before installing PHP, let’s set up your Mac with an essential tool.
Install Homebrew
Setting Up Homebrew Package Manager
What is Homebrew? It’s like an “app store” for installing tools like PHP with simple commands. Let’s install it:
- Open Terminal.
- Paste this command and press Enter:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Follow the on-screen prompts (type your password if asked).
Checking brew command
To check that Homebrew works correctly, Open your Terminal and Type:
brew --version
Homebrew Version
If you see a version number, it’s ok, If not, and you are getting the ‘brew command not found’ error, you have to run below two commands to add Homebrew to your PATH:
For Apple Silicon(M1/M2/M3)
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile eval "$(/opt/homebrew/bin/brew shellenv)"
For Intel Chip:
echo 'eval "$(/usr/local/bin/brew shellenv)"' >> ~/.zprofile eval "$(/usr/local/bin/brew shellenv)"
Finally, run this command to make sure everything is correct:
brew doctor
It will help you if you need to fix something.
Method 1: Install PHP on macOS Using Homebrew
Install PHP on Mac with Homebrew
Homebrew makes installing PHP as easy as running a few commands. Let’s do this step-by-step.
Step 1: Update Homebrew and Tap PHP Versions
Updating Homebrew ensures you get the latest PHP versions and fixes. Here’s how:
- Open Terminal.
- Update brew with running:
brew update
- Tap PHP’s official repository:
brew tap shivammathur/php
This adds PHP versions to Homebrew’s menu. It’s better to run brew update
command monthly to stay updated.
Step 2: Install PHP via Homebrew
Time to install PHP! Replace php@8.3
with your preferred version (e.g., php@8.2
):
- In Terminal, run:
brew install shivammathur/php/php@8.3
- Wait 2-5 minutes (Homebrew handles everything).
Step 3: Configure PHP Path in macOS Terminal
To tell your Mac and also Compiler where to find the PHP you just installed, do this:
For Apple Silicon (M1, M2, M3 chips)
echo 'export PATH="/opt/homebrew/opt/php@8.3/bin:$PATH"' >> ~/.zshrc echo 'export PATH="/opt/homebrew/opt/php@8.3/sbin:$PATH"' >> ~/.zshrc
export LDFLAGS="-L/opt/homebrew/opt/php@8.3/lib" export CPPFLAGS="-I/opt/homebrew/opt/php@8.3/include"
For Intel-based Macs
echo 'export PATH="/usr/local/opt/php@8.3/bin:$PATH"' >> ~/.zshrc echo 'export PATH="/usr/local/opt/php@8.3/sbin:$PATH"' >> ~/.zshrc
export LDFLAGS="-L/usr/local/opt/php@8.3/lib" export CPPFLAGS="-I/usr/local/opt/php@8.3/include"
Step 4: Check PHP is Installed on your MacOS:
Restart Terminal to load new settings and then run the following command to check.
php -v
Check PHP Installation on macOS
If you see PHP version like 8.3.x, it’s correct.
The php.ini file is located in the following directory ( If you have a different PHP version, just replace 8.3 with your exact version):
/usr/local/etc/php/8.3/php.ini
Method 2: Manual PHP installtion on macOS
Install PHP on macOS Manually
Want full control? Let’s compile PHP from scratch! This method is perfect if you need a custom setup.
Downloading and Compiling PHP from Source
Here’s how:
- Download PHP Source Code (e.g., PHP 8.2.1):
- Open Terminal.
- Run like this:
curl -O https://www.php.net/distributions/php-8.2.1.tar.gz
- Extract the Files:
tar -xzvf php-8.2.1.tar.gz cd php-8.2.1
- Configure PHP:
Run this command to enable common features (like MySQL and OpenSSL):./configure --prefix=/usr/local/php \ --with-apxs2=/usr/local/apache2/bin/apxs \ --with-openssl \ --with-zlib \ --enable-mbstring \ --enable-mysqlnd \ --with-curl \ --with-mysqli \ --enable-fpm
- Compile and Install:
make -j$(sysctl -n hw.ncpu) sudo make install
Check if it worked: Type php -v
. If you see the version, it’s installed successfully.
Configuring PHP for Apache/Nginx Integration
Time to connect PHP to your web server!
For Apache:
- Open Apache’s config file:
sudo nano /etc/apache2/httpd.conf
- Look for LoadModule php_module and add:
LoadModule php_module /usr/local/php/lib/libphp.so <FilesMatch \.php$> SetHandler application/x-httpd-php </FilesMatch>
- Restart Apache:
sudo apachectl restart
For Nginx:
- Install PHP-FPM (if not done earlier):
brew install php@8.2
- Open your Nginx site config:
sudo nano /usr/local/etc/nginx/nginx.conf
- Add this inside the
server
block:location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; }
- Restart Nginx:
sudo nginx -s reload
Test PHP
To check PHP is installed on your macOS:
- Create a file:
echo "<?php phpinfo(); ?>" | sudo tee /Library/WebServer/Documents/info.php
- Visit
https://localhost/info.php
in your browser. If you see a PHP details page—it’s success!
Method 3: Using Built-in PHP on Older macOS Versions (Before macOS 12, Monterey)
Install Built-in PHP on macOS
This method only works on macOS versions before Monterey (macOS 12). Apple removed the built-in PHP in newer versions..
Enabling Default PHP and Updating Modules
- Enable Built-in PHP:
- Open Terminal and run:
sudo nano /etc/apache2/httpd.conf
- Find this line:
#LoadModule php7_module libexec/apache2/libphp7.so
- Remove the
#
to uncomment it. - Save with ⌃O → Enter → ⌃X.
- Open Terminal and run:
- Restart Apache:
sudo apachectl restart
- Verify PHP:
php -v
If you see something like PHP 7.3.X, it’s working!
Updating PHP Modules
Warning: Updating macOS’s PHP is tricky, but here’s a workaround:
- Install Missing Modules:
Install Xcode Command Line Tools
xcode-select --install
- Use
pecl
(PHP’s extension installer). For example, to add GD graphics library:sudo pecl install gd
- Follow prompts if asked.
- Link Extensions to Built-in PHP:
- For GD, Open terminal and run:
sudo nano /etc/php.ini
Find this line and uncomment and save it (remove ;):
;extension=gd
- For GD, Open terminal and run:
- Check Active Modules:
php -m
Test Your Setup:
- Create a test file:
echo "<?php echo 'Hello from macOS PHP!'; ?>" > ~/Sites/test.php
- Visit
https://localhost/test.php
in your browser. If you see the message, it works!
Managing Multiple PHP Versions on macOS
Install multiple PHP versions on macOS
Need PHP 8.2 for one project and 7.4 for another? No problem!
Here we will install and manage multiple PHP versions together on your Mac.
Switching PHP Versions with Homebrew
Homebrew makes PHP version changing easy. Here’s how:
- Install Multiple PHP Versions:
brew install shivammathur/php/php@8.2 brew install shivammathur/php/php@7.4
- Switch Versions:
brew unlink php
brew link --force --overwrite php@7.4
- Verify the Switch:
php -v
If it shows PHP 7.4.X, you’re right!
Tips: Use brew list | grep php
to see all installed PHP versions.
Automate Switching Using brew-php-switcher
Instead of manually unlinking/linking, you can install a helper tool:
brew install brew-php-switcher
Now you can switch with:
brew-php-switcher 7.4
Test It: Close and reopen Terminal. Run php -v
to confirm!
How to uninstall PHP completely from macOS?
To uninstall and remove PHP on your mac, follow the below steps:
For Homebrew PHP:
- Uninstall PHP:
brew uninstall php@8.2
- Remove leftover files:
brew cleanup
For Built-in PHP:
- Revert Apache config:
- Comment out
LoadModule php_module...
in/etc/apache2/httpd.conf
.
- Comment out
- Restart Apache:
sudo apachectl restart
Conclusion
We learned how to install PHP on Mac devices with three different ways. Also learned to install multiple PHP versions together and choose one of them while developing every project.
Now you should be able to run your PHP codes on your macOS with no problems. If you also want to install PHP on Windows, we have an article for it.