Himanshu SharmaSoftware engineer
Let’s talk

PHP · Lesson 2 of 54

Install PHP

Install PHP on Windows, macOS or Linux, or run it with Docker, then check the version and the loaded extensions from the command line.

12 minGetting startedUpdated 22 October 2026

Awaiting technical review. This lesson is readable but not yet listed in search engines.

By the end of this lessonInstall a current version of PHP and confirm it works with php -v.

Which version you need

This course uses PHP 8.2 or newer. Every output shown in it was produced by running the code with real PHP. Older versions miss features the course relies on (for example readonly classes and some functions), and versions before 8.1 no longer receive security fixes, so there is no reason to start on them.

You can have PHP installed and not know it: macOS used to ship with it, many Linux servers have it, and tools such as XAMPP include it. The first step is to open a terminal (Command Prompt or PowerShell on Windows, Terminal on macOS and Linux) and type:

bash
php -v

If you see a version number of 8.2 or higher, skip to Your first PHP script. If you see "command not found" or an older version, install it as described below.

Windows

There are two simple routes on Windows.

XAMPP is a free bundle of Apache, MariaDB (a MySQL-compatible database) and PHP. Download it from the Apache Friends website, run the installer and keep the default folder, C:\xampp. PHP lands in C:\xampp\php. XAMPP is convenient for beginners because one control panel starts the web server and the database.

The official zip from windows.php.net is lighter if you only want PHP. Download the "Non Thread Safe" x64 zip for the current version, unzip it to a folder such as C:\php, copy php.ini-development to php.ini, and open php.ini in a text editor to enable the extensions you need by removing the semicolon in front of lines such as extension=pdo_sqlite and extension=mbstring.

Either way, the terminal must be able to find php.exe. Add the PHP folder (C:\xampp\php or C:\php) to the Path environment variable: search Windows for "environment variables", edit Path for your user, add the folder, and open a new terminal window. Then php -v should work.

macOS

Install Homebrew if you do not have it, then:

bash
brew install php
php -v

Homebrew keeps PHP up to date with brew upgrade php. The configuration file is reported by php --ini.

Linux

Use your distribution's package manager. On Ubuntu and Debian:

bash
sudo apt update
sudo apt install php-cli php-mbstring php-xml php-sqlite3 php-mysql php-intl
php -v

On Fedora, use sudo dnf install php-cli php-mbstring php-pdo php-mysqlnd. If your distribution offers only an old version, the Ondřej Surý PPA (Ubuntu) and the Remi repository (Fedora, RHEL) provide current builds.

Docker

If you already use Docker, you can run PHP without installing it on the machine at all. This runs a script from the current folder in the official image:

bash
docker run --rm -v "$PWD":/app -w /app php:8.3-cli php script.php

Check what you have

Three commands tell you most of what you need to know about an installation. The first prints the version:

php
<?php
echo version_compare(PHP_VERSION, '8.2.0', '>=') ? "PHP version is fine\n" : "Please upgrade PHP\n";
echo "Major version: " . PHP_MAJOR_VERSION . "\n";
result
PHP version is fine
Major version: 8

php -m lists the extensions, the optional parts of PHP. This course uses mbstring (Unicode text), pdo_sqlite (a small database that needs no server), json and intl in places. You can also ask from inside PHP:

php
<?php
foreach (['mbstring', 'pdo_sqlite', 'json'] as $extension) {
    echo $extension . ': ' . (extension_loaded($extension) ? 'loaded' : 'missing') . "\n";
}
result
mbstring: loaded
pdo_sqlite: loaded
json: loaded

If one is missing, enable it in php.ini (Windows and some Linux builds) or install the matching package (php-mbstring, php-sqlite3 on Ubuntu). php --ini prints the path of the php.ini in use, which saves you editing the wrong copy, a very common source of confusion.

A code editor

Any text editor works, but a code editor makes PHP much easier to read. Visual Studio Code with the "PHP Intelephense" extension is free and popular; PhpStorm is a paid editor built for PHP. Both colour the code, point out mistakes as you type and complete function names.

Common mistakes

  • Editing the wrong php.ini. There can be several on one machine. Always check php --ini first.
  • Not opening a new terminal after changing the PATH on Windows. Old windows keep the old PATH.
  • Installing a very old PHP from an outdated guide or package list. Check php -v and aim for 8.2 or newer.
  • Mixing up XAMPP's PHP and another copy. If php -v shows a different version from the one you expect, run where php (Windows) or which php (macOS and Linux) to see which file runs.

Exercise

Install PHP, then (1) print the version with php -v, (2) find your php.ini with php --ini, and (3) check that mbstring and pdo_sqlite appear in the output of php -m.

Show one possible answer
bash
php -v
php --ini
php -m

The first command should show 8.2 or higher. The second shows a line such as "Loaded Configuration File: C:\xampp\php\php.ini". In the third, look for mbstring and pdo_sqlite in the list. If either is missing, enable it in that php.ini or install the package for it, then run php -m again.

Key points

  • You need PHP 8.2 or newer for this course. Check with php -v.
  • On Windows, XAMPP or the official zip both work; on macOS use Homebrew; on Linux use the distribution's packages.
  • php -m lists the loaded extensions, such as pdo_sqlite, mbstring and intl.
  • php --ini shows which php.ini file is used, which is where settings and extensions are configured.

Check yourself

1. Which command prints the installed PHP version?
2. You get the error 'php is not recognized as an internal or external command' on Windows. What is the most likely cause?
3. Which command lists the PHP extensions that are loaded?
4. Where are extensions such as pdo_mysql usually switched on?