Install PHP on macOS

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 --version

You 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 Technologies

Step 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 ~/.zshrc

Method 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 composer

Step 2: Install Valet

# Install Valet globally via Composer
composer global require laravel/valet

# Install and configure Valet
valet install

Step 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-app

Now 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

  1. Visit MAMP.info
  2. Download MAMP & MAMP PRO (free version is sufficient)
  3. Run the installer and follow instructions

Step 2: Configure MAMP

  1. Open MAMP from Applications
  2. Configure your document root (default: /Applications/MAMP/htdocs)
  3. Start Apache and MySQL servers
  4. Test by visiting http://localhost:8888

Step 3: Switch PHP Versions

MAMP allows easy switching between PHP versions:

  1. Open MAMP application
  2. Go to Preferences → PHP
  3. Select desired PHP version from dropdown
  4. Restart servers

Method 4: Docker

For containerized development, Docker provides isolated PHP environments.

Step 1: Install Docker Desktop

  1. Download Docker Desktop for Mac
  2. Install and start Docker Desktop

Step 2: Create PHP Development Environment

  1. Create a project directory
  2. 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:
  1. Create index.php:
<?php
echo "Hello from Docker PHP on macOS!";
phpinfo();
?>
  1. Start the environment:
docker-compose up -d
  1. 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 --install

Compile 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 install

Installing Composer

Composer is the standard package manager for PHP projects.

Method 1: Homebrew

brew install composer

Method 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/composer

Method 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.1

Popular IDEs and Editors for macOS

Visual Studio Code (Recommended)

  1. Download Visual Studio Code
  2. Install extensions:
    • PHP Intelephense
    • PHP Debug
    • PHP DocBlocker
    • Prettier

PhpStorm

  1. Download PhpStorm
  2. Professional PHP IDE with excellent macOS integration
  3. Free trial available

Sublime Text

  1. Download Sublime Text
  2. 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:

  1. Install Apache via Homebrew:
brew install httpd
  1. 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>
  1. Restart Apache:
sudo brew services restart httpd

Managing 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.0

Using 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 phpunit7

Testing 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.php

Run 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/root

2. 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:8080

4. 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=mysqli

Development 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.local

2. 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=sqlite3

External Resources


Related Tutorials:

Last updated on