Published on

Compile PHP8 on Windows WSL2 Ubuntu

Authors

This guide shows how to compile PHP 8 on Windows WSL2 Ubuntu. It also includes a simpler alternative using the ondrej/php PPA if you don’t need to build from source.

Prerequisites

sudo apt update
sudo apt install -y build-essential autoconf bison re2c pkg-config \
  libxml2-dev libsqlite3-dev libonig-dev libcurl4-openssl-dev libssl-dev \
  libzip-dev libjpeg-dev libpng-dev libwebp-dev libxpm-dev libfreetype6-dev \
  libreadline-dev libtidy-dev libxslt1-dev libicu-dev libgmp-dev \
  libsqlite3-0 zlib1g-dev

Get PHP source

# Example: build PHP 8.0.x
wget https://www.php.net/distributions/php-8.0.30.tar.gz
 tar -xzf php-8.0.30.tar.gz
cd php-8.0.30

Alternatively, clone php-src:

git clone https://github.com/php/php-src.git
cd php-src
./buildconf --force

Configure

Adjust the prefix to where you want PHP to be installed (e.g. /opt/php8):

./configure \
  --prefix=/opt/php8 \
  --with-config-file-path=/opt/php8/etc \
  --enable-fpm \
  --with-openssl \
  --with-zlib \
  --with-curl \
  --with-zip \
  --enable-mbstring \
  --enable-intl \
  --with-xsl \
  --with-tidy \
  --with-gmp \
  --with-pdo-mysql \
  --with-pdo-sqlite \
  --with-sqlite3 \
  --with-readline \
  --with-iconv \
  --with-freetype \
  --with-jpeg \
  --with-webp

Notes:

  • Some flags differ across minor versions; run ./configure --help to discover options on your system.
  • If a dependency is missing, install the corresponding -dev package and re-run configure.

Build & install

make -j"$(nproc)"
sudo make install

Copy default INI as a starting point and create FPM/pools as needed:

sudo mkdir -p /opt/php8/etc
cp php.ini-development /opt/php8/etc/php.ini

Optionally install and enable common extensions via pecl or compile them along.

Switch versions with update-alternatives

Register your new PHP binary with update-alternatives so you can switch between versions:

sudo update-alternatives --install /usr/bin/php php /opt/php8/bin/php 80
sudo update-alternatives --config php
php -v

If you installed php-fpm, set up a systemd service or run it manually for development.

Alternative: Install from PPA (simpler)

If you don’t need to compile:

sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
sudo apt install -y php8.0 php8.0-cli php8.0-common php8.0-mbstring php8.0-xml php8.0-curl

Switch with:

sudo update-alternatives --config php
php -v

Troubleshooting

  • Missing headers during configure? Install the -dev variant of the library (e.g., libonig-dev, libzip-dev).
  • Linker errors about JPEG/PNG/WebP: ensure corresponding -dev packages and --with-<lib> flags are present.
  • On WSL2, ensure you have enough memory/CPU; reduce -j$(nproc) if you hit resource limits.
  • If pecl isn’t found, confirm your $PATH includes /opt/php8/bin or the install prefix you used.