0

Installing PHP 8.4 (Homebrew / apt / Windows)

Beginner5 min read·php-01-002

Concept

Installing PHP 8.4 correctly sets the foundation for everything that follows. The installation method matters because it determines how you manage multiple PHP versions, apply security patches, and configure extensions. The wrong approach leads to system PHP conflicts, broken dependencies, and hours of debugging unrelated to your actual code.

PHP is distributed through three primary channels depending on your OS: Homebrew on macOS, apt/ondrej PPA on Debian/Ubuntu, and the php.net Windows installer or WSL2 on Windows. Each comes with the Zend Engine, the CLI SAPI, and a default set of bundled extensions. You will always need additional extensions (pdo_mysql, pdo_sqlite, redis, mbstring, gd, zip) for real projects.

macOS via Homebrew

Homebrew installs PHP into /opt/homebrew/opt/php@8.4/ (Apple Silicon) or /usr/local/opt/php@8.4/ (Intel). The formula includes the CLI binary, FPM binary, and most common extensions. The system PHP at /usr/bin/php is Apple's (old, frozen) version — never use it for development.

text
brew install php@8.4
brew link php@8.4 --force

Shivammathur's tap provides faster, more up-to-date builds: brew tap shivammathur/php && brew install shivammathur/php/php@8.4.

Linux via ondrej/php PPA

The official Ubuntu/Debian repositories lag behind by months. Ondrej Sury's PPA (ppa:ondrej/php) tracks official PHP releases within days. This is the de facto standard for PHP on Ubuntu:

text
add-apt-repository ppa:ondrej/php
apt-get update
apt-get install php8.4 php8.4-cli php8.4-fpm php8.4-mysql php8.4-mbstring php8.4-xml php8.4-curl php8.4-zip

Each extension is a separate package prefixed with php8.4-. The php.ini files live in /etc/php/8.4/.

Native PHP on Windows works but is painful — missing extensions, path issues, performance on the filesystem. The recommended path is WSL2 with Ubuntu, then follow the Linux apt instructions above. This gives you a proper Linux environment with full PHP compatibility. The native Windows installer is available at windows.php.net if WSL2 is not an option.

Verifying your installation

After installation, always verify: php -v should show PHP 8.4.x. Run php -m to list loaded extensions. Run php --ini to find the active php.ini path — critical for configuration changes.

Platformphp.ini locationExtensions dir
macOS Homebrew/opt/homebrew/etc/php/8.4/php.ini/opt/homebrew/lib/php/pecl/
Ubuntu/Debian/etc/php/8.4/cli/php.ini/usr/lib/php/20240924/
WindowsC:\php\php.iniC:\php\ext\

Code Example

php
<?php
declare(strict_types=1);

// Verify your PHP environment programmatically
// Run this as: php verify-setup.php

echo "PHP Version: " . PHP_VERSION . "\n";
echo "PHP Major:   " . PHP_MAJOR_VERSION . "\n";
echo "PHP Minor:   " . PHP_MINOR_VERSION . "\n";
echo "PHP OS:      " . PHP_OS . "\n";
echo "PHP SAPI:    " . PHP_SAPI . "\n";
echo "php.ini:     " . php_ini_loaded_file() . "\n";
echo "\n";

// Check required extensions for a typical Laravel project
$required = [
    'pdo', 'pdo_mysql', 'pdo_sqlite', 'mbstring',
    'xml', 'curl', 'zip', 'bcmath', 'json', 'openssl',
    'tokenizer', 'fileinfo', 'ctype',
];

$missing = [];
foreach ($required as $ext) {
    if (!extension_loaded($ext)) {
        $missing[] = $ext;
    }
}

if (empty($missing)) {
    echo "All required extensions are loaded.\n";
} else {
    echo "Missing extensions: " . implode(', ', $missing) . "\n";
    echo "Install with: sudo apt-get install php8.4-{mbstring,xml,curl,zip,bcmath}\n";
}

// OPcache status
if (function_exists('opcache_get_status')) {
    $status = opcache_get_status(false);
    echo "\nOPcache enabled: " . ($status['opcache_enabled'] ? 'yes' : 'no') . "\n";
} else {
    echo "\nOPcache: not available (normal for CLI without -d opcache.enable_cli=1)\n";
}

Interview Q&A

Q: Why would you use the ondrej/php PPA instead of the default Ubuntu repository?

The default Ubuntu repositories ship PHP versions tied to the Ubuntu LTS release cycle, meaning Ubuntu 22.04 ships PHP 8.1 and will not update to PHP 8.4 without a dist-upgrade. Ondrej Sury's PPA tracks the official PHP release schedule and provides packages within days of a new release. This lets you install multiple PHP versions side by side (php7.4, php8.1, php8.4 all on the same server) and switch between them using update-alternatives. For any serious PHP development or hosting, the PPA is the only practical choice on Ubuntu/Debian.


Q: What is the difference between php8.4-cli and php8.4-fpm packages on Linux?

php8.4-cli installs the command-line binary used for scripts, artisan commands, queue workers, and interactive sessions. php8.4-fpm installs PHP-FPM (FastCGI Process Manager), which runs as a daemon and handles requests from web servers like Nginx or Caddy over a Unix socket or TCP port. You need both on a web server: FPM for serving HTTP requests and CLI for running maintenance commands. They each load their own php.ini (at /etc/php/8.4/cli/php.ini and /etc/php/8.4/fpm/php.ini), so settings like memory_limit and max_execution_time are configured independently.


Q: Why is the system PHP on macOS (/usr/bin/php) dangerous to use for development?

Apple ships a frozen version of PHP with macOS (often 8.1 or older) for internal system tooling. It is not updated with security patches through Homebrew, may be removed in future macOS versions, and installing extensions into it requires disabling SIP (System Integrity Protection). Any Homebrew packages that depend on PHP will conflict with it. Using Apple's system PHP in production scripts is a security risk. Always install PHP via Homebrew or another package manager and ensure which php points to your managed installation, not /usr/bin/php.