How to Install PHP on Ubuntu 24.04

By 

•

Updated on

•

10 min read

Ubuntu and PHP logos with the title Install PHP on Ubuntu 24.04

PHP is one of the most used server-side programming languages. Many popular CMS and frameworks such as WordPress, Magento, and Laravel are written in PHP.

This guide covers the steps necessary to install PHP on Ubuntu 24.04 and integrate it with Nginx and Apache. The default Ubuntu 24.04 repositories include PHP 8.3. We will also show you how to install other PHP versions.

Quick Reference

For a printable quick reference, see the PHP cheatsheet .

TaskCommand
Install PHP 8.3 with Apachesudo apt install libapache2-mod-php8.3
Install PHP 8.3 with Nginxsudo apt install php8.3-fpm
Install the MySQL extension for PHP 8.3sudo apt install php8.3-mysql
Check the default CLI versionphp -v
List modules loaded by the PHP 8.3 CLIphp8.3 -m
Restart PHP-FPMsudo systemctl restart php8.3-fpm
Switch PHP version (CLI)sudo update-alternatives --config php
Install PHP 8.5 (Sury repository)sudo apt install php8.5-fpm

Prerequisites

To follow this guide, you need to be logged in as a user with sudo privileges .

Installing PHP 8.3 with Apache

If you are using Apache as your web server, install the PHP 8.3 Apache module. The package also installs the PHP 8.3 command-line interpreter.

Terminal
sudo apt update
sudo apt install libapache2-mod-php8.3

Once the packages are installed, restart Apache for the PHP module to get loaded:

Terminal
sudo systemctl restart apache2

Installing PHP 8.3 with Nginx

Unlike Apache, Nginx does not have built-in support for processing PHP files. We will use PHP-FPM (FastCGI Process Manager) to handle PHP files.

Run the following commands to install PHP 8.3 and the PHP-FPM package:

Terminal
sudo apt update
sudo apt install php8.3-fpm

Once the installation is completed, the FPM service starts automatically. To check the status of the service, run:

Terminal
sudo systemctl status php8.3-fpm
output
● php8.3-fpm.service - The PHP 8.3 FastCGI Process Manager
     Loaded: loaded (/usr/lib/systemd/system/php8.3-fpm.service; enabled; preset: enabled)
     Active: active (running) since Thu 2026-12-03 20:54:23 UTC; 4s ago

In the output, enabled means PHP-FPM starts at boot, and active (running) means the service is running now.

Edit the Nginx server block for your site and add the PHP location shown below. If your site uses index.php as its home page, add it to the existing index directive so a request for / can reach it.

nginx
server {

    # . . . other code

    index index.php index.html;

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
    }
}

Test the configuration and reload Nginx if the test passes.

Terminal
sudo nginx -t && sudo systemctl reload nginx

Installing PHP Extensions

PHP extensions are compiled libraries that extend the core functionality of PHP. Use apt to install the extension packages for your PHP version. For example, install the MySQL and GD extensions for PHP 8.3 with the following command.

Terminal
sudo apt install php8.3-mysql php8.3-gd

Here are some of the most commonly used PHP extensions:

ExtensionPackageDescription
MySQLphp8.3-mysqlMySQL and MariaDB database support
cURLphp8.3-curlURL transfer library
GDphp8.3-gdImage processing
Mbstringphp8.3-mbstringMultibyte string support
XMLphp8.3-xmlXML parsing and manipulation
ZIPphp8.3-zipZIP archive support
Intlphp8.3-intlInternationalization functions
BCMathphp8.3-bcmathArbitrary precision math

You do not need to install OPcache separately. The PHP 8.3 CLI, FPM, and Apache packages all depend on php8.3-opcache, so APT installs it together with PHP.

To install all commonly needed extensions at once:

Terminal
sudo apt install php8.3-mysql php8.3-curl php8.3-gd php8.3-mbstring \
    php8.3-xml php8.3-zip php8.3-intl php8.3-bcmath

After installing a new PHP extension, restart Apache or PHP-FPM depending on your setup:

Terminal
sudo systemctl restart apache2

Or for Nginx with PHP-FPM:

Terminal
sudo systemctl restart php8.3-fpm

To see which modules the PHP 8.3 CLI loads, run the versioned executable.

Terminal
php8.3 -m

Testing PHP Processing

Create a new file named php-check.php in the document root of the site you configured. Use the path in Apache’s DocumentRoot or Nginx’s root directive. For a site rooted at /var/www/html, open the file with the following command.

