PHP Installation: Complete Guide for Windows, macOS & Linux

Learn how to install PHP on Windows, macOS, and Linux. Follow this complete beginner-friendly guide to PHP setup, configuration, extensions, web servers, Composer, and troubleshooting.

PHP Installation

PHP is one of the most widely used server-side scripting languages for building dynamic websites and web applications. Before writing and running PHP programs, you need to install PHP on your computer or use a web server that already provides PHP.

The installation process depends on your operating system and the way you plan to use PHP. Windows users often install PHP directly or use packages such as XAMPP. On macOS, PHP can be installed through package managers such as Homebrew. On Linux, PHP is usually available through the distribution’s package manager.

This guide explains PHP installation in a simple and practical way, including installation methods, configuration, verification, web-server setup, command-line usage, common problems, and useful next steps.

What Is PHP Installation?

PHP installation means setting up the PHP runtime environment on a computer or server so that PHP code can be executed.

A PHP file normally contains code such as:

<?php
echo "Hello, World!";
?>

A web browser cannot execute PHP code by itself. PHP must run on a server or through the PHP command-line interpreter. The PHP engine processes the code and produces output, usually HTML, which is then sent to the browser.

A basic PHP environment may contain:

  • PHP
  • A web server such as Apache or Nginx
  • A database such as MySQL or MariaDB
  • PHP extensions
  • Configuration files
  • A development environment or code editor

You do not always need all of these components. For learning PHP from the command line, installing PHP alone may be enough.

PHP Installation Requirements

Before installing PHP, check your computer’s operating system and decide how you want to use PHP.

For basic PHP development, you generally need:

  • A supported operating system
  • PHP
  • A text or code editor
  • A terminal or command prompt
  • A web browser

For traditional web development, you may also need:

  • Apache or Nginx
  • MySQL or MariaDB
  • Required PHP extensions

Modern PHP development can also use tools such as Composer and frameworks such as Laravel.

PHP Installation on Windows

There are several ways to install PHP on Windows. The best method depends on whether you want a simple learning environment or a more controlled PHP development setup.

Method 1: Install PHP Directly on Windows

PHP can be installed directly on Windows without installing a complete server package.

The general process is:

  1. Download a suitable PHP build.
  2. Extract the PHP files.
  3. Configure PHP.
  4. Add PHP to the Windows PATH.
  5. Verify the installation.
  6. Configure a web server if you need browser-based PHP applications.

The official PHP website provides Windows builds and documentation.

Official PHP website

Download PHP

Go to the official PHP website and choose a version appropriate for your development environment.

For Windows, pay attention to the build type and architecture. Modern Windows systems are commonly 64-bit, but you should choose a build compatible with your system and requirements.

PHP builds may differ in areas such as:

  • Thread safety
  • Architecture
  • Compiler/runtime requirements
  • PHP version

If you are unsure, check the official documentation for the particular PHP version you are installing.

Extract PHP

After downloading the PHP package, extract it to a suitable folder.

For example:

C:\php

The PHP directory should contain files such as:

php.exe
php.ini-development
php.ini-production
ext\

The exact contents can vary depending on the PHP version and distribution.

Add PHP to PATH

Adding PHP to the Windows PATH allows you to run the php command from Command Prompt or PowerShell without entering the complete directory path.

For example, if PHP is installed in:

C:\php

add that directory to the system or user PATH.

After changing PATH, open a new terminal window.

Then run:

php -v

If PHP is installed correctly, the terminal should display the installed PHP version.

For example:

PHP 8.x.x (cli) ...

The exact version shown depends on the version you installed.

Method 2: Install PHP Using XAMPP on Windows

For beginners, XAMPP is one of the easiest ways to create a local PHP development environment.

XAMPP commonly includes:

  • Apache
  • PHP
  • MariaDB
  • phpMyAdmin
  • Additional utilities

This means you can install one package instead of configuring each component separately.

XAMPP official website

How to Install PHP With XAMPP

Download and install XAMPP from its official website.

During installation, select the components you need. For basic PHP development, Apache and PHP are important. Database development may also require MariaDB and phpMyAdmin.

After installation:

  1. Open the XAMPP Control Panel.
  2. Start Apache.
  3. Start MariaDB if your application needs a database.
  4. Open the XAMPP web directory.
  5. Create a PHP file.
  6. Open the file through localhost.

The default web directory is commonly:

C:\xampp\htdocs

Create a file such as:

hello.php

Add:

<?php
echo "Hello, World!";
?>

Then open:

http://localhost/hello.php

If everything is configured correctly, the browser should display:

Hello, World!

Method 3: Install PHP Using a Development Stack

Other Windows development packages can also provide PHP, Apache, and database services in a convenient local environment.

Examples include development stacks designed for PHP applications.

The advantage of using a stack is that you do not need to manually configure every component.

