How to Install PHP on Windows, Mac, and Linux

PHP is a powerful and widely-used scripting language, especially for web development. Whether you’re a seasoned developer or just starting, getting PHP set up on your local machine is a fundamental step. This guide will walk you through the process of installing PHP on Windows, macOS, and Linux, ensuring you’re ready to start coding.


Prerequisites Before Installing PHP

Before diving into the installation process, it’s good to have a few things in mind:

  • Understand Your OS: Know which operating system (Windows, macOS, or Linux distribution like Ubuntu/Debian) you’re running.
  • Administrator Privileges: You’ll need administrator or root access to install software and configure system paths.
  • Web Server (Optional but Recommended): While you can run PHP scripts from the command line, most web projects will require a web server like Apache or Nginx. Bundled solutions like XAMPP/WAMP (for Windows) or Homebrew (for macOS) often include these.

Installing PHP on Windows

Using XAMPP/WAMP

For Windows users, the easiest way to get PHP up and running, along with a web server (Apache) and a database (MySQL/MariaDB), is by using a pre-configured package like XAMPP or WAMP.

  1. Download: Visit the official XAMPP (apachefriends.org) or WAMP (wampserver.com) website and download the appropriate installer for your system.
  2. Run Installer: Execute the downloaded file. Follow the on-screen instructions, accepting the defaults for most options.
  3. Select Components: Ensure that PHP, Apache, and MySQL/MariaDB are selected during the installation.
  4. Complete Installation: Once the installation is finished, launch the XAMPP Control Panel or WAMPmanager.
  5. Start Services: From the control panel, start the Apache and MySQL/MariaDB services.

Now you have PHP, Apache, and a database server all set up!

Manual Installation

While XAMPP/WAMP is recommended, you can also install PHP manually:

  1. Download PHP: Go to the PHP for Windows downloads page and download the Thread Safe version of the PHP zip file (e.g., php-X.Y.Z-nts-Win32-vcXX-x64.zip).
  2. Extract: Create a new folder, for example, C:\php, and extract the contents of the downloaded zip file into it.
  3. Configure php.ini:
    • Rename php.ini-development to php.ini in your C:\php folder.
    • Open php.ini with a text editor.
    • Uncomment (remove the semicolon ;) and adjust directives like extension_dir = "ext" and enable necessary extensions (e.g., extension=php_mysqli.dll, extension=php_curl.dll).
  4. Add PHP to PATH:
    • Search for “Environment Variables” in the Windows search bar and open “Edit the system environment variables.”
    • Click “Environment Variables…”
    • Under “System variables,” find the Path variable and click “Edit…”
    • Click “New” and add the path to your PHP installation (e.g., C:\php).
    • Click “OK” on all windows to save changes.
  5. Configure Web Server (e.g., Apache): If you’re using Apache (which you’d also need to install manually), you’ll need to configure it to work with PHP by adding lines like LoadModule php_module "C:/php/php8apache2_4.dll" to your httpd.conf file. This step is more complex and often why XAMPP/WAMP is preferred.

Installing PHP on Mac

Using Homebrew

Homebrew is the easiest and most recommended way to install PHP (and many other developer tools) on macOS.

  1. Install Homebrew: If you don’t have Homebrew installed, open your Terminal and run:
    Bash
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    Follow the on-screen instructions.
  2. Install PHP: Once Homebrew is installed, you can install a specific PHP version (e.g., PHP 8.2) by running:
    Bash
    brew install php@8.2
    Homebrew will install PHP and manage its dependencies.
  3. Link PHP (if necessary): Homebrew will usually symlink the newly installed PHP version. If not, you might need to run:
    Bash
    brew link php@8.2 --force --overwrite
  4. Install a Web Server (e.g., Nginx or Apache):
    Bash
    brew install nginx # or brew install httpd # for Apache
    You’ll then need to configure your web server to process PHP files using PHP-FPM (FastCGI Process Manager).

Built-in PHP Version (macOS)

Older macOS versions used to come with a pre-installed version of PHP, but this is no longer the case with newer macOS releases. Even if present, it’s generally not recommended to use the system’s built-in PHP for development, as it might be outdated or not have the necessary extensions. Always prefer Homebrew for a modern, actively managed PHP environment.


