How to Install PHP 8.2 on Ubuntu 22.04 or 20.04

This guide will demonstrate how to install PHP 8.2 on Ubuntu 22.04 or 20.04 LTS releases using the command-line terminal with a third-party PPA maintained by Ondřej Surý.

PHP, a cornerstone of web development, continues to evolve, offering new features and improvements with each version. The release of PHP 8.2 is no exception, bringing a suite of enhancements that promise to bolster performance, improve code readability, and introduce more robust type systems. For developers and system administrators alike, understanding these updates and how to leverage them is essential for maintaining and developing modern web applications.

PHP 8.2 introduces several notable enhancements designed to streamline development workflows and enhance the security and functionality of applications:

  • Readonly Classes: Making immutable classes more straightforward and concise.
  • Disjunctive Normal Form (DNF) Types: Offering more flexibility in type declarations.
  • Stand-alone Types for null, false, and true: Improving type checking with dedicated types.
  • New “Random” Extension: Providing a more secure and object-oriented approach to generating random numbers.
  • Constants in Traits: Allowing traits to declare constants, enhancing their utility.
  • Deprecated Dynamic Properties: Encouraging better coding practices by deprecating unsanctioned dynamic properties.
  • New Classes, Interfaces, and Functions: Introducing new functionalities and attributes to advance PHP’s capabilities further.

These enhancements underscore PHP’s commitment to advancing web technology and highlight the importance of staying current with the latest development practices.

Let’s dive into how to install this latest version and unlock the full potential of your web development projects.

Import PHP 8.2 PPA on Ubuntu

Update Ubuntu Before PHP Installation

It’s crucial to start by updating your Ubuntu system. This step enhances security and ensures the smooth installation of new software. Refresh your package list and upgrade outdated packages with these commands:

sudo apt update
sudo apt upgrade

Install Initial Packages for PHP PPA on Ubuntu

Before adding the PHP PPA, install the necessary packages for secure package handling. These include tools for certificate authentication and software property management.

Run the following command:

sudo apt install ca-certificates apt-transport-https software-properties-common lsb-release -y

Add PHP 8.2 PPA on Ubuntu

Now, integrate the Ondřej Surý’s PHP PPA into your system. This repository provides the latest PHP versions, surpassing Ubuntu’s default offerings.

Import the repository with this command:

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

After adding the PPA, update the package cache to recognize the new source:

sudo apt update

Finally, upgrade any packages that need updating with the following:

sudo apt upgrade

Install PHP 8.2 on Ubuntu

Option 1: Install PHP as an Apache Module

For Apache HTTP server environments, PHP is typically installed as a module. To maintain a streamlined server, only install the specific PHP version required.

Install PHP 8.2 on Ubuntu as an Apache module using:

sudo apt install php8.2 libapache2-mod-php8.2

Post-installation, restart Apache to load the new PHP module:

sudo systemctl restart apache2

Option 2: Install Apache with PHP-FPM on Ubuntu

PHP-FPM offers enhanced performance for busy websites.

To install PHP 8.2 on Ubuntu with PHP-FPM for Apache:

sudo apt install php8.2-fpm libapache2-mod-fcgid

Enable PHP-FPM with:

sudo a2enmod proxy_fcgi setenvif
sudo a2enconf php8.2-fpm

Restart Apache to apply changes:

sudo systemctl restart apache2

Option 3: Install PHP 8.2 Support for Nginx

Nginx requires PHP-FPM to process PHP files. Install PHP 8.2 on Ubuntu and PHP-FPM for Nginx with:

sudo apt install php8.2 php8.2-fpm php8.2-cli

Start the PHP-FPM 8.2 service manually if it isn’t running:

sudo systemctl start php8.2-fpm

Configuring Nginx Server Block for PHP-FPM 8.2

Modify the Nginx server block to process PHP files. Add this to the relevant server blocks:

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

You can validate the syntax of your modified Nginx configuration using the following command:

sudo nginx -t

This command verifies the syntax of the configuration files and highlights any errors found. A successful output will look like this:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Once you’ve confirmed the syntax is correct, restart the Nginx service to apply the changes:

sudo systemctl restart nginx

As a final note, always ensure you check the version of PHP installed on your system. Use the following command to do this:

php --version

This command displays your system’s current PHP version, allowing you to confirm that your installation or upgrade was successful.

Terminal Confirmation of PHP 8.2 Successfully Installed on UbuntuPin
PHP 8.2 Successfully Installed

Install PHP 8.2 Extensions on Ubuntu

Install PHP 8.2 Extensions

Select extensions that align with your project’s requirements to customize PHP 8.2 on Ubuntu 22.04 or 20.04. This approach enhances performance and security.

Install the necessary extensions using the following:

sudo apt install php8.2-{cli,fpm,curl,mysqlnd,gd,opcache,zip,intl,common,bcmath,imagick,xmlrpc,readline,memcached,redis,mbstring,apcu,xml,dom,memcache}

Understanding PHP 8.2 Extensions

  • php-cli: Command Line Interface for running PHP scripts.
  • php-fpm: FastCGI Process Manager for efficient request handling.
  • php-curl: Enables communication with different server types.
  • php-mysqlnd: MySQL Native Driver for database connections.
  • php-gd: Graphics library for image manipulation.
  • php-opcache: Improves PHP performance by caching precompiled script bytecode.
  • php-zip: Manage zip file operations.
  • php-intl: Internationalization support for global character sets.
  • php-common: Common functionalities across multiple PHP modules.
  • php-bcmath: For precise floating-point arithmetic.
  • php-imagick: Image processing capabilities using ImageMagick.
  • php-xmlrpc: XML-RPC server and client functionalities.
  • php-readline: Interactive input reading from the terminal.
  • php-memcached & php-redis: Caching solutions for improved performance.
  • php-mbstring: Handling multibyte character encodings.
  • php-apcu: User caching to enhance PHP application performance.
  • php-xml & php-dom: XML parsing and manipulation.

To find additional modules, as the above is just a sample, use the search command as follows:

sudo apt search php8.2-
Terminal showing search for PHP 8.2 packages on UbuntuPin
Exploring Additional PHP 8.2 Packages on Ubuntu

Monitoring Installed PHP 8.2 Modules

Use php -m to view the loaded PHP modules. It’s advised to check and remove unused modules to maintain efficiency routinely.

php8.2 -m
Terminal listing installed PHP 8.2 modules on UbuntuPin
Displaying PHP 8.2 Modules in Ubuntu Terminal

Install PHP 8.2 Development Tools on Ubuntu

For PHP development and debugging:

sudo apt install php8.2-xdebug php8.2-pcov php8.2-dev

Concluding Thoughts

In conclusion, this guide has successfully equipped you with the steps to install PHP 8.2 on Ubuntu 22.04 or 20.04. The instructions provided cater to various server environments, whether configuring PHP as an Apache module, using PHP-FPM with Apache, or setting up PHP support for Nginx. Regularly update your system and PHP versions for security and performance benefits.

With these skills, you’re now ready to leverage the full potential of PHP 8.2 in your web development projects, ensuring a more robust and efficient experience.

Leave a Comment


Your Mastodon Instance
Share to...