However, if you want to understand how PHP itself works, a direct PHP installation can provide a better learning experience.

PHP Installation on macOS

PHP can be installed on macOS in several ways.

One common approach is using Homebrew, a popular package manager for macOS.

Homebrew official website

First, check whether Homebrew is available:

brew --version

Then PHP can be installed with:

brew install php

After installation, verify PHP:

php -v

You should see information about the installed PHP version.

The exact installation details can change as macOS and Homebrew evolve, so consult the current Homebrew and PHP documentation when setting up a new system.

PHP Installation on Linux

Linux distributions generally make PHP available through their package managers.

The exact command depends on the Linux distribution.

Ubuntu and Debian

On Ubuntu or Debian-based systems, you can commonly install PHP with:

sudo apt update
sudo apt install php

Then check the installation:

php -v

For web development, you may also need additional packages.

For example, Apache with PHP can be installed using packages appropriate for your distribution.

You might also need extensions such as:

sudo apt install php-mysql

The exact package names can differ depending on the Linux release and PHP version.

Fedora

On Fedora, PHP can commonly be installed through DNF:

sudo dnf install php

Then verify:

php -v

Additional extensions can be installed separately when required.

Arch Linux

On Arch Linux, PHP can commonly be installed using:

sudo pacman -S php

Then verify:

php -v

Always check the package documentation for your particular Linux distribution because package names and configuration procedures may change.

How to Check Whether PHP Is Installed

After installing PHP, the easiest verification method is to use the command line.

Open Command Prompt, PowerShell, Terminal, or another shell and run:

php -v

You can also use:

php --version

If PHP is installed correctly, you will see the PHP version and other information.

For example:

PHP 8.x.x (cli) ...
Copyright ...

The important part is that the command is recognized and PHP returns version information.

Check the PHP Configuration

PHP uses a configuration file called php.ini.

You can find information about the active configuration file with:

php --ini

You can also inspect PHP’s configuration from the command line:

php -i

The php --ini command is particularly useful because it helps you determine which configuration file PHP is actually loading.

This is important when you have multiple PHP installations on the same computer.

What Is php.ini?

php.ini is PHP’s main configuration file.

It controls many PHP settings, including:

  • Error reporting
  • Memory limits
  • File upload limits
  • Maximum execution time
  • Time zone
  • Extensions
  • Session settings
  • Date and time behavior

A PHP installation may provide template configuration files such as:

php.ini-development
php.ini-production

The appropriate configuration depends on whether PHP is being used for development or production.

For local development, a development-oriented configuration is generally more suitable.

For a production server, security and performance settings should be configured carefully.

Running PHP From the Command Line

PHP can execute scripts without a web server.

Create a file called:

test.php

Add:

<?php

echo "PHP is working!";

Then open a terminal in the directory containing the file and run:

php test.php

PHP should display:

PHP is working!

This is called CLI, or Command Line Interface, usage.

CLI PHP is useful for:

  • Testing PHP code
  • Running scripts
  • Automation
  • Cron jobs
  • Maintenance tasks
  • Composer
  • Framework commands
  • Development tools

Running PHP With the Built-In Development Server

PHP includes a simple built-in development server.

It is useful for local development and testing.

Suppose your PHP project is stored in:

myproject

Open a terminal inside that directory and run:

php -S localhost:8000

PHP will start a local development server.

You can then open:

http://localhost:8000

If your project contains an index.php file, PHP can process it and display the result.

For example:

<?php

echo "Welcome to my PHP website!";

The built-in server is convenient, but it is not intended to be a production web server.

PHP With Apache

Apache is a popular web server that can work with PHP.

A traditional PHP environment may look like this:

Browser
   ↓
Apache
   ↓
PHP
   ↓
Application
   ↓
Database

When a user requests a PHP page, Apache passes the PHP file to the PHP runtime. PHP executes the code and generates the response.

Packages such as XAMPP simplify this setup for local development.

PHP With Nginx

Nginx is another popular web server used with PHP.

A common setup uses:

Browser
   ↓
Nginx
   ↓
PHP-FPM
   ↓
PHP Application

PHP-FPM stands for PHP FastCGI Process Manager.

It provides a way for Nginx to communicate with PHP processes.

Nginx and PHP-FPM are widely used in production environments, especially for modern web applications.

Installing PHP Extensions

PHP extensions add additional functionality to PHP.

Some applications require extensions for:

  • MySQL databases
  • PostgreSQL
  • XML
  • JSON
  • Multibyte strings
  • cURL
  • Image processing
  • Internationalization
  • ZIP archives

Some functionality is included in modern PHP installations, while other features require separate extensions depending on the operating system and PHP distribution.

You can see loaded extensions with:

php -m

For more detailed information, use:

php -i

If an application reports that a particular extension is missing, identify the required extension and install or enable the appropriate package for your operating system.