Terminal
sudoedit /var/www/html/php-check.php

Add this code to print the PHP version and the interface your web server uses to run it.

php-check.phpphp
<?php
echo PHP_VERSION, ' (', PHP_SAPI, ')', PHP_EOL;

Open /php-check.php on the hostname you configured, for example http://example.com/php-check.php. On a standard Ubuntu 24.04 installation with the Apache PHP module, the page shows the following line.

output
8.3.6 (apache2handler)

The number shows which PHP release served the request. You will see fpm-fcgi with PHP-FPM. After testing, remove the file from your site’s document root. For the example above, use the following command.

Terminal
sudo rm /var/www/html/php-check.php

Installing Other PHP Versions

Ondřej Surý, a Debian developer, maintains a repository that includes multiple PHP versions. The ppa:ondrej/php PPA that older guides use is frozen and no longer receives updates. Ondřej now publishes the Ubuntu packages at packages.sury.org.

This is a third-party source, so use it only if you need a different PHP version than what Ubuntu provides. If you already use ppa:ondrej/php, go straight to Migrating from the PPA . If packages.sury.org is already enabled, skip to Installing PHP 8.5 .

Adding the Sury Repository

For a fresh setup, refresh APT and install the packages needed to download the Sury signing key.

Terminal
sudo apt update
sudo apt install ca-certificates curl

Download and install the Sury archive keyring.

Terminal
keyring=$(mktemp --suffix=.deb) &&
    curl -fsSLo "$keyring" https://packages.sury.org/debsuryorg-archive-keyring.deb &&
    sudo dpkg -i "$keyring" &&
    rm -- "$keyring"

Add the repository for noble, the Ubuntu 24.04 codename, and refresh the package index. The Signed-By field tells APT which keyring to use for this source.

Terminal
sudo tee /etc/apt/sources.list.d/php.sources > /dev/null <<'EOF'
Types: deb
URIs: https://packages.sury.org/php/
Suites: noble
Components: main
Signed-By: /usr/share/keyrings/debsuryorg-archive-keyring.gpg
EOF
sudo apt update

Continue to Installing PHP 8.5 .

Migrating from the PPA

The frozen PPA ships a package that enables the new repository. When you run apt update, APT may ask you to accept the label change to PPA for PHP (deprecated). Accept that change and install the migration package.

Terminal
sudo apt update
sudo apt install debsuryorg-repository-php

Once the package has enabled Sury, remove the old PPA and refresh APT.

Terminal
sudo add-apt-repository --remove ppa:ondrej/php
sudo apt update

Your installed PHP packages stay in place. APT can fetch their next updates from the new repository.

Installing PHP 8.5

Sury carries multiple PHP versions for Ubuntu 24.04. Once you enable it, a system upgrade can replace Ubuntu’s PHP 8.3 packages and libraries such as libgd3 with Sury’s builds. If APT upgrades an unversioned package such as php-fpm or php-mysql to Sury’s version, it can also pull in PHP 8.6. Preview those changes separately with the following command.

Terminal
sudo apt -s upgrade

Use versioned package names to pick a PHP branch. The following command previews installing PHP 8.5 with PHP-FPM and several common extensions. For Apache, use libapache2-mod-php8.5 in place of php8.5-fpm in the preview and install commands.

Terminal
sudo apt -s install php8.5-fpm php8.5-cli php8.5-gd php8.5-curl \
    php8.5-mysql php8.5-mbstring php8.5-xml

The -s option simulates the transaction. Check what APT would install, upgrade, or remove. When the list looks right, run the installation.

Terminal
sudo apt install php8.5-fpm php8.5-cli php8.5-gd php8.5-curl \
    php8.5-mysql php8.5-mbstring php8.5-xml

PHP 8.5 includes OPcache. For PHP 8.4, substitute 8.4 in the package names above. On an Nginx server, use the versioned FPM package. The plain php8.5 metapackage can pull in Apache when PHP-FPM and CGI are both absent.

Check the installed PHP 8.5 CLI directly, regardless of which version the php command selects.

Terminal
php8.5 -v

Switching Between PHP Versions

In automatic mode, update-alternatives points the php command to the installed version with the highest priority. With PHP 8.3 and 8.5 installed, php -v reports 8.5 in automatic mode. To set PHP 8.3 as the default CLI version, run the following command.

Terminal
sudo update-alternatives --set php /usr/bin/php8.3

To interactively choose the default version:

Terminal
sudo update-alternatives --config php

