How to change PHP version in Ubuntu 26.04 - Resolute Raccoon

Introduction

After upgrading my Ubuntu 26.04 I would automatically have my PHP version switched to PHP 8.5. The problem is Ubuntu 26.04 only comes with PHP 8.5 when I needed to use PHP 8.4 so the solution is to install a repository that grants access to other versions of PHP.

Remove ondrej repository

Skip this step if you didn’t install the ondrej repository previously as it needs to be removed, this is because the library has been moved to a different repository.

Run the following command to remove the ondrej/php repository.

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

Install PHP repository

Firstly before adding the new repository update your system.

sudo apt -y update

Install the following libraries which are required to install the PHP repository.

  • lsb-release
  • ca-certificates
  • curl
sudo apt -y install lsb-release ca-certificates curl

Download the packages.sury.org key required for the PHP repository.

sudo curl -sSLo /tmp/debsuryorg-archive-keyring.deb https://packages.sury.org/debsuryorg-archive-keyring.deb

Install the downloaded key file.

sudo dpkg -i /tmp/debsuryorg-archive-keyring.deb

Use the installed key to add the PHP repository.

sudo sh -c 'echo "deb [signed-by=/usr/share/keyrings/debsuryorg-archive-keyring.gpg] https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'

Finally update and upgrade your system.

sudo apt -y update && sudo apt -y upgrade

Change PHP version

Now I will have the ability to install other versions of PHP.

Here is how I installed PHP 8.4 and the required PHP modules needed for my web development work.

sudo apt install -y php8.4 php8.4-bcmath php8.4-curl php8.4-zip php8.4-gd php8.4-mbstring php8.4-gmp php8.4-dom php8.4-mysql php8.4-dev

Now switch the default PHP version to the PHP 8.4 version.

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

Run the PHP version to verify that 8.4 is indeed the current one being used.

php -v

References

Thanks to jmflores for the solution to install PHP versions in Ubuntu 26.04 - Resolute Raccoon.

https://codeberg.org/oerdnj/deb.sury.org/issues/91