Skip to content

How to Install PHP (8.5, 8.4, 8.3 & Older) on Ubuntu 24.04

Install PHP on Ubuntu 24.04: the default PHP 8.3, or PHP 8.5, 8.4, 8.2, 7.4 and older via the ondrej PPA. Add extensions, set up Apache or Nginx, and switch versions.

Steps
10
Category
PHP
Covers
Ubuntu
Versions & support
About PHP →, About Ubuntu →
On this page 10 steps

PHP still runs a large share of the web. WordPress, Laravel, Drupal, Magento, Nextcloud and countless in-house applications depend on it, so installing PHP is usually one of the first jobs on a new Ubuntu 24.04 (Noble Numbat) server.

Ubuntu 24.04 ships PHP 8.3 in its own repositories. That is a good default, but many projects need something else: the newer PHP 8.5 or 8.4, or an older branch such as 8.2, 8.1 or even 7.4 for a legacy application. This guide shows both routes: installing the stock PHP 8.3 package, and installing any version from 8.5 down to 5.6 from the Ondřej Surý PPA. You will also add extensions, connect PHP to Apache or Nginx, run several versions side by side, and switch between them.

Updated November 2026: the guide now covers PHP 8.5 and 8.4, and includes a version support table and multi-version switching.

Which PHP Version Should You Install?

Each PHP branch gets two years of active support (bug and security fixes) and then two more years of security-only fixes. The table shows where each branch stands as of November 2026:

VersionReleasedSecurity support untilStatus
PHP 8.5Nov 2025Dec 31, 2029Active support. Best choice for new projects
PHP 8.4Nov 2024Dec 31, 2028Active support until Dec 31, 2026
PHP 8.3Nov 2023Dec 31, 2027Security fixes only. Ubuntu 24.04 default
PHP 8.2Dec 2022Dec 31, 2026Security fixes only. Reaches end of life very soon
PHP 8.1 and older2021 and earlierEndedEnd of life. Use only for legacy code

In short: use PHP 8.5 or 8.4 for new work, keep 8.3 if you want packages maintained by Ubuntu itself, and install 8.2, 8.1, 7.4 or 5.6 only when an application cannot run on anything newer. See the PHP release tracker for the latest point releases and dates.

Prerequisites

  • A system running Ubuntu 24.04 LTS (desktop or server)
  • A user account with sudo privileges
  • An internet connection

Step 1 of 10Update Your System

Refresh the package index and apply pending updates before installing anything:

sudo apt update
sudo apt upgrade -y

If the upgrade pulled in a new kernel, reboot before continuing.

Step 2 of 10Option A: Install the Default PHP 8.3 from Ubuntu

If PHP 8.3 is fine for your application, you don’t need any extra repository. Ubuntu’s php meta-package installs PHP 8.3, and Canonical backports security fixes into it for the whole life of Ubuntu 24.04:

sudo apt install php php-cli php-common -y

Note that Ubuntu keeps the version number fixed at 8.3.6 and patches it in place, so php -v will show 8.3.6 even though the package is fully patched. If you need the newest upstream point release or a different PHP branch, use Option B instead.

Tip: On a fresh system, sudo apt install php also pulls in Apache and libapache2-mod-php. If you plan to use Nginx, install php-fpm first (or install php-cli only) so Apache is not added.

Step 2 of 10Option B: Add the Ondřej Surý PHP PPA

The ondrej/php PPA is maintained by Ondřej Surý, who also maintains PHP packaging in Debian. For Ubuntu 24.04 it currently provides PHP 8.5, 8.4, 8.3, 8.2, 8.1, 8.0, 7.4, 7.3, 7.2, 7.1, 7.0 and 5.6, and it is usually updated within days of each upstream release.

Install the helper package that provides add-apt-repository, then add the PPA:

sudo apt install software-properties-common -y
sudo LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php -y
sudo apt update

The LC_ALL=C.UTF-8 prefix avoids a Unicode error that add-apt-repository can throw on systems with a non-UTF-8 locale, because the maintainer’s name contains the letter “ř”. The command saves the repository as /etc/apt/sources.list.d/ondrej-ubuntu-php-noble.sources and imports its signing key.

