How to Install PHP 8.2 on Ubuntu 24.04

Choose a different version or distribution

Introduction

Before we begin talking about how to install PHP 8.2 on Ubuntu 24.04, let's briefly understand – What is PHP 8.2?

PHP 8.2 is a popular scripting language used for web development. It comes with new features and improvements that enhance performance and developer productivity. With PHP 8.2, you can expect better error handling, improved type system, enhanced language consistency, and increased performance.

This version brings enhancements to the language that make it easier for developers to write clean and efficient code. Developers can leverage these new features to create faster, more secure, and scalable web applications. Upgrade to PHP 8.2 to take advantage of these improvements and stay up-to-date with the latest advancements in web development technology.

In this tutorial, you will install PHP 8.2 on Ubuntu 24.04. We will also address a few FAQs on how to install PHP 8.2 on Ubuntu 24.04.

Advantages of PHP 8.2

  1. Improved error handling: PHP 8.2 offers better error reporting and handling mechanisms.
  2. Enhanced performance: Increased speed and efficiency for faster web application execution.
  3. Type system enhancements: Improved type checking for more robust code.
  4. Language consistency: Ensures a more coherent and predictable programming experience.
  5. Increased productivity: New features streamline development, making coding more efficient.

Prerequisites

  • A system with Ubuntu 24.04
  • sudo privileges
  • Internet Connection

Step 1: Update Your System

Before starting the installation, it’s advisable to update your package list to ensure you receive the latest versions of software and dependencies.

sudo apt update
sudo apt upgrade -y

Step 2: Install Software Properties Common

Ubuntu 24.04 usually includes the software-properties-common package, but if it’s not installed, you can add it with the following command. This package offers the scripts needed for adding repositories to your system.

sudo apt install software-properties-common -y

Step 3: Add PHP Repository

To access the latest PHP versions, including 8.3, 8.2, and 7.4, it is recommended to add a third-party repository such as the Ondřej Surý PPA, which is well-regarded in the Ubuntu community. This repository offers updated PHP Debian packages for active LTS releases.

To add the PHP repository to your system, use the following commands:

sudo add-apt-repository ppa:ondrej/php -y
sudo apt update

Step 4: Install PHP

Once the repository has been added to your system, you can continue with the installation of PHP and the required modules.

sudo apt install php8.2 -y

Step 5: Verify Installation

After completing the installation, you can confirm that PHP is properly installed by checking its version.

php -v

This command will display the PHP version you installed, along with some additional details. If you have multiple PHP versions installed, it will show the default PHP version. To view other PHP versions, use commands such as php8.3 -v, php8.2 -v, and so on.

Step 6: Install PHP Extensions

PHP extensions add or improve functionalities in your PHP installation. For your projects, you might require specific extensions. To install an extension, use the following command, substituting extension_name with the extension's name and phpX.X with your PHP version (e.g., php8.2).

sudo apt install phpX.X-extension_name

For example, to install the MySQL extension for PHP 8.2, use the following command:

sudo apt install php8.2-mysql

Step 7: Install PHP Composer (Optional)

Composer is a tool for managing dependencies in PHP applications. While not mandatory for every project, it is highly useful for developers who want to optimize their workflow and ensure that project dependencies are properly installed and managed. If necessary, you can install Composer by executing the following commands.

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php
php -r "unlink('composer-setup.php');"

Step 8: Configure PHP (Optional)

Optimizing PHP settings can improve your project's performance. The primary configuration file, php.ini, is located in specific directories depending on your setup. You can identify the correct PHP version as follows:

  • Apache: /etc/php/X.X/apache/php.ini
  • CLI: /etc/php/X.X/cli/php.ini
  • FPM: /etc/php/X.X/fpm/php.ini

Be sure to replace ‘X.X’ with your PHP version. FPM is typically used with Nginx and occasionally with Apache for applications using non-default PHP versions.

FAQs to Install PHP 8.2 on Ubuntu 24.04

How do I increase the upload file size limit?

In the same php.ini file, find and change: upload_max_filesize = 20M post_max_size = 20M.

How do I enable a PHP module?

Use the command: sudo phpenmod <module_name>

How do I disable a PHP module?

Use the command: sudo phpdismod <module_name>

Where can I find the PHP configuration file?

The main configuration file is located at:text/etc/php/8.2/apache2/php.ini

What is the default web root directory?

The default web root directory for Apache is usually:text/var/www/html

How do I check installed PHP modules?

Run: php -m This will list all installed modules.

Conclusion

We hope this tutorial helped you understand how to install PHP 8.2 on Ubuntu 24.04.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.