/PHP, WSL, Windows

How to Install the Latest PHP Version on Windows Subsystem for Linux (WSL)

I needed to locally run a PHP project that required PHP 8. By default only PHP 7.x is available in WSL repository. To upgrade your PHP installation to the latest PHP 8.x version within Windows Subsystem for Linux (WSL) read on.

Step 1: Launch Your WSL Environment

Start by opening your Linux distribution through the Start menu or by typing its name into PowerShell or Command Prompt.

Step 2: Prepare Your System

Before proceeding with the PHP upgrade, ensure your system's package list is up to date. In your WSL terminal, run:

sudo apt update && sudo apt upgrade -y

This command updates the package list and upgrades any existing packages to their latest versions.

Step 3: Install PHP 8.x

The default repositories in your Linux distribution may not immediately offer the latest PHP version. To install PHP 8.x, you might first need to add a third-party repository, such as the widely-used Ondřej Surý PPA for Ubuntu and Debian distributions. Here’s how you do it:

Add the PHP Repository: Install the software-properties-common package to manage additional repositories and then add the Ondřej Surý PHP repository.

sudo apt -y install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update

Install PHP 8.x: With the repository added, you can now install PHP 8.x. Replace 8.x with the specific minor version you wish to install, I used 8.3.

sudo apt install php8.3

This command installs PHP 8.3 along with common extensions. If you need additional PHP extensions, install them using the sudo apt install php8.3-extension_name format.

Step 4: Verify Your PHP Installation

After installation, verify that PHP 8.3 is correctly installed by checking its version:

php -v

PHP 8.3.3-1+ubuntu20.04.1+deb.sury.org+1 (cli) (built: Feb 15 2024 18:38:21) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.3, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.3-1+ubuntu20.04.1+deb.sury.org+1, Copyright (c), by Zend Technologies

This command displays the installed PHP version, confirming that you're now running PHP 8.3 on your WSL environment.