Adding the PPA does not change anything that is already installed. It only makes the extra versions available to apt. There are two side effects to know about. The PPA’s PHP 8.3 is newer than Ubuntu’s 8.3.6, so apt upgrade replaces Ubuntu’s 8.3 packages with the PPA builds. And the unversioned php and php-* meta-packages then follow the PPA’s default version, which is newer than 8.3. With the PPA enabled, always install versioned packages such as php8.4-curl so you get exactly the version you expect.

Step 3 of 10Install a Specific PHP Version

With the PPA enabled, every PHP package is prefixed with its version number (phpX.Y). Pick the version your application needs:

  • PHP 8.5 (latest)
    sudo apt install php8.5 php8.5-cli php8.5-common -y
    
  • PHP 8.4
    sudo apt install php8.4 php8.4-cli php8.4-common -y
    
  • PHP 8.3
    sudo apt install php8.3 php8.3-cli php8.3-common -y
    
  • PHP 8.2
    sudo apt install php8.2 php8.2-cli php8.2-common -y
    
  • PHP 8.1 (end of life)
    sudo apt install php8.1 php8.1-cli php8.1-common -y
    
  • PHP 7.4 (end of life)
    sudo apt install php7.4 php7.4-cli php7.4-common -y
    
  • PHP 5.6 (end of life)
    sudo apt install php5.6 php5.6-cli php5.6-common -y
    

PHP 8.0, 7.3, 7.2, 7.1 and 7.0 follow the same pattern: replace the version number in the package names.

Warning: PHP 8.1 and older no longer receive security fixes from the PHP project. Run them only for legacy code that cannot be upgraded, keep them away from the public internet where you can, and plan a migration.

Note: Like Ubuntu’s own package, phpX.Y pulls in Apache and its PHP module on a fresh system. If you use Nginx, install phpX.Y-fpm first, or install only phpX.Y-cli.

Step 4 of 10Verify the Installation

Check the active PHP version:

php -v

The output should look similar to this:


PHP 8.5.12 (cli) (built: Oct 23 2026 10:14:52) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.5.12, Copyright (c) Zend Technologies
    with Zend OPcache v8.5.12, Copyright (c), by Zend Technologies

With more than one version installed, php runs the default version. Each version also has its own binary, so you can always call one directly, for example php8.4 -v or php7.4 -v.

Step 5 of 10Install PHP Extensions

Extensions are packaged as phpX.Y-name. This set covers what WordPress, Laravel, Symfony and most other applications expect (shown for PHP 8.4; change the version to match yours):

sudo apt install php8.4-{mysql,curl,gd,mbstring,xml,zip,intl,bcmath,opcache,readline} -y

On Ubuntu’s default PHP 8.3 (Option A), drop the version number:

sudo apt install php-{mysql,curl,gd,mbstring,xml,zip,intl,bcmath,readline} -y

A few things worth knowing:

  • PHP 8.5 has OPcache built in. There is no php8.5-opcache package, so leave opcache out of the list for 8.5 or apt will stop with “Unable to locate package”.
  • Install phpX.Y-pgsql instead of phpX.Y-mysql if your application uses PostgreSQL.
  • To find an extension’s exact package name, search for it: apt search php8.4- | grep redis.
  • List the loaded modules with php -m. To turn individual modules on and off, see how to enable or disable PHP modules on Ubuntu.

Step 6 of 10Use PHP with Apache or Nginx

Apache (mod_php)

Install the Apache module for your PHP version. Only one mod_php version can be enabled at a time, so disable any other one first:

sudo apt install apache2 libapache2-mod-php8.4 -y
sudo a2dismod php8.3
sudo a2enmod php8.4
sudo systemctl restart apache2

a2dismod just prints a notice if that module was not enabled, so it is safe to run. To give different sites different PHP versions under Apache, use PHP-FPM instead; see running multiple PHP versions with Apache.

Nginx (PHP-FPM)

Nginx passes PHP requests to PHP-FPM over a socket. Install and enable the FPM service for your version:

sudo apt install php8.4-fpm -y
sudo systemctl enable --now php8.4-fpm

Then point the PHP location in your Nginx server block at the version’s socket:


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

