How to install PHP 8.4 on Ubuntu 24.04
Add the Ondřej Surý PPA repository to your Ubuntu 24.04 system to access PHP 8.4 packages. Follow these steps to install the latest PHP version with essential extensions like MySQLi, PDO, and cURL.
Ubuntu 24.04 ships with PHP 8.3 by default. You must add an external repository to install PHP 8.4. This guide shows you how to configure the system to fetch the latest stable release and install required extensions.
Prerequisites
- Ubuntu 24.04 LTS (Noble Numbat) running as root or via sudo.
- Internet connection to download packages.
- At least 500 MB of free disk space for the PHP binaries and extensions.
Step 1: Update the package index
Refresh your local package database to ensure you have the latest metadata before adding new sources.
sudo apt update
You will see a list of packages to be upgraded or fetched. Ignore the output for now and proceed to add the repository.
Step 2: Add the Ondřej Surý PPA
Add the official PHP repository maintained by Ondřej Surý. This repository contains PHP 8.4 and backports for older versions.
sudo add-apt-repository ppa:ondrej/php
Press Enter to confirm the addition of the new source list.
Step 3: Update the package index again
Refresh the package index to include the new repository you just added.
sudo apt update
Wait for the process to complete. You will see output listing packages from the new repository.
Step 4: Install PHP 8.4
Install the core PHP package. This command pulls the latest 8.4.x release available in the repository.
sudo apt install php8.4 -y
Wait for the installation to finish. The output will show the download progress and the list of installed files.
Step 5: Install essential PHP extensions
Install common extensions required for web development and database connectivity. Run the following command to install MySQLi, PDO, and cURL.
sudo apt install php8.4-mysql php8.4-pdo php8.4-curl php8.4-xml php8.4-zip -y
Review the list of extensions you need. Add any other modules like GD, Mbstring, or Intl to the command if your application requires them.
Step 6: Install Composer
Install Composer to manage PHP dependencies for your projects. This tool is essential for modern PHP development.
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer
Wait for the installation to complete. You will see a success message confirming Composer is installed.
Verify the installation
Check the installed PHP version to confirm you have 8.4. Run the version command in your terminal.
php -v
You should see output similar to this:
PHP 8.4.0 (cli) (built: ...)
Copyright (c) The PHP Group
Zend Engine v4.4.0, Copyright (c) Zend Technologies
Check which PHP binary is active on your system. This ensures web servers like Apache or Nginx use the correct version.
which php
The output should point to /usr/bin/php8.4 or a similar path depending on your configuration.
Troubleshooting
If you encounter an error saying "package php8.4 is not available," verify that the repository is enabled. Run the following command to list enabled sources.
cat /etc/apt/sources.list.d/ondrej-php.list
If the file is missing, the PPA addition failed. Re-run the add-apt-repository command in Step 2.
If you see an error about a missing dependency like "libxml2-dev," install the missing library first. Run this command to fix broken dependencies.
sudo apt install libxml2-dev -y
If the PHP binary is not found when running php -v, check the PATH environment variable. Add /usr/bin to your PATH if necessary. Edit your .bashrc file to include the path.
echo 'export PATH=/usr/bin:$PATH' >> ~/.bashrc
Source the file to apply changes immediately. Run source ~/.bashrc to reload the environment variables.
If you need to switch between PHP versions, use the phpenmod command to enable or disable specific modules. However, for PHP 8.4, the version is managed by the package manager. Do not mix different PHP versions on the same system unless you use containers.
Ensure your web server configuration points to the correct PHP version. If using Apache, check the mod_php configuration. If using Nginx, ensure the PHP-FPM socket points to the PHP 8.4 service. Restart the web server after changes.
sudo systemctl restart apache2
or
sudo systemctl restart nginx
Restart the PHP-FPM service to apply changes to the runtime environment.
sudo systemctl restart php8.4-fpm
Verify the service is running with systemctl status php8.4-fpm. Check the logs at /var/log/php8.4-fpm.log for errors if the service fails to start.