Installing PHP on Linux (Ubuntu/Debian)

For Ubuntu and Debian-based distributions, the apt-get package manager makes PHP installation straightforward.

Using apt-get command

  1. Update Package List: It’s always a good idea to update your package list first:
    Bash
    sudo apt update sudo apt upgrade
  2. Install PHP and Apache (or Nginx):
    • For PHP and Apache:
      Bash
      sudo apt install php libapache2-mod-php
      This command installs the default PHP version available in your distribution’s repositories and the Apache module that allows Apache to process PHP files.
    • For PHP and Nginx (requires PHP-FPM):
      Bash
      sudo apt install php-fpm nginx
      You will then need to configure Nginx to pass PHP requests to PHP-FPM.
  3. Install Common PHP Extensions: You’ll likely need various PHP extensions. You can install them by searching and then installing:
    Bash
    sudo apt search php- sudo apt install php-cli php-mysql php-curl php-gd php-mbstring php-xml php-zip
    Adjust the list of extensions based on your project’s needs.

Verify Installation

After installation, you can verify PHP by checking its version:

Bash

php -v

You should see output similar to this:

PHP 8.2.10 (cli) (built: Aug 24 2023 14:02:18) (NTS) Copyright (c) The PHP Group Zend Engine v4.2.10, Copyright (c) Zend Technologies with Zend OPcache v8.2.10, Copyright (c), by Zend Technologies


Testing PHP Installation (phpinfo())

A classic way to test your PHP installation is by creating a phpinfo.php file.

  1. Create File: In your web server’s document root (e.g., htdocs for XAMPP, www for WAMP, /var/www/html for Linux Apache, or your Homebrew Apache/Nginx directory on Mac), create a file named phpinfo.php with the following content:
    PHP
    <?php phpinfo(); ?>
  2. Access in Browser: Open your web browser and navigate to http://localhost/phpinfo.php.You should see a detailed page displaying your PHP configuration, version, loaded extensions, and much more. If you see this page, your PHP installation is successful!

Common Installation Issues & Fixes

  • “PHP command not found” (Windows/Mac/Linux):
    • Fix: Ensure PHP’s executable directory is added to your system’s PATH environment variable. For Homebrew, run
      brew link php@X.Y --force --overwrite.
  • Web server not processing PHP files:
    • Fix: Ensure your web server (Apache, Nginx) is correctly configured to use the PHP module (Apache) or PHP-FPM (Nginx). Check web server error logs for clues.
  • Missing Extensions:
    • Fix: Edit your php.ini file and uncomment (remove ;) the relevant extension= lines. For Linux, use
      sudo apt install php-extension_name.
  • Version Conflicts (especially on Mac):
    • Fix: If you have multiple PHP versions, ensure your PATH variable prioritizes the desired version. Homebrew can help manage this with
      brew link.

Conclusion + FAQs

Congratulations! You’ve successfully installed PHP on your chosen operating system. You’re now equipped to build dynamic web applications and explore the vast world of PHP development.

FAQs

Q: Can I have multiple PHP versions installed?

A: Yes, it’s common for developers to work with multiple PHP versions. Tools like Homebrew (Mac) and ppa:ondrej/php (Linux) make managing different versions much easier.

Q: What’s the difference between Thread Safe and Non-Thread Safe PHP?

A: Thread Safe (TS) versions are designed for multi-threaded web servers like Apache (when using mod_php). Non-Thread Safe (NTS) versions are generally used with FastCGI (FPM) web servers like Nginx or Apache with mod_fcgid, as well as for command-line usage. For Windows, if you’re using XAMPP/WAMP, it often comes with a TS version suitable for Apache.

Q: Do I need a web server to run PHP?

A: Not necessarily. You can run PHP scripts from the command line (php your_script.php). However, for web applications, a web server (Apache, Nginx) is essential to serve files and process HTTP requests.

Q: How do I update PHP to a newer version?

A: On Windows with XAMPP/WAMP, you’d typically download a newer version of the entire package. For Homebrew (Mac), use brew upgrade php@X.Y. On Linux, use sudo apt install php8.X (replacing 8.X with the desired version) and then configure your web server to use the new version.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top