Test and reload Nginx with sudo nginx -t && sudo systemctl reload nginx. The socket path contains the version number, so remember to update it when you change PHP versions. A stale socket path is the most common cause of a 502 Bad Gateway error after a PHP upgrade. For a complete walkthrough, see how to set up Nginx with PHP-FPM on Ubuntu 24.04.

Step 7 of 10Run Multiple PHP Versions and Switch the Default

PPA versions install side by side under /etc/php/X.Y/ and /usr/bin/phpX.Y, so you can keep, for example, PHP 8.4 for new sites and PHP 7.4 for an old application on the same server. Each FPM version runs as its own service (php8.4-fpm, php7.4-fpm) with its own socket.

The php command on the command line is managed by the alternatives system. To change which version it points to, run:

sudo update-alternatives --config php

Choose the number for the version you want. You can also set it directly:

sudo update-alternatives --set php /usr/bin/php8.4

This changes only the CLI default (and with it Composer, cron jobs and scripts). Web servers keep using whatever you configured in Step 6. More detail is in how to switch between multiple PHP versions on Ubuntu.

Step 8 of 10Adjust php.ini Settings

Every SAPI (server API) reads its own configuration file:

  • CLI: /etc/php/X.Y/cli/php.ini
  • Apache: /etc/php/X.Y/apache2/php.ini
  • PHP-FPM: /etc/php/X.Y/fpm/php.ini

Editing the wrong file is a classic time waster. php --ini shows the file the CLI loads. For the web server, check the “Loaded Configuration File” line of a phpinfo() page (see Step 9). These are sensible starting values for a typical web application:


memory_limit = 256M
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 120
date.timezone = UTC

Restart the service that uses the file so the changes take effect, for example sudo systemctl restart php8.4-fpm or sudo systemctl restart apache2. The CLI reads php.ini on every run, so it needs no restart.

Step 9 of 10Test PHP in the Browser

Create a test file in the web root:

echo "<?php phpinfo();" | sudo tee /var/www/html/info.php

Open http://your-server-ip/info.php in a browser. The page shows the PHP version the web server is actually using, the loaded php.ini and all enabled extensions. Delete the file when you are done, because it reveals details about your server:

sudo rm /var/www/html/info.php

Step 10 of 10Install Composer (Optional)

Composer is the dependency manager most PHP projects use. Ubuntu 24.04 has a composer package, but it is an older 2.7 release. To get the current version, use the official installer and move the binary into your PATH:

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
rm composer-setup.php
composer --version

Composer runs with the default php binary. To run it with another version, call that version directly: php7.4 /usr/local/bin/composer install. For more options, see how to install Composer on Ubuntu.

Uninstall a PHP Version

To remove a version you no longer need, purge its packages and clean up unused dependencies:

sudo apt purge 'php7.4*' -y
sudo apt autoremove -y

The quotes stop the shell from expanding the wildcard. Make sure no site, FPM pool or cron job still uses that version first.

Troubleshooting

  • “Unable to locate package php8.4”: the PPA is missing or apt was not refreshed. Run Step 2 (Option B) again, followed by sudo apt update.
  • The browser shows a different version than php -v: the CLI and the web server are configured separately. Check the enabled Apache module (ls /etc/apache2/mods-enabled | grep php) or the fastcgi_pass socket in Nginx.
  • The browser downloads .php files instead of running them: Apache has no PHP module enabled, or Nginx has no PHP location block. Revisit Step 6.
  • 502 Bad Gateway with Nginx: the FPM service is stopped or the socket path is wrong. Check systemctl status php8.4-fpm and ls /run/php/.

Conclusion

You now have PHP running on Ubuntu 24.04: either Ubuntu’s own PHP 8.3, or any version from 8.5 down to 5.6 from the Ondřej Surý PPA, with extensions, a web server connection and the ability to switch versions. Keep PHP patched with regular sudo apt update && sudo apt upgrade runs, and move applications off end-of-life branches such as 8.1 and 7.4 as soon as you can.

Setting up a full stack? Follow Apache, MySQL and PHP-FPM on Ubuntu 24.04. If you are on the newer LTS, see how to install PHP 8.5 on Ubuntu 26.04.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

3 Comments

  1. Clearly you never tested this as ondrej’s repo doesn’t actually support php 7.4 on Ubuntu 24.04 yet.

Leave a Reply

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


Back to top