How to Install PHP on Mac: Simple Step-by-Step Guide
To install
PHP on a Mac, the easiest way is to use Homebrew by running brew install php in the Terminal. Alternatively, macOS comes with a pre-installed PHP version you can enable with sudo apachectl start and check using php -v.Syntax
Use the following command in the Terminal to install PHP via Homebrew:
brew install php: Installs the latest PHP version.php -v: Checks the installed PHP version.sudo apachectl start: Starts the built-in Apache server that can run PHP.
bash
brew install php php -v sudo apachectl start
Example
This example shows how to install PHP using Homebrew and verify the installation by checking the PHP version.
bash
brew install php php -v
Output
PHP 8.2.6 (cli) (built: May 4 2024 12:00:00) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.2.6, Copyright (c) Zend Technologies
Common Pitfalls
Some common mistakes when installing PHP on Mac include:
- Not having Homebrew installed before running
brew install php. - Using outdated macOS versions where the built-in PHP is deprecated or missing.
- Forgetting to update your
PATHenvironment variable if PHP is installed in a non-standard location. - Not restarting the Terminal or shell after installation, causing the system to not recognize the new PHP command.
bash
### Wrong: Trying to run PHP without Homebrew installed
php -v
### Right: Install Homebrew first
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install php
php -vQuick Reference
Summary tips for installing PHP on Mac:
- Install Homebrew if you don't have it:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" - Install PHP:
brew install php - Check PHP version:
php -v - Use built-in PHP on older macOS by enabling Apache:
sudo apachectl start - Restart Terminal after installation to update environment variables.
Key Takeaways
Use Homebrew to easily install the latest PHP version on Mac with 'brew install php'.
Check your PHP installation by running 'php -v' in the Terminal.
Ensure Homebrew is installed before trying to install PHP.
Restart your Terminal after installation to recognize the new PHP command.
Older macOS versions include PHP pre-installed but may require enabling Apache.