, , , ,

How to Upgrade PHP 8.1 to PHP 8.2 on Ubuntu: A Step-by-Step Guide

Posted by

Certainly! Here’s a comprehensive guide on how to upgrade PHP from version 8.1 to 8.2 on an Ubuntu system

Upgrading PHP to the latest version is crucial for ensuring that your applications run smoothly, securely, and with access to the latest features and performance improvements. If you are currently running PHP 8.1 on your Ubuntu system and wish to upgrade to PHP 8.2, this guide will walk you through the entire process step-by-step.

Why Upgrade to PHP 8.2?

PHP 8.2 brings several new features, enhancements, and deprecations that make it a worthwhile upgrade. Notable features include readonly classes, better type checks, improved performance, and various quality-of-life improvements for developers. Staying up-to-date with the latest PHP version ensures that your applications benefit from these advancements while maintaining security standards.

Prerequisites

Before starting the upgrade process, ensure you have the following:

  • A system running Ubuntu (18.04, 20.04, or later).
  • Sudo privileges on your user account.
  • PHP 8.1 installed on your system.
  • A backup of your server or application in case something goes wrong during the upgrade.

Step 1: Add the PHP PPA Repository

First, add the ondrej/php PPA repository, which provides the latest PHP versions. If you’ve previously added this PPA, you can skip this step.

Note: You have a question in your mind that how do I check whether this PPA is added or not? Don’t worry with the solution below

To check if the ondrej/php PPA repository is already added to your Ubuntu system, you can follow these steps:
Step 1: List All Added PPAs
You can list all the PPAs added to your system by running the following command in the terminal:
grep -r "ondrej/php" /etc/apt/sources.list* /etc/apt/sources.list.d/
Step 2: Check the Output
If the PPA is installed: The command will return one or more lines indicating that the ondrej/php PPA is present. The output will look something like this:
/etc/apt/sources.list.d/ondrej-ubuntu-php-focal.list:deb http://ppa.launchpad.net/ondrej/php/ubuntu focal main
If the PPA is not installed: The command will return nothing, meaning the PPA is not present on your system.
Step 3: Verify the PPA
If you find the ondrej/php PPA in the list, you can further verify its content by running:
cat /etc/apt/sources.list.d/ondrej-ubuntu-php-*.list
This command will display the actual lines in the PPA list, ensuring that the repository is correctly set up.
Step 4: Update Package List (Optional)
If the PPA is installed, you can update your package list to make sure it’s using the latest packages from that PPA:
sudo apt-get update
This will refresh your package database with information from all available repositories, including ondrej/php.
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update

This step ensures that your package manager can access the latest PHP packages.

Step 2: Install PHP 8.2

With the PPA repository added, you can now install PHP 8.2. This step includes installing the core PHP package and any additional extensions your application might require.

sudo apt-get install php8.2 php8.2-cli php8.2-fpm php8.2-mysql php8.2-xml php8.2-curl php8.2-mbstring php8.2-zip php8.2-gd php8.2-intl php8.2-soap php8.2-bcmath php8.2-xdebug

Replace or add extensions based on the specific needs of your application.

Step 3: Disable PHP 8.1 and Enable PHP 8.2

To ensure that your web server and command-line interface use PHP 8.2 instead of PHP 8.1, you’ll need to disable the older version and enable the new one:

sudo a2dismod php8.1
sudo a2enmod php8.2

These commands disable PHP 8.1 and enable PHP 8.2 in the Apache web server.

Step 4: Update the Alternatives System

The alternatives system in Ubuntu allows you to manage multiple versions of a program on your system. To set PHP 8.2 as the default version, run the following commands:

sudo update-alternatives --set php /usr/bin/php8.2
sudo update-alternatives --set phpize /usr/bin/phpize8.2
sudo update-alternatives --set php-config /usr/bin/php-config8.2

These commands ensure that when you run php, phpize, or php-config from the command line, the system will use PHP 8.2.

Step 5: Restart Your Web Server

After making these changes, restart your web server to apply them:

  • For Apache:
  sudo /opt/lampp/lampp restart
  • For Nginx (if you’re using PHP-FPM):
  sudo systemctl restart php8.2-fpm
  sudo systemctl restart nginx

Restarting the web server ensures that it starts using PHP 8.2 for processing requests.

Step 6: Verify the PHP Version

To confirm that PHP 8.2 is installed and set as the default version, run:

php -v

You should see output indicating that PHP 8.2 is now active:

PHP 8.2.x (cli) (built: ...)

Step 7: Test Your Application

Upgrading to a new PHP version can sometimes cause compatibility issues with existing applications. It’s important to thoroughly test your application to ensure that everything functions as expected under PHP 8.2.

  • Check your application’s error logs for any warnings or errors.
  • Test all critical functionality, including database connections, file uploads, and third-party integrations.
  • Ensure that all PHP extensions required by your application are installed and properly configured.

Conclusion

Upgrading to PHP 8.2 on Ubuntu is a straightforward process, but it’s important to proceed carefully to avoid disrupting your application. By following this guide, you can ensure that your system benefits from the latest features and performance improvements that PHP 8.2 has to offer. Remember to back up your data before starting the upgrade and thoroughly test your application afterward.

With PHP 8.2, you can take advantage of the latest advancements in PHP development, ensuring that your applications remain secure, fast, and feature-rich. Enjoy the new features and improved performance that come with this upgrade!

This article should serve as a comprehensive guide for anyone looking to upgrade their PHP version on an Ubuntu system.

5 3 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x