0

I generally upgrade my servers with the following command:

sudo -- sh -c 'apt-get update; apt-get upgrade -y; apt-get dist-upgrade -y; apt-get autoremove -y; apt-get autoclean -y'

Is there a clean way to upgrade everything except for PHP, or would it be easier just to upgrade everything and then rollback to PHP 8.2? I learned the hard way with a recent upgrade that WordPress isn't ready for PHP 8.3 (https://make.wordpress.org/core/handbook/references/php-compatibility-and-wordpress-versions/), and had to do a complete fresh install of PHP:

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

sudo apt purge php* sudo apt autoremove sudo rm -rf /etc/php/8.3 sudo rm -rf /usr/lib/php

sudo apt install php php-mysql sudo service apache2 restart

Note: Ubuntu moved to PHP 8.3 on January 18th: https://discourse.ubuntu.com/t/noble-numbat-release-schedule/35649

  • You indicate you're using Ubuntu 22.04 (jammy) so why the link that relates to what will be Ubuntu 24.04 LTS (ie. noble) that isn't expected to be released until April 2024? That final link you give doesn't appear to relate to your jammy system that I can see in your question. – guiverc Feb 02 '24 at 04:47
  • 3
  • @guiverc — I agree; Ubuntu release notes are a little confusing at times. It's the only mention of PHP 8.3 I could find, but I can assure you, my Ubuntu 22.04 server automatically upgraded to PHP 8.3 just by doing general updates/upgrades with the command above (not explicitly and manually upgrading my PHP version). – ABuntToo Feb 02 '24 at 05:06
  • @Daniel T — At a glance, no, it doesn't look like it helps with my specific issue, but I'll dig in further to see if it can help. – ABuntToo Feb 02 '24 at 05:07
  • For your specific issue, try sudo apt-mark hold php – Daniel T Feb 02 '24 at 05:11
  • @Daniel T — Do you know the nuances and possible issues this might cause? Will it hold PHP at its exact version, meaning 8.2.*, or will it just hold it at the major version of 8.2, but otherwise update for minor releases within the major version? Altogether, would this work to try it out?: sudo -- sh -c 'apt-get update; apt-get upgrade -y; apt-get dist-upgrade -y; apt-get autoremove -y; apt-get autoclean -y; apt-mark hold php' – ABuntToo Feb 02 '24 at 05:16
  • 1
    It will hold it at the exact version you have installed at the time you run the command. It is counterproductive to run upgrades and dist-upgrades before running apt-mark. If you want a specific version, you need to run apt-get install php=<the version you want>; apt-mark hold php. I used apt-mark on a random package (tar) in a fresh install, and it held back the whole major.minor.patch version, including the patch version, not just the major or minor version. – Daniel T Feb 02 '24 at 05:20
  • 1
    @Daniel T — Okay, I think the info you've provided is the best possible answer to this question. Go ahead and answer and I'll mark it as correct. – ABuntToo Feb 02 '24 at 05:24
  • I see only php | 2:8.1+92ubuntu1 | jammy | all for PHP & jammy, but apt will upgrade using whatever sources you've added to your system, and if that's 3rd party sources added, those will get used, so I'd check where you got it from (ie. apt policy etc). apt mark works on a package level (not version level). If you want to control updates; I suggest avoiding use of -y which gives permission before you've read what you're agreeing to upgrade. I'm a desktop user, so don't retain what I read on ML's in regards PHP upgrade/intentions & dates etc.. – guiverc Feb 02 '24 at 05:33
  • Added an answer despite the linked canonical answer, so that we can include the additional info about the PPA required for the php version, clarify non-semver nature, and correct the order that the commands are run – Daniel T Feb 02 '24 at 05:43

1 Answers1

1

You need to use apt-mark hold to pin the php version before you do your upgrade. The php 8.3 version that meets your requirements is 2:8.3+94+ubuntu22.04.1+deb.sury.org+2. The modified list of steps you want to do is the following:

# Your original cleaning up of the wrong version
sudo add-apt-repository --remove ppa:ondrej/php
sudo apt purge php*
sudo apt autoremove
sudo rm -rf /etc/php/8.3
sudo rm -rf /usr/lib/php

I do not recommend removing ppa:ondrej/php .

Ubuntu 22.04 has php 8.1, and you need the ppa for php 8.3

sudo add-apt-repository ppa:ondrej/php # Readd

Install and pin the correct version

sudo apt install php=2:8.3+94+ubuntu22.04.1+deb.sury.org+2 php-mysql sudo apt-mark php # Prevent apt-get {,dist-}upgrade from changing the version. sudo service apache2 restart

Later, routine upgrades will not touch the php version

They will show it below the "The following packages have been kept back:" line

apt-get update apt-get upgrade -y apt-get dist-upgrade -y apt-get autoremove -y apt-get autoclean -y

Take care to avoid running these steps in the wrong order. It is counterproductive to run upgrades and dist-upgrades before running apt-mark. That would just pin the latest version available at the time of that command, not version 8.3 like you want. You need to run apt-mark either immediately after apt-get install php=<the version you want> or after verifying the installed version is already the version you want.

This will hold back the package at the exact major.minor.patch version, including the patch version, not just major or minor.

Daniel T
  • 4,594
  • 1
    Thank you. If anyone else runs into this issue, note that WordPress 6.5 is set to release on March 26th, which will be adding better support for PHP 8.3, if you want to unhold PHP upgrades at that time. sudo apt-mark unhold php – ABuntToo Feb 02 '24 at 05:56