Contact us
Servers 2 min read · 12/2022

Upgrading PHP on Ubuntu Including Already Installed Modules

A simple way to upgrade the PHP version on Ubuntu/Debian, including all modules that are already installed.

PHP 8.2 is out, and since PHP is still the foundation of how we build websites, why not give it a quick try. Thanks to deb.sury.org, it is available for Ubuntu and Debian almost immediately, so it's just a matter of running apt install. To avoid typing out the names of all the module packages I have installed for 8.1 by hand, I used the power of the Linux terminal, chained a few commands together and was done in no time:

bash
dpkg -l | grep php8.1 | awk '{print $2}' | tr -s "\n" " " | sed -e "s/1/2/g" | xargs sudo apt install -y

What the individual parts do:

  • dpkg -l - lists all installed packages
  • grep php8.1 - filters only the packages that contain php8.1
  • awk '{print $2}' - the output has several columns and the php8.1 package name is in the second one
  • tr -s "\n" " " - the output is line-based, i.e. each package on a new line; this turns it into a single line by replacing the newline character with a space
  • sed -e "s/1/2/g" - when upgrading from 8.1 to 8.2, all we need to do is replace 1 with 2 in the package names
  • xargs sudo apt install -y - xargs passes the resulting line as an argument to the apt install command with the required sudo, and the -y option automatically confirms all changes

For distributions other than Debian and Ubuntu clones, the command would most likely need substantial changes. But it can certainly serve as inspiration

Share
LinkedIn Facebook

Need more than an article?

Get in touch with no obligation – a few sentences about what you are dealing with are enough.

Read on

Similar articles

More notes from operations and development.

All advice