Chris Moore
play

Upgrade PHP on Ubuntu 16.04 and 18.04

Coloured lines of code on a screen

Ubuntu and it's family of Linux Operating Systems make great web servers but sometimes the official software packages that install by default using apt fall behind. This article will walk you through the process of fixing this limitation for PHP.

Add Ondrejs PPA Repo

PPA's or Personal Package Archive, are a collection of software not included in Ubuntu by default. Typically these repositories focus on a single program, but they can include more depending on the person maintaining them. Ondřej Surý has created this PPA which has the latest versions of PHP in it. Lets add it to your system, and update to get a list of all the software we can install.

$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt-get update

Remove PHP 7.0

You can find out if you have a preinstalled version of PHP that you need to remove by running:

$ php -v

To remove our old versions of PHP, run the following command - just change the version number to the version currently on your server:

$ sudo apt-get purge php7.0 php7.0-common

Once you have removed the old version we can replace it with PHP 7.2 in this case (you can add any currently supported version by changing the version number).

Install PHP 7.2

sudo apt install php7.2 php7.2-common php7.2-cli php7.2-fpm php7.2-mysql php7.2-xml php7.2-curl

Other PHP Packages

To install Laravel, we also need other packages - run this command to install:

$ sudo apt-get install php7.2-curl php7.2-xml php7.2-zip php7.2-gd php7.2-mysql php7.2-mbstring

Check that it Worked

Lets finish by restarting our server!

$ sudo reboot

Once we get back into your server, let's confirm the php version:

$ php -v

After doing this on mine - I get:

PHP 7.2.23-1+ubuntu18.04.1+deb.sury.org+1 (cli) (built: Oct  8 2019 05:32:02) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.23-1+ubuntu18.04.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

Add Reference to Nginx Server Block (optional)

Finally, if you use Nginx, you'll want to add a reference location to your server block to let the website know where to find the correct PHP installation.


location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        }