W3docs

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:

ApproachBest forIncludes a web server?
PHP CLI + built-in serverLearning, small scriptsBuilt-in (dev only)
XAMPP / MAMP / LaragonBeginners wanting Apache + MySQLYes
Package managerDevelopers, production-like setupsNo (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 php

Prefer 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 php

Homebrew links the php binary onto your PATH automatically.

Linux (Debian/Ubuntu)

Use apt:

sudo apt update
sudo apt install php php-cli

On 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 -v

You 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:8000

Visit 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

  1. Open the Apache configuration file located in the Apache installation folder (usually conf/httpd.conf on Windows or /etc/apache2/apache2.conf on Linux/macOS).
  2. Find the PHP module loading directive. For manual Windows installations, it typically looks like this:
#LoadModule php_module "modules/php8apache2_4.dll"
  1. 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 like sudo a2enmod php8.2.)
  2. Restart Apache to apply the changes.

Testing the PHP Installation

  1. Create a new file with a .php extension (e.g. index.php) in your web root folder (usually htdocs or /var/www/html on Linux/macOS).
  2. Enter the following code into the file:
php— editable, runs on the server
  1. Save and close the file.
  2. Open a web browser and navigate to http://localhost/index.php.
  3. 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

  • php is not recognized / command not found. PHP's folder is missing from your PATH. Re-open the terminal after editing PATH, 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 through http://localhost (served by a web server), not by double-clicking the file.
  • Wrong PHP version. If multiple versions are installed, php -v shows the one first on your PATH. 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:

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.

Practice

Practice
What are the ways to install PHP?
What are the ways to install PHP?
Was this page helpful?