Install PHP on macOS
Installing PHP on macOS can be done in several ways. This guide covers the most common methods, from the simple Homebrew approach to more advanced Docker setups.
Method 1: Homebrew (Recommended)
Homebrew is the most popular package manager for macOS and makes installing PHP straightforward.
Step 1: Install Homebrew
If you don’t have Homebrew installed, open Terminal and run:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"Follow the on-screen instructions to complete the installation.
Step 2: Install PHP
Once Homebrew is installed, install PHP with:
# Install the latest stable PHP version
brew install php
# Or install a specific version
brew install [email protected]
brew install [email protected]Step 3: Verify Installation
Check that PHP is installed correctly:
php --versionYou should see version information like:
PHP 8.2.12 (cli) (built: Oct 27 2023 13:12:34) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.12, Copyright (c) Zend Technologies
with Zend OPcache v8.2.12, Copyright (c), by Zend TechnologiesStep 4: Configure PHP Path
Homebrew installs PHP in a specific location. Add it to your PATH:
# For bash (add to ~/.bash_profile)
echo 'export PATH="/usr/local/opt/php/bin:$PATH"' >> ~/.bash_profile
# For zsh (add to ~/.zshrc)
echo 'export PATH="/usr/local/opt/php/bin:$PATH"' >> ~/.zshrc
# Apply changes
source ~/.bash_profile # or source ~/.zshrcMethod 2: Valet (for Laravel Development)
If you’re planning to develop Laravel applications, Laravel Valet provides an excellent development environment.
Step 1: Install Dependencies
# Install Homebrew if not already installed
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Install PHP
brew install php
# Install Composer
brew install composerStep 2: Install Valet
# Install Valet globally via Composer
composer global require laravel/valet
# Install and configure Valet
valet installStep 3: Create Your First Site
# Create a directory for your sites
mkdir ~/Sites
cd ~/Sites
# Create a new Laravel project (or use existing PHP files)
laravel new my-app
# Link the directory
valet link my-appNow you can access your site at http://my-app.test.
Method 3: MAMP (All-in-One Solution)
MAMP provides a complete local server environment similar to XAMPP on Windows.
Step 1: Download and Install MAMP
- Visit MAMP.info
- Download MAMP & MAMP PRO (free version is sufficient)
- Run the installer and follow instructions
Step 2: Configure MAMP
- Open MAMP from Applications
- Configure your document root (default:
/Applications/MAMP/htdocs) - Start Apache and MySQL servers
- Test by visiting
http://localhost:8888
Step 3: Switch PHP Versions
MAMP allows easy switching between PHP versions:
- Open MAMP application
- Go to Preferences → PHP
- Select desired PHP version from dropdown
- Restart servers
Method 4: Docker
For containerized development, Docker provides isolated PHP environments.
Step 1: Install Docker Desktop
- Download Docker Desktop for Mac
- Install and start Docker Desktop
Step 2: Create PHP Development Environment
- Create a project directory
- Create
docker-compose.yml:
version: '3.8'
services:
php:
image: php:8.2-apache
ports:
- "8080:80"
volumes:
- .:/var/www/html
environment:
- APACHE_DOCUMENT_ROOT=/var/www/html
mysql:
image: mysql:8.0
ports:
- "3306:3306"
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: myapp
volumes:
- mysql_data:/var/lib/mysql
volumes:
mysql_data:- Create
index.php:
<?php
echo "Hello from Docker PHP on macOS!";
phpinfo();
?>
- Start the environment:
docker-compose up -d- Visit
http://localhost:8080
Method 5: Native PHP Installation
For advanced users who want to compile PHP from source.
Prerequisites
Install Xcode Command Line Tools:
xcode-select --installCompile from Source
# Download PHP source
curl -O https://www.php.net/distributions/php-8.2.12.tar.gz
tar -xzf php-8.2.12.tar.gz
cd php-8.2.12
# Configure and compile
./configure \
--prefix=/usr/local/php \
--with-apxs2=/usr/local/apache2/bin/apxs \
--with-mysql \
--with-pdo-mysql \
--with-openssl \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg \
--with-png \
--with-freetype \
--enable-mbstring \
--enable-opcache
make
sudo make installInstalling Composer
Composer is the standard package manager for PHP projects.
Method 1: Homebrew
brew install composerMethod 2: Official Installer
# Download and run installer
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"
# Move to global location
sudo mv composer.phar /usr/local/bin/composerMethod 3: brew-php-switcher
If using multiple PHP versions:
# Install brew-php-switcher
brew install brew-php-switcher
# Switch PHP version
brew-php-switcher 8.1Popular IDEs and Editors for macOS
Visual Studio Code (Recommended)
- Download Visual Studio Code
- Install extensions:
- PHP Intelephense
- PHP Debug
- PHP DocBlocker
- Prettier
PhpStorm
- Download PhpStorm
- Professional PHP IDE with excellent macOS integration
- Free trial available
Sublime Text
- Download Sublime Text
- Install PHP packages through Package Control
Configuring Web Server
Using Built-in PHP Server
For quick development without Apache:
# Start PHP built-in server
php -S localhost:8000
# Or with custom document root
php -S localhost:8000 -t public/Configuring Apache
If you need Apache with PHP module:
- Install Apache via Homebrew:
brew install httpd- Edit Apache configuration:
# Open Apache config
sudo nano /usr/local/etc/httpd/httpd.conf
# Add PHP handler
LoadModule php_module /usr/local/opt/php/lib/httpd/modules/libphp.so
# Add PHP file handler
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>- Restart Apache:
sudo brew services restart httpdManaging Multiple PHP Versions
Using brew-php-switcher
# Install multiple PHP versions
brew install [email protected]
brew install [email protected]
# Install brew-php-switcher
brew tap shivammathur/php
brew install brew-php-switcher
# Switch between versions
brew-php-switcher 8.1
brew-php-switcher 8.0Using Phive
Phive helps manage PHP tools and versions:
# Install Phive
curl -phar.io/phive.phar > phive.phar
chmod +x phive.phar
sudo mv phive.phar /usr/local/bin/phive
# Install tools with specific PHP versions
phive install phpunit --target phpunit7Testing Your Installation
Create Test File
Create a test file in your web root:
<?php
// test.php
echo "PHP Version: " . phpversion() . "\n";
echo "Server Software: " . $_SERVER['SERVER_SOFTWARE'] . "\n";
echo "Document Root: " . $_SERVER['DOCUMENT_ROOT'] . "\n";
// Test extensions
echo "\nLoaded Extensions:\n";
foreach (get_loaded_extensions() as $extension) {
echo "- $extension\n";
}
?>
Run from Command Line
php test.phpRun in Browser
Place file in web directory and access via http://localhost/test.php.
Common macOS-Specific Issues
1. Permission Issues
Problem: “Permission denied” errors when accessing files.
Solution:
# Fix permissions for web directory
sudo chown -R $(whoami):staff /path/to/web/root
chmod -R 755 /path/to/web/root2. macOS SIP (System Integrity Protection)
Problem: Cannot modify system files.
Solution: Use Homebrew installations instead of system modifications.
3. Port Conflicts
Problem: Port 80 or 443 already in use.
Solution:
# Find what's using the port
sudo lsof -i :80
# Kill the process
sudo kill -9 PID
# Or use different port
php -S localhost:80804. Extension Loading Issues
Problem: Extensions not loading properly.
Solution:
# Check extension directory
php -i | grep extension_dir
# Edit php.ini
nano /usr/local/etc/php/8.2/php.ini
# Add extension
extension=mysqliDevelopment Workflow Tips
1. Virtual Hosts
Set up virtual hosts for multiple projects:
# Edit hosts file
sudo nano /etc/hosts
# Add entries
127.0.0.1 myproject.local
127.0.0.1 anotherproject.local2. Automatic PHP Switching
Create alias for easy PHP version switching:
# Add to ~/.zshrc or ~/.bash_profile
alias php8.1="brew-php-switcher 8.1"
alias php8.0="brew-php-switcher 8.0"3. Development Database
Use SQLite for local development:
# Enable SQLite extension in php.ini
extension=sqlite3External Resources
Related Tutorials: