How do I upgrade PHP7.2 to PHP7.3 or PHP7.4 in Ubuntu 18.04 LTS
I am trying to upgrade my PHP, but Ubuntu does not provide packages for newer versions of PHP for Ubuntu 18.04 LTS.
To understand the problem better, it helps to keep in mind that a Linux Distribution such as Ubuntu or Debian strives to be a cohesive set of packages that work together. Upgrading software can break the finely tuned interdependencies provided by a Linux distribution. You therefore have four main options for upgrading software in a Linux environment:
You can compile and manually install the software yourself from the source. (this method requires knowledge of how software works together with its complex interdependencies and often requires upgrading other software that may in turn require upgrading yet even more software). Things can break easily when you do this and often requires a lot of time and effort to ensure that lots of moving parts are working together properly.
You can install an "official" updated package provided by your distribution. (Ubuntu does not provide an official package for PHP7.3 or PHP7.4 in the Ubuntu 18.04 LTS ecosystem)
You can use a third party package repository the tries to retrofit everything to make the newer version work with your current distribution version. The SURY PHP repository is commonly recommended if you have to upgrade your PHP while using an older Ubuntu or Debian distribution. This seems to be the best way to retrofit PHP 7.4 into an Ubuntu 18.04 system.
You can upgrade or do a fresh install of Ubuntu 20.04 which is built around PHP 7.4. Doing an in-place distribution upgrade is rarely a good idea because newer packages often break when trying to retain configurations written for older versions of the software which is being upgraded.
Doing a clean install of Ubuntu 20.04 LTS is by far the safest and easiest way to use PHP 7.4 on Ubuntu.
To use the SURY repository for Ubuntu, instructions will vary depending if you are using Apache or nginx as your web server. PHP upgrade packages can be found here:
Basically you will want to run the following as
# LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php # LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/apache2 # apt-get update # apt-get install php7.4
# LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php # LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/nginx-mainline # apt-get update # apt-get install php7.4
If you want to use MySQL with PHP, you will need to add the following:
# apt-get install php7.4-mysql
If you want to use PHP7.3 instead, just replace the version number in the instructions above.
To test this out, create a file called
/var/www/html/phpinfo.php and put the following text in it:
<?php phpinfo( ); ?>
You should now be able to browse your site, i.e. http://example.com/phpinfo.php and see a page showing detailed information about your PHP setup.