For Apache, disable the current PHP module and enable the one you want:

Terminal
sudo a2dismod php8.3
sudo a2enmod php8.5
sudo systemctl restart apache2

For Nginx, update the PHP-FPM socket path in your server block to point to the correct version:

nginx
fastcgi_pass unix:/run/php/php8.5-fpm.sock;

Then test the configuration and reload Nginx.

Terminal
sudo nginx -t && sudo systemctl reload nginx

Checking the PHP Version

To check the version used by the default PHP command, run:

Terminal
php -v
output
PHP 8.3.6 (cli) (built: Nov 18 2026 23:42:07) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.6, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.6, Copyright (c), by Zend Technologies

In the output, 8.3.6 is the PHP release, and cli means this is the command-line build. To find the version that Apache or PHP-FPM uses, see how to check the PHP version .

PHP Configuration Files

PHP configuration files are located at:

  • Main config: /etc/php/8.3/cli/php.ini (CLI) and /etc/php/8.3/apache2/php.ini (Apache) or /etc/php/8.3/fpm/php.ini (FPM)
  • FPM pool config: /etc/php/8.3/fpm/pool.d/www.conf
  • Extension configs: /etc/php/8.3/mods-available/

After changing any configuration, restart the relevant service:

Terminal
sudo systemctl restart php8.3-fpm

Uninstalling PHP

To remove PHP and related packages, you can purge them and clean up dependencies. The 'php*' pattern matches every package whose name starts with php, including tools such as phpMyAdmin. To preview what would be removed, run:

Terminal
sudo apt -s purge 'php*'

If the list contains only packages you want to remove, run the purge:

Terminal
sudo apt purge 'php*'
sudo apt autoremove

Troubleshooting

Unable to locate package php8.5
Ubuntu 24.04 includes only PHP 8.3. Other versions come from the Sury repository. Add it as shown in the Installing Other PHP Versions section and try again.

502 Bad Gateway in Nginx
Nginx cannot reach the PHP-FPM socket set in fastcgi_pass. This usually happens after switching versions when the server block still points to the old socket. It also happens when the FPM service for that version is stopped. List the sockets with ls /run/php/ and compare them with your server block. For other causes, see how to fix 502 Bad Gateway in Nginx .

FAQ

What PHP version does Ubuntu 24.04 include?
Ubuntu 24.04 ships with PHP 8.3 in its default repositories. The exact upstream version is 8.3.6.

Why does Ubuntu 24.04 report PHP 8.3.6 when newer 8.3 releases exist?
Ubuntu 24.04 stays on PHP 8.3.6 for the life of the release. Security fixes from later 8.3 releases are backported to it. Each round of fixes changes only the Ubuntu part of the package version, such as 8.3.6-0ubuntu0.24.04.14. To see the package version on your system, run apt policy php8.3. If you need the latest upstream 8.3 release, install PHP from the Sury repository.

How do I upgrade PHP 8.3 to PHP 8.5 on Ubuntu 24.04?
Add the Sury repository and install PHP 8.5 next to 8.3 with versioned packages, such as php8.5-fpm and php8.5-mysql. Point your site to the new version and test it. On Nginx, change the PHP-FPM socket in fastcgi_pass. On Apache, disable the php8.3 module and enable php8.5. Once everything works, remove the PHP 8.3 packages.

What is the difference between php and php-fpm?
From Ubuntu’s repositories, the php metapackage installs PHP 8.3 with the Apache module, PHP-FPM, or CGI. On a server that has none of them, APT picks the Apache module. The php-fpm package installs PHP-FPM, the process manager Nginx uses to run PHP code.

Can I run multiple PHP versions at the same time?
Yes. With PHP-FPM, you can install multiple versions from Sury and point each virtual host or server block to the socket for the version it needs.

Do I need to restart Apache or Nginx after installing an extension?
Yes. After installing a PHP extension, restart Apache (sudo systemctl restart apache2) or PHP-FPM (sudo systemctl restart php8.3-fpm) for the extension to be loaded.

Where is the php.ini file located?
PHP uses separate php.ini files for each SAPI. The CLI config is at /etc/php/8.3/cli/php.ini, the Apache config at /etc/php/8.3/apache2/php.ini, and the FPM config at /etc/php/8.3/fpm/php.ini.

Conclusion

We have shown you how to install PHP 8.3 on Ubuntu 24.04 with both Apache and Nginx. To show and log PHP errors while you develop, see our guide on PHP error reporting .

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page