Check Whether a Specific Extension Is Enabled

You can use:

php -m

Then look for the extension name.

For example, you might see:

curl
json
mbstring
openssl
PDO

You can also check a particular extension with:

php -r "echo extension_loaded('curl') ? 'Enabled' : 'Disabled';"

This command checks whether the cURL extension is loaded.

PHP and MySQL

Many PHP applications use a database.

MySQL and MariaDB are common choices.

PHP can communicate with MySQL-compatible databases using interfaces such as:

  • MySQLi
  • PDO

A modern application may use PDO because it provides a consistent database interface and supports prepared statements.

For example:

<?php

$pdo = new PDO(
    'mysql:host=localhost;dbname=test',
    'root',
    ''
);

echo "Database connection successful!";

The actual username, password, database name, and server configuration will depend on your environment.

Never copy database credentials from examples into a production application.

Installing Composer

Composer is the dependency manager commonly used in the PHP ecosystem.

It allows developers to install and manage PHP packages.

After installing PHP, you can install Composer from its official website.

Composer official website

After installation, check it with:

composer --version

A working PHP installation is required for Composer to operate correctly.

Composer is particularly important when working with modern PHP frameworks and libraries.

PHP Installation for Laravel

If you plan to use Laravel, installing PHP alone is usually not enough.

You will commonly need:

  • PHP
  • Required PHP extensions
  • Composer
  • A database when required
  • Node.js and npm for frontend asset workflows, depending on the project

After installing PHP and Composer, Laravel can be installed according to the current Laravel documentation.

The exact PHP version and extension requirements depend on the Laravel version you are using, so always check the documentation for that specific version.

PHP Version Selection

Choosing the right PHP version is important.

Using an old PHP release can create security and compatibility problems. At the same time, upgrading an existing application to a newer major version can sometimes require code changes.

Before choosing a version, consider:

  • Application compatibility
  • Framework requirements
  • Required extensions
  • Security support
  • Hosting environment
  • Third-party package compatibility

For a new project, using a currently supported PHP release is generally preferable.

For an existing project, first check its compatibility requirements before upgrading.

PHP Version Compatibility

PHP versions are not all completely compatible with each other.

For example, code written for an older PHP version may use features that have since been deprecated or removed.

Before changing PHP versions in an existing project:

  1. Back up the project.
  2. Check framework requirements.
  3. Check Composer dependencies.
  4. Review deprecated features.
  5. Test the application.
  6. Check all required extensions.
  7. Test the database connection.

Never upgrade a production PHP environment without testing the application first.

How to Create Your First PHP File

After installing PHP, create a file named:

index.php

Add:

<?php

echo "Hello from PHP!";

If you are using the PHP development server, open a terminal in the same directory and run:

php -S localhost:8000

Then visit:

http://localhost:8000

You should see:

Hello from PHP!

Congratulations. Your PHP environment is working.

PHP Installation vs PHP Hosting

Installing PHP on your personal computer creates a local development environment.

PHP hosting is different.

A PHP hosting server normally provides:

  • PHP
  • A web server
  • Database support
  • Domain support
  • SSL/TLS
  • Server configuration
  • File management

You do not normally install PHP manually on shared hosting. The hosting provider usually manages the server environment.

On a VPS or dedicated server, however, you may need to install and configure PHP yourself.

Common PHP Installation Problems

PHP installation is usually straightforward, but beginners can encounter several common problems.

'php' is not recognized

On Windows, you may see an error similar to:

'php' is not recognized as an internal or external command

This usually means PHP is not in the system PATH.

Check the PHP installation directory and add it to PATH.

Then close and reopen the terminal.

Run:

php -v

again.

PHP Version Is Different From Expected

If php -v shows an unexpected version, your computer may have multiple PHP installations.

Check the executable being used.

On Windows:

where php

On Linux and macOS:

which php

This helps identify which PHP executable is being executed.

PHP Works in Terminal but Not in the Browser

This often means the command-line PHP installation is working, but the web server is not correctly configured.

Remember that:

php -v

tests the CLI environment.

It does not prove that Apache, Nginx, or another web server is configured correctly.

404 Not Found

If your browser displays a 404 error, check:

  • The server is running.
  • The URL is correct.
  • The PHP file is inside the correct document root.
  • The requested filename is correct.

For example, if you use the built-in server, make sure you started it from the correct project directory.

PHP Code Appears in the Browser

If the browser displays something like:

<?php echo "Hello"; ?>

instead of the expected output, PHP is probably not being processed by the server.

Check your web-server configuration and PHP integration.

Missing PHP Extension

You may encounter an error such as:

Class "PDO" not found

or an application may report that a required extension is missing.

Use:

php -m

to inspect installed extensions.

Then install or enable the required extension.

Changes to php.ini Are Not Taking Effect

First check which configuration file PHP is using:

php --ini

If you are using Apache or PHP-FPM, remember that the web server may use a different PHP configuration from the CLI.

After changing configuration, you may need to restart the relevant service.

How to Test PHP Installation With phpinfo()

PHP provides a useful function called phpinfo().

Create:

info.php

with:

<?php

phpinfo();

Open the file through your local web server.

The page displays detailed information about:

  • PHP version
  • Server API
  • Loaded extensions
  • Configuration values
  • Environment variables
  • PHP settings

This can be extremely useful when troubleshooting.

However, do not leave a publicly accessible phpinfo() page on a production server because it can reveal detailed information about the server environment.

PHP Configuration for Development

A development environment often benefits from useful error reporting.

For example:

<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');

This can help you find programming errors while developing.

However, displaying detailed errors to visitors is not appropriate for a production website. Production systems should use appropriate error logging and avoid exposing sensitive server information.

PHP Installation Security Tips

Installing PHP is only the beginning. Security is important throughout the development and deployment process.

Keep PHP updated and use supported versions.

You should also:

  • Download PHP from trusted sources.
  • Keep extensions updated.
  • Avoid exposing configuration files.
  • Never publish passwords in source code.
  • Use prepared statements for database queries.
  • Validate user input.
  • Escape output appropriately.
  • Use HTTPS in production.
  • Disable unnecessary services.
  • Do not expose phpinfo() publicly.
  • Keep dependencies updated.
  • Review security advisories for your PHP version and dependencies.

PHP Installation Checklist

Use this checklist after installation:

  • PHP is installed.
  • php -v works.
  • PHP is available in PATH if needed.
  • php --ini identifies the configuration.
  • Required PHP extensions are enabled.
  • A test PHP script runs successfully.
  • The development server works if needed.
  • Apache or Nginx is configured if required.
  • Database connectivity works if required.
  • Composer is installed if required.
  • The PHP version is compatible with your project.
  • Production configuration is secured.

Frequently Asked Questions About PHP Installation

Is PHP free to install?

Yes. PHP is open-source software and can be installed without purchasing a PHP license.

Can I install PHP without Apache?

Yes. PHP can run from the command line, and PHP also includes a simple development server. Apache or Nginx is needed when you want to use those web servers to serve PHP applications.

Can PHP run on Windows?

Yes. PHP can run on Windows and is widely used for local PHP development on Windows computers.

Can PHP run on macOS?

Yes. PHP can be installed and used on macOS through supported installation methods such as package managers.

Can PHP run on Linux?

Yes. PHP is widely supported on Linux, and most major Linux distributions provide PHP packages.

How do I know if PHP is installed?

Run:

php -v

If PHP is installed correctly and available in PATH, the command displays the PHP version.

How do I check installed PHP extensions?

Run:

php -m

This displays loaded PHP modules.

What is php.ini used for?

php.ini is PHP’s main configuration file. It controls many settings related to PHP behavior, error handling, resource limits, extensions, uploads, and other runtime features.

Do I need MySQL to learn PHP?

No. You can learn basic PHP without a database. MySQL or another database becomes useful when you start building applications that store and retrieve data.

Do I need XAMPP to use PHP?

No. XAMPP is convenient, especially for beginners, but PHP can also be installed and configured separately.

Can I run PHP without a web server?

Yes. You can execute PHP scripts using the command line:

php script.php

You can also use PHP’s built-in development server:

php -S localhost:8000

Is PHP installation difficult for beginners?

Basic PHP installation is usually not difficult. Tools such as XAMPP can make local setup easier on Windows. However, manual installation teaches you more about PHP configuration and web-server integration.

Best Way to Install PHP for Beginners

The best installation method depends on your goal.

If you are completely new to PHP and use Windows, a local development package such as XAMPP can provide a simple starting point.

If you want to learn how PHP itself works, installing PHP directly and using the built-in development server is a useful approach.

If you use Linux, your distribution’s package manager is usually the simplest option.

If you use macOS, a package manager such as Homebrew provides a convenient installation method.

For professional development, it is important to understand not only how to install PHP but also how PHP versions, extensions, Composer, web servers, databases, and application dependencies work together.

Conclusion

PHP installation is the first practical step toward developing PHP websites and applications. The exact process depends on your operating system and development requirements.

For simple learning, you can install PHP and run scripts directly from the command line. For traditional local web development, you can combine PHP with Apache or Nginx. Beginner-friendly packages such as XAMPP can simplify the setup by providing PHP, a web server, and database tools in one environment.

After installation, always verify your setup with:

php -v

and inspect your configuration with:

php --ini

You can then create a simple PHP file, run it from the command line or development server, and begin learning PHP.

A properly configured and supported PHP environment gives you a strong foundation for learning everything from basic PHP syntax to databases, APIs, Composer, frameworks, and complete web applications.

Scroll to Top