32

I am running a WordPress website over LEMP stack server on Ubuntu 18.04 on DigitalOcean. WordPress is recommending me to update my PHP to the latest version.

I am running PHP 7.2.17; Please tell me the safest way to update my PHP to the latest version PHP 7.3.

3
  • Note that if you run a supported Ubuntu distribution (e.g. 16.04, 18.04, 18.10 or 19.04), security updates will be backkported to the PHP you're running, so unless you require new features not available in PHP7.2, there's really no reason to upgrade
    – vidarlo
    May 25, 2019 at 11:14
  • I don't need any extra feature. I always look for a new method to optimize my site performance. And, I am aware that updating PHP is a great move to improve your site performance. Thank you! May 25, 2019 at 11:26
  • I ask a follow up question in Should I add-apt-repository ppa:ondrej/apache2 when I add-apt-repository ppa:ondrej/php, as advised? which is essentially about whether it's necessary to add ppa:ondrej/apache2 or ppa:ondrej/nginx in order to completely upgrade PHP, if it is used in conjunction with the operating web server. Mar 20, 2022 at 12:27

2 Answers 2

50

Open the terminal and type:

sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install php7.3 # The latest version in this repo for 18.04 and 20.04 is currently php7.4.  

The latest version in ppa:ondrej/php for 22.04 is currently php8.2 but php7.4 is also still supported.

php7.3 will be updated automatically when updates become available. Alternatively you can build php7.3 from source in Ubuntu 18.04, but it will not be updated automatically, and it also takes more than an hour to build if you want to test it too.

After installing php7.3 you can show php7.3 modules that can also be installed by running the following command:

apt-cache search php7.3 | grep php7.3  

A quick one-line command to install the same packages on php7.3 as php7.2:

sudo apt install $(apt list --installed | grep php7.2- | cut -d'/' -f1 | sed -e 's/7.2/7.3/g') 
10
  • 1
    It works, May i know the source of this repository? According to your answer after adding this repository, php7.3 or any newer version that will come in future will be updated automatically when updates become available. But i want to update it manually because, sometime due to compatibility issue it can break site. So, please guide me to update it manually whenever i want. May 25, 2019 at 14:02
  • 2
    @karel, please do not add PPA deb-lines blindly, add them with their pages - such as for this case launchpad.net/~ondrej/+archive/ubuntu/php .
    – N0rbert
    May 25, 2019 at 14:04
  • @TechieBraj See How to prevent updating of a specific package? and then just the same as to prevent a package from being updated with sudo apt-mark hold <package-name> you can reverse the process so that the package can be updated again with sudo apt-mark unhold <package-name>
    – karel
    May 25, 2019 at 14:16
  • 3
    Another quick liner to completely remove old php7.2 packages: sudo apt purge $(apt list --installed | grep php7.2- | cut -d'/' -f1)
    – Stalinko
    Mar 4, 2020 at 13:57
  • 1
    And here how to remove warning from apt list output: sh $(apt list --installed 2>/dev/null | grep php7.3- | cut -d'/' -f1 | sed -e 's/7.2/7.3/g' )
    – Searge
    Dec 27, 2022 at 10:14
3

From @karel's answer I replace the first command from sudo add-apt-repository ppa:ondrej/php to sudo apt-add-repository ppa:ondrej/php and it works for me. So finally I run:

sudo apt-add-repository ppa:ondrej/php    
sudo apt-get update
sudo apt-get install php7.3

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .