How To Install the Latest PHP Version on Ubuntu

Purchasing the Samsung Galaxy S7 had an unforeseen consequence: My A Picture A Day code stopped pulling GPS coordinates from the EXIF data embedded in the pictures I took with the phone. I knew the GPS data was there somewhere, because every EXIF reader I tried showed it - I just couldn’t get my PHP code to extract the data.

A bit of research on the interwebs revealed that the missing GPS data was caused by a bug in PHP, and the good news was that the bug was fixed in PHP 7.0.10. The bad news was that Ubuntu 16.04 is stuck on PHP 7.0.8.

This is the flip-side to using Ubuntu. APT and the Ubuntu packages make it very easy to install new software. Compiling, dependencies, and standard configuration are handled for you, but you’re also at the mercy of the package maintainers, and how quickly they compile new packages. In the case of PHP, this means Ubuntu 16.04 LTS users currently are locked to PHP 7.0.8, lagging four patch versions behind the official packages available from PHP.net.

Surý to the Rescue

Thankfully, there are zealous volunteers who compile up-to-date PHP packages for Ubuntu. In this case, we’ll use packages compiled by Ondřej Surý to help us out.

Warning: As always when I give away free advice, I have to point out that you’re following them at your own risk. If your Ubuntu box catches fire when following the instructions below, you have only yourself to blame. Think about it: You’re following advice you got from some random guy on the internet. That’s not the best idea ever, is it?

Run the following commands to register Ondřej Surý’s PPA repository, update your local package list, and install the latest and greatest version of PHP:

# Install add-apt-repository.
sudo apt-get install software-properties-common
# Add Surý's PPA repository.
sudo add-apt-repository ppa:ondrej/php
# Update your local package list.
sudo apt-get update
# Upgrade PHP.
sudo apt-get upgrade
# Restart Apache2 to make sure the new PHP packages are used.
sudo service apache2 restart

When running the “upgrade” command, APT might tell you that a lot of the PHP packages are being held back. If that’s the case, run the following commands:

# Force install of held back PHP packages
sudo apt-get install COPY-LIST-OF-HELD-BACK-PHP-PACKAGES-HERE
# Restart Apache2 to make sure the new PHP packages are used.
sudo service apache2 restart

There you go. Enjoy the latest version of PHP.


Feedback

Do you have any thoughts you want to share? A question, maybe? Or is something in this post just plainly wrong? Then please send an e-mail to vegard at vegard dot net with your input. You can also use any of the other points of contact listed on the About page.

Caution

It looks like you're using Google's Chrome browser, which records everything you do on the internet. Personally identifiable and sensitive information about you is then sold to the highest bidder, making you a part of surveillance capitalism.

The Contra Chrome comic explains why this is bad, and why you should use another browser.