Installing PHP
Getting PHP running on your computer shouldn't be complicated, but outdated tutorials and platform differences create unnecessary confusion. This lesson walks you through installing PHP 8.3 properly on any operating system. You'll avoid common pitfalls and end up with a development environment that matches modern PHP practices.
Let's be direct about something first: many PHP tutorials recommend all-in-one packages like XAMPP or WAMP. These tools bundle Apache, MySQL, and PHP together for convenience. While they work for absolute beginners, they often create bad habits and hide important concepts you'll need to understand later.
We're taking a different approach. You'll install PHP natively on your system, understand how it actually works, and build good development practices from day one. This takes slightly more effort upfront but saves countless hours of frustration later.
Understanding PHP Installation Options
Before diving into platform-specific instructions, you need to understand what you're actually installing and why different approaches exist.
What You're Actually Installing
PHP isn't a single program but a collection of components working together:
PHP Interpreter: The core engine that reads and executes PHP code. This is what transforms your PHP scripts into output.
PHP CLI (Command Line Interface): A version of PHP that runs from your terminal or command prompt. Essential for modern development workflows, testing, and running development tools.
PHP Extensions: Additional modules that provide extra functionality like database connections, image processing, or encryption. Some extensions come bundled with PHP, others require separate installation.
Configuration Files: Settings that control how PHP behaves. The main configuration file is called php.ini
and affects everything from memory limits to error reporting.
Installation Approaches Explained
Package Managers: Tools like Homebrew (macOS), apt (Ubuntu), or Chocolatey (Windows) that handle installation and updates automatically. This is the modern, recommended approach.
Official Binaries: Pre-compiled PHP downloads from php.net. These work but require more manual configuration.
Source Compilation: Building PHP from source code. Only necessary for specialized server configurations or custom requirements.
All-in-One Packages: XAMPP, MAMP, and similar tools that bundle multiple technologies. Convenient but can create dependency on specific configurations.
We'll focus on package managers because they provide the cleanest installation experience and make updates trivial.
Installing on macOS
macOS includes an older version of PHP by default, but you shouldn't use it for development. Apple includes PHP for system tools, not for your projects. Installing a current version through Homebrew gives you control over your PHP environment.
Installing Homebrew
First, you need Homebrew, the package manager that makes installing development tools straightforward on macOS. Open Terminal and run:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
The installation process will prompt you for your password and explain what it's doing. This is normal and safe. Homebrew needs administrator privileges to install software system-wide.
After installation completes, restart your terminal or run the commands Homebrew suggests to update your PATH environment variable.
Installing PHP 8.3
With Homebrew installed, getting PHP becomes simple:
brew install [email protected]
Homebrew downloads, compiles, and configures PHP automatically. This process takes a few minutes depending on your internet connection and computer speed.
Verifying Your Installation
Check that PHP installed correctly:
php --version
You should see output similar to:
PHP 8.3.x (cli) (built: Date)
Copyright (c) The PHP Group
Zend Engine v4.3.x, Copyright (c) Zend Technologies
If you see an older version number or get a "command not found" error, your PATH environment variable might need adjustment. Run:
echo 'export PATH="/opt/homebrew/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc
Testing Your Installation
Create a simple test file to verify everything works:
<?php
echo "Hello from PHP " . PHP_VERSION . "!\n";
echo "Current date and time: " . date('Y-m-d H:i:s') . "\n";
phpinfo();
?>
Save this as test.php
and run it from your terminal:
php test.php
If you see version information and the current date, your PHP installation is working correctly.
Installing on Windows
Windows PHP installation has improved significantly in recent years, but you still need to choose between several approaches. We'll use Chocolatey, a package manager that brings Unix-style package management to Windows.
Installing Chocolatey
Open PowerShell as Administrator (right-click PowerShell and select "Run as administrator") and execute:
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
This command installs Chocolatey, which handles software installation and updates automatically.
Installing PHP 8.3
With Chocolatey ready, install PHP:
choco install php
Chocolatey downloads and configures PHP, automatically adding it to your system PATH so you can run php
from any directory.
Alternative: Manual Installation
If you prefer not using Chocolatey, download PHP directly from windows.php.net. Choose the "Non Thread Safe" version unless you specifically need thread safety. Extract the ZIP file to a directory like C:\php
and add that directory to your system PATH environment variable.
Verifying Installation
Open a new Command Prompt or PowerShell window and test:
php --version
You should see PHP 8.3 version information. If Windows can't find the php command, restart your terminal or log out and back in to refresh environment variables.
Configuration Considerations
Windows PHP installations often need minor configuration adjustments. Locate your php.ini
file by running:
php --ini
Open the configuration file in a text editor and ensure these settings are enabled:
extension=curl
extension=mbstring
extension=openssl
extension=pdo_mysql
extension=pdo_sqlite
Uncomment these lines by removing the semicolon at the beginning if they're commented out.
Installing on Linux
Linux distributions typically include PHP in their package repositories, but versions often lag behind current releases. We'll show you how to get PHP 8.3 on the most common distributions.
Ubuntu/Debian
Ubuntu's default repositories might not have PHP 8.3 immediately. Add Ondřej Surý's repository, which maintains current PHP versions for Ubuntu:
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update
Now install PHP 8.3:
sudo apt install php8.3 php8.3-cli php8.3-common
Install commonly needed extensions:
sudo apt install php8.3-curl php8.3-mbstring php8.3-mysql php8.3-xml php8.3-zip
CentOS/RHEL/Fedora
For Red Hat-based distributions, enable the Remi repository:
sudo dnf install epel-release
sudo dnf install https://rpms.remirepo.net/enterprise/remi-release-9.rpm
sudo dnf module enable php:remi-8.3
Then install PHP:
sudo dnf install php php-cli php-common php-curl php-mbstring php-mysql php-xml php-zip
Arch Linux
Arch maintains current PHP versions in the official repositories:
sudo pacman -S php php-apache
Verification
Test your installation on any Linux distribution:
php --version
Create and run the same test file shown in the macOS section to verify everything works correctly.
Understanding Your PHP Installation
Now that PHP is installed, you should understand what you actually have and how to manage it effectively.
Finding Important Files
Your PHP installation includes several important files and directories:
PHP Binary: The main PHP executable. Usually located at /usr/bin/php
on Linux/macOS or C:\php\php.exe
on Windows.
Configuration File (php.ini): Controls PHP's behavior. Find it by running php --ini
.
Extensions Directory: Contains additional PHP modules. Location varies by system but is shown in phpinfo()
output.
Include Path: Directories where PHP looks for files when you use include
or require
statements.
Essential Configuration
PHP's default configuration works for development, but you might want to adjust certain settings for a better development experience.
Open your php.ini
file and consider these changes:
; Display errors during development
display_errors = On
error_reporting = E_ALL
; Increase memory limit for development
memory_limit = 256M
; Enable useful extensions
extension=curl
extension=mbstring
extension=pdo_mysql
extension=pdo_sqlite
After making changes, restart any running PHP processes for the changes to take effect.
Testing PHP Functionality
Create a more comprehensive test to verify your PHP installation includes essential features:
<?php
echo "PHP Version: " . PHP_VERSION . "\n\n";
// Test basic functionality
echo "Basic math: " . (2 + 2) . "\n";
// Test string handling
$message = "Hello, World!";
echo "String manipulation: " . strtoupper($message) . "\n";
// Test array functionality
$fruits = ['apple', 'banana', 'orange'];
echo "Array count: " . count($fruits) . "\n";
// Test date functions
echo "Current timestamp: " . time() . "\n";
echo "Formatted date: " . date('Y-m-d H:i:s') . "\n";
// Test available extensions
echo "\nLoaded extensions:\n";
$extensions = get_loaded_extensions();
foreach (['curl', 'mbstring', 'pdo', 'json'] as $ext) {
echo $ext . ": " . (in_array($ext, $extensions) ? "✓" : "✗") . "\n";
}
?>
This script tests core PHP functionality and shows which extensions are available. All the features demonstrated should work with a proper PHP 8.3 installation.
Running PHP Code
With PHP installed, you have several ways to execute PHP code. Understanding these options helps you choose the right approach for different situations.
Command Line Execution
The php
command runs PHP scripts from your terminal:
php filename.php
This method is perfect for testing scripts, running maintenance tasks, or executing utility programs.
Interactive Mode
PHP includes an interactive mode for testing small code snippets:
php -a
This opens a PHP shell where you can type PHP code directly and see immediate results. Type exit
to quit.
Built-in Web Server
PHP 8.3 includes a built-in web server perfect for development:
php -S localhost:8000
This starts a web server on port 8000. Put PHP files in the same directory and access them at http://localhost:8000/filename.php
in your browser.
The built-in server handles static files (CSS, JavaScript, images) automatically and provides a complete development environment without additional software.
Checking Configuration
Several commands help you understand your PHP installation:
# Show PHP version and build information
php --version
# Show configuration file location
php --ini
# List all loaded extensions
php -m
# Show complete configuration (lots of output)
php --info
Troubleshooting Common Issues
Even straightforward installations sometimes encounter problems. Here are solutions to the most common issues:
"Command not found" Errors
If your system can't find the php
command, the installation didn't update your PATH environment variable correctly.
macOS/Linux: Add the PHP binary location to your shell configuration file:
echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
Windows: Add the PHP installation directory to your system PATH through System Properties > Environment Variables.
Permission Denied Errors
On Unix-like systems, you might encounter permission errors when running PHP scripts. Ensure your PHP files have appropriate permissions:
chmod +x script.php
Extension Loading Failures
If PHP can't load required extensions, check that they're properly installed and enabled in your php.ini
file. On Linux, you might need to install additional packages:
# Ubuntu/Debian example for missing extensions
sudo apt install php8.3-gd php8.3-intl php8.3-bcmath
Memory or Time Limit Issues
PHP's default limits might be too restrictive for development. Increase them in php.ini
:
memory_limit = 512M
max_execution_time = 300
Multiple PHP Versions
If you have multiple PHP versions installed, ensure you're using the correct one:
# Check which PHP version is active
which php
# On some systems, you might need to specify the version
php8.3 --version
Development Tools Worth Exploring
While not strictly necessary for learning PHP, these tools significantly improve the development experience. Consider exploring them as you become more comfortable with PHP itself.
Code Editors and IDEs
Visual Studio Code: Free, lightweight editor with excellent PHP support through extensions. The PHP Intelephense extension provides syntax highlighting, code completion, and error detection.
PhpStorm: Professional IDE specifically designed for PHP development. Includes advanced debugging, testing integration, and code analysis tools. Requires a paid license but offers student discounts.
Sublime Text: Fast, customizable editor with good PHP support through packages.
Debugging Tools
Xdebug: The standard PHP debugger that allows step-through debugging, profiling, and code coverage analysis. Installation and configuration require some effort but dramatically improve development efficiency.
PHP Debug Bar: Lightweight debugging tool that displays useful information about your PHP application's performance and behavior.
Package Management
Composer: The standard PHP dependency manager. While not needed for basic PHP learning, Composer becomes essential as you work with third-party libraries and modern PHP frameworks. We'll cover Composer extensively later in the course.
Local Development Servers
Laravel Valet (macOS): Lightweight development environment that automatically serves PHP projects from a directory.
Docker: Containerization platform that ensures consistent development environments across different machines and operating systems.
Code Quality Tools
PHP_CodeSniffer: Ensures your code follows established style guidelines and coding standards.
PHPStan: Static analysis tool that catches errors and type issues before you run your code.
PHP-CS-Fixer: Automatically fixes code style issues according to defined standards.
What's Next
You now have a working PHP 8.3 installation and understand the basic tools for running PHP code. The next lesson introduces PHP's data types - the building blocks for all PHP applications.
Don't worry about installing every development tool immediately. Start with a basic text editor and the command line. Add sophisticated tools as you encounter specific needs and gain experience with PHP development workflows.
Your PHP installation will evolve as you learn. You might add extensions, adjust configuration settings, or integrate additional tools. The foundation you've built today supports whatever direction your PHP learning takes.
Remember that installation is just the beginning. The real learning happens when you start writing and running PHP code. The projects throughout this course will give you practical experience with the PHP installation you've just completed.
← Previous Lesson: What Makes PHP Tick Next Lesson: Basic Data Types →