Installing PHP and running your first script

Install PHP on Windows, macOS or Linux, then run code in the terminal and the browser.

  • Beginner
  • 3 min read
  • Updated 17 Oct 2026

What you will learn

  • Install PHP 8 on your operating system
  • Check the installed version
  • Run a script from the terminal
  • Serve pages with the built-in web server

Before you start

  • What is PHP?
  • A computer where you can install software
  • A code editor such as VS Code

To run PHP you need the PHP interpreter installed. You do not need a full web server like Apache for learning: PHP ships with a small built-in server that is perfect for development.

Install PHP

Pick your operating system.

Windows

The simplest option is a local development bundle such as Laragon or XAMPP, which installs PHP, a web server and MySQL together. Alternatively:

  1. Download the latest VS16 x64 Non Thread Safe zip from the PHP for Windows site.
  2. Extract it to C:\php.
  3. Add C:\php to your PATH environment variable.
  4. Open a new terminal so the change takes effect.

macOS

Install Homebrew if you do not have it, then run:

Shell
brew install php

Linux (Debian or Ubuntu)

Shell
sudo apt update
sudo apt install php-cli

Check the installation

Open a terminal and run:

Shell
php -v

You should see something like this. The exact numbers will differ:

Plain text
PHP 8.3.12 (cli) (built: Sep 24 2026 10:12:31) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.12

If you see "command not found" or "'php' is not recognized", PHP is not on your PATH. Close and reopen the terminal first. If that does not help, check the folder you added to PATH.

Run a script from the terminal

Create a folder for this course, for example php-course, and inside it a file called hello.php:

PHP
<?php

echo "Hello from PHP!\n";
echo 'PHP version: ' . PHP_VERSION . "\n";

In the terminal, move into the folder and run it:

Shell
cd php-course
php hello.php

Output:

Plain text
Hello from PHP!
PHP version: 8.3.12

\n is a newline character. In the terminal it moves output to the next line.

Run pages in the browser

Start PHP's built-in server from the same folder:

Shell
php -S localhost:8000

Now create index.php:

PHP
<!DOCTYPE html>
<html lang="en">
<body>
    <h1>It works</h1>
    <p>The time on the server is <?php echo date('H:i'); ?>.</p>
</body>
</html>

Open http://localhost:8000 in your browser. Refresh the page and the time updates, because PHP runs again on every request. Press Ctrl+C in the terminal to stop the server.

The built-in server is for development only. It handles one request at a time and has no security hardening. Real sites run PHP behind a web server such as Nginx, Apache or LiteSpeed.

Set up your editor

Any editor works. If you use VS Code, install the PHP Intelephense extension. It shows errors as you type and completes function names, which saves a lot of time while learning.

Summary

  • Install PHP 8 with a bundle (Windows), Homebrew (macOS) or your package manager (Linux).
  • php -v shows the installed version.
  • php file.php runs a script in the terminal.
  • php -S localhost:8000 serves a folder at http://localhost:8000 for development.

Further reading