Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Installing and Running PHP 8.x from the Command Line

Hello! This is the first lesson in a four-module introduction to modern PHP. You’ll begin by getting PHP running locally; later lessons will build from everyday syntax to classes, web requests, and project workflow. For now, the goal is practical: install a currently supported PHP 8.x release, run a file in your terminal, and serve that same file to a browser.

Allow roughly 40 minutes, including installation. Setup may take longer if your computer already has another PHP version installed.


Install PHP and verify which version runs

PHP is an interpreter: it executes PHP code and produces output. You can invoke it directly from a terminal, or use its built-in development server to respond to browser requests. Neither route requires you to install a database or a separate web server for this lesson.

Before installing, check whether PHP is already available:

php -v

Look for PHP 8.x and (cli) in the output. An installed PHP 8 release is not necessarily a supported one: older 8.x releases eventually reach end of support. Choose a release currently listed as supported on PHP’s official downloads pages, and compare that choice with the version reported by php -v. Examples in older tutorials and manuals may display obsolete versions.

Choose the installation route for your operating system:

  • Windows: Obtain a ZIP build of a currently supported PHP 8.x release through PHP’s official downloads pages. For command-line use, a non-thread-safe build is sufficient; the thread-safe build shown in some older tutorials also works. Choose the build appropriate for your system, extract it to a stable directory such as C:\tools\php, and follow any runtime-prerequisite instructions supplied with the build. Add the directory containing php.exe to your Path environment variable using the Windows environment-variable editor. Open a new terminal before checking the result.

  • macOS with Homebrew: Run brew install php in a terminal. If Homebrew says PHP is already installed but php -v shows an unsupported release, update that installation with brew upgrade php, then check again.

  • Debian or Ubuntu with APT: Run the commands below. On an older operating-system release, its default package may be an unsupported PHP version; verify rather than assuming the package name guarantees support.

    sudo apt update
    sudo apt install php-cli
    
  • Fedora with DNF: Run sudo dnf install php-cli, then verify the installed version.

If your package manager offers only an unsupported release, use a maintained package source appropriate to your operating system or update the operating system before continuing. The important outcome is a supported version you can actually invoke, not merely a successful installer message.

The following video shows the installation-to-browser workflow. Its particular download is from an older recording, so use it to understand the steps, not to select today’s version.

Install PHP 8, Use the built-in Web Server and Run PHP Files

Watch “Install PHP 8, Use the built-in Web Server and Run PHP Files” by BoostMyTool to see version checking, command-line execution, and the development server in action.

On any operating system, watch version verification, file execution, and browser access. If you use Windows, ZIP installation and Path setup provide additional orientation. Use your system’s current Path editor rather than copying the video’s older method of editing a raw Path string; you can skip its intervening php.ini and extension changes for this lesson.

After installation, open a fresh terminal and run php -v again. If it reports the wrong version, locate the executable your terminal found:

# macOS or Linux
command -v php

# Windows Command Prompt or PowerShell
where.exe php

Multiple PHP installations can coexist. These commands help reveal whether an older executable appears earlier on your Path. Fix that selection and repeat php -v before moving on.


Run your first file from the command line

Create a folder called php-basics, with a public folder inside it. In public, save a file named index.php containing:

<?php
echo "Hello from PHP!\n";

Use a code editor or plain-text editor, and ensure the file is actually named index.php, not index.php.txt. You do not need to understand every punctuation mark yet: <?php marks the start of PHP code, and echo produces output. The next lesson will unpack file structure, statements, comments, and output.

Open a terminal in php-basics and run:

php public/index.php

You should see:

Hello from PHP!

This invocation executes the file once and returns you to the terminal prompt. The browser is not involved. If PHP says it cannot open the file, check both your terminal’s current folder and the file’s name. If it reports a parse error, compare the file with the example, including the opening tag, quotes, and semicolon.

One useful distinction: php -v tells you which interpreter the terminal will run; php public/index.php tells that interpreter which file to execute. Both checks matter when troubleshooting an installation.


Serve the same file to a browser

PHP’s built-in server lets you test a small application locally without configuring Apache or Nginx. Before starting it, read the manual’s short explanation of what the server serves and its intended use.

PHP: Built-in web server - Manual

Read the PHP manual’s “Built-in web server” section to understand its document root, default index-file behavior, and development-only warning.

In “Built-in web server,” begin with the opening warning. Then read from the server explanation, focusing on how the working directory or -t determines the document root and why visiting a directory can load index.php. You can stop before the router-script paragraph; routing is not needed here.

From your php-basics folder, start the server:

php -S 127.0.0.1:8000 -t public

The -S option starts the server at the specified local address and port. The -t public option makes public the document root—the folder from which the server serves files. Keeping that choice explicit makes it easier to see which files a browser can request.

Leave that terminal running and visit http://127.0.0.1:8000/ in your browser. You should see Hello from PHP! Visiting http://127.0.0.1:8000/index.php reaches the file explicitly; the shorter address works because the server looks for an index file in the requested directory.

The two runs use the same PHP file but have different lifetimes. The terminal command executes it once. The server remains running and executes the file when a matching browser request arrives. Your browser receives the resulting output, not the PHP source code. You may also see request log lines appear in the server terminal. Press Ctrl+C there when you are finished.

Keep the server bound to 127.0.0.1 for this local exercise. PHP’s built-in server is for development and testing, not for serving an application on a public network.


You now have the essential setup for the rest of the course: a supported PHP interpreter selected in your terminal, a file you can run with php filename.php, and a local server that can deliver its output to a browser. In the next lesson, you’ll examine the syntax of PHP files and write more than a single line of output.

End of Module,Exit lesson

Can't find a good explanation? Sign up and we'll make it for you

Sign up