Legacy projects can be upgraded in 2 directions - PHP-wise and infrastructure-wise. We can get a new Ubuntu 25 but still have to run old PHP.
The same goes for upgrading a legacy project. Locally, we use Ubuntu 25, but we work on a project that uses PHP 7.2.
By default, Ubuntu releases ship only one PHP version. Often we need one of the other ~10 versions. How do we run old or new PHP on the latest Ubuntu?
This post extends reply on Github issue, to give it more visibility. Based on comments and SO struggles, many developers give up after reading too many misleading solutions:
Don't fret. There is a way.
Disclaimer: do this cautiously, preferably on a local dev machine rather than production, to keep the risk of conflicts low.
A typical solution is to add an external repository with all the old PHP versions. This repository is maintained by Ondrej Sury, who has maintained it since the beginning of PHP times.
sudo add-apt-repository ppa:ondrej/php
sudo apt install ppa-purge
sudo ppa-purge ppa:ondrej/php
Then reload dependencies:
sudo apt-get update
sudo apt-get upgrade
And we can install our desired PHP version:
sudo apt-get install php7.4 php7.4-curl
This worked very well for years. Until 2021.
Maintaining these packages comes at a great cost. PHP is released once a year. Ubuntu has 2-3 releases a year, this adds to maintenance cost and time, adding to server costs.
We sponsored (PHP 8.1 release)[https://github.com/oerdnj/deb.sury.org/issues/1439#issuecomment-705552989] to help a bit:
But it wasn't enough. There is still a way to sponsor Ondrej if you want to help out regularly.
This and probably other reasons lead to narrowing support only to Ubuntu LTS releases.
That means only once every 2 years, without the x.10 improved version:
If you're working on an upgrade, downgrading the server would be a bit counterproductive. In the same way, using old PHP and waiting for 2 years to use another outdated PHP version would not help.
So how do we get the PHP version we want without being limited by the LTS Ubuntu release cycle?
If we try to install e.g. PHP 8.2 on Ubuntu 25, we get the following error:
sudo apt-get install php8.2
E: Package 'php-82' has no installation candidate
We already added the repository above, but still get errors.
Let's pretend we use the LTS Ubuntu 24.04:
# edit sources file
nano /etc/apt/sources.list.d/ondrej-ubuntu-php-plucky.sources
Change our codename:
-suites: x
+suites: noble
Save, and update packages:
sudo apt-get update
Now, let's give it a try:
sudo apt-get install php8.2
...
Get:1 https://ppa.launchpadcontent.net/ondrej/php/ubuntu noble/main amd64 php8.2-amqp amd64 2.1.2-5+ubuntu24.04.1+deb.sury.org+1 [59.1 kB]
Selecting previously unselected package php8.2.
(Reading database ... 276942 files and directories currently installed.)
Preparing to unpack .../php8.2.1.2-5+ubuntu24.04.1+deb.sury.org+1_amd64.deb ...
Unpacking php8.2 (2.1.2-5+ubuntu24.04.1+deb.sury.org+1) ...
Setting up php8.2 (2.1.2-5+ubuntu24.04.1+deb.sury.org+1) ...
Package installs successfully, yay!
That's it. Now we have PHP 8.2 installed on our local Ubuntu 25. Once PHP 8.5 is out, we can it too.
Happy coding!