How to Install PHP
PHP (Hypertext Preprocessor) is a server-side scripting language widely used for developing dynamic web applications. In this guide, we will provide you with a
PHP (Hypertext Preprocessor) is a server-side scripting language widely used for developing dynamic web applications. Before you can run any PHP code, you need a PHP interpreter installed on your machine. This guide walks you through installing PHP on Windows, macOS, and Linux, choosing the right setup for your goal, and verifying that everything works.
If you are brand new to the language, read the PHP Introduction first to understand what PHP is and where it runs.
Which installation should I choose?
There is no single "correct" way to install PHP — the best option depends on what you want to do:
- Just learning the language? Install the PHP CLI (command-line) interpreter. PHP ships with a built-in development server, so you do not need Apache or Nginx to get started.
- Building a full web stack? Use an all-in-one bundle like XAMPP, MAMP, or Laragon. These install PHP, Apache (or Nginx), and MySQL together with one click.
- Working on a real project? Install PHP through your system's package manager (
apt,brew,choco). This is the cleanest, most updatable approach for developers.
The table below summarizes the trade-offs:
| Approach | Best for | Includes a web server? |
|---|---|---|
| PHP CLI + built-in server | Learning, small scripts | Built-in (dev only) |
| XAMPP / MAMP / Laragon | Beginners wanting Apache + MySQL | Yes |
| Package manager | Developers, production-like setups | No (install separately) |
Prerequisites
Before you begin, make sure you have:
- A computer running Windows, macOS, or Linux.
- A text editor such as Visual Studio Code, Sublime Text, or Notepad++.
- Access to a terminal (Command Prompt or PowerShell on Windows, Terminal on macOS/Linux).
- A web server (Apache or Nginx) only if you skip the built-in development server.
Installing PHP by operating system
Windows
The simplest path on Windows is the Chocolatey package manager. In an elevated PowerShell:
choco install phpPrefer a manual install? Download the latest non-thread-safe Zip from the official PHP downloads page, extract it to C:\php, and add C:\php to your Path environment variable so the php command is available everywhere.
macOS
Homebrew is the standard tool:
brew install phpHomebrew links the php binary onto your PATH automatically.
Linux (Debian/Ubuntu)
Use apt:
sudo apt update
sudo apt install php php-cliOn Fedora/RHEL the equivalent is sudo dnf install php php-cli.
Verifying the installation
Whichever method you used, confirm PHP is on your PATH by checking the version from a terminal:
php -vYou should see output similar to this (your version number will vary):
PHP 8.3.6 (cli) (built: ...) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.3.6, ...If you instead get a "command not found" error, PHP's directory was not added to your PATH — revisit the install step for your OS.
Running PHP without a web server
PHP includes a built-in development server, which is the fastest way to run pages while learning. Create a file named index.php:
<?php
echo "Hello from PHP " . PHP_VERSION;Then start the server from that folder and open the URL it prints:
php -S localhost:8000Visit http://localhost:8000 in your browser. The built-in server is for development only — never expose it to the public internet.
Configuring PHP with Apache
- Open the Apache configuration file located in the Apache installation folder (usually
conf/httpd.confon Windows or/etc/apache2/apache2.confon Linux/macOS). - Find the PHP module loading directive. For manual Windows installations, it typically looks like this:
#LoadModule php_module "modules/php8apache2_4.dll"- Remove the
#symbol to uncomment the line, ensure the path matches your PHP installation directory, then save and close the file. (Note: On Linux/macOS, PHP is usually enabled via package managers using commands likesudo a2enmod php8.2.) - Restart Apache to apply the changes.
Testing the PHP Installation
- Create a new file with a
.phpextension (e.g.index.php) in your web root folder (usuallyhtdocsor/var/www/htmlon Linux/macOS). - Enter the following code into the file:
- Save and close the file.
- Open a web browser and navigate to
http://localhost/index.php. - If everything is set up correctly, you will see a page displaying information about your PHP installation. You can also verify the installation via the command line by running
php -v.
Common installation issues
phpis not recognized / command not found. PHP's folder is missing from yourPATH. Re-open the terminal after editingPATH, since changes only apply to new shell sessions.phpinfo()shows as plain text. The browser downloaded the file or the web server isn't processing PHP. Make sure you opened it throughhttp://localhost(served by a web server), not by double-clicking the file.- Wrong PHP version. If multiple versions are installed,
php -vshows the one first on yourPATH. Adjust the order or use the full path (e.g./usr/local/opt/[email protected]/bin/php).
Next steps
Now that PHP runs on your machine, continue with:
- PHP Syntax — how a PHP script is structured.
- PHP Variables — storing and reusing data.
- PHP Echo and Print — outputting content to the page.
Conclusion
You now have PHP installed and verified, plus the ability to run scripts either through Apache or PHP's built-in development server. Pick the workflow that matches your goal — the CLI server for quick learning, or a full Apache/Nginx stack for real applications — and you're ready to start writing PHP.