Upgrade to the latest PHP version in ubuntu 16.04

The default PHP version for Ubuntu 16.04 is 7.0. If you want to install a new version of PHP on 16.04 you need to use an additional PPA (as it is shown below) or you must compile it on your own.

My server with Ubuntu 16.04 currently uses PHP 7.2 and there are several operational instances of WordPress and MediaWiki. The web server is Apache 2.4. The commands that I've used to migrate from PHP 7.0 to 7.2 are:

# Add the repository 'ppa:ondrej/php'
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update

# Install PHP 7.2
sudo apt install -y php7.2 php7.2-cli libapache2-mod-php7.2
sudo apt install -y php-imagick php-gettext php-memcache php-apcu php-pear php-xml php-xmlrpc
sudo apt install -y php-memcached php-mysql php-intl php-mbstring php-curl php-gd php-imagick
sudo apt install -y php7.2-common php7.2-mysql php7.2-cgi 
sudo apt install -y php7.2-curl php7.2-zip php7.2-mbstring php7.2-xmlrpc php7.2-gd php7.2-xml php7.2-xsl
sudo apt install -y php7.2-dev php7.2-bz2 php7.2-intl php7.2-json php7.2-opcache php7.2-readline 
sudo apt install -y php7.2-imap php7.2-pspell php7.2-recode php7.2-sqlite3 php7.2-tidy php7.2-bcmath #php7.2-mcrypt

# Update the Apache's PHP version
sudo a2dismod php7.0
sudo a2enmod php7.2
sudo systemctl restart apache2.service

# Update the CLI PHP version
sudo update-alternatives --set php /usr/bin/php7.2

In the repository ppa:ondrej/php PHP 7.3 is also available, I think to migrate soon to this latest version.

If there is a problem with the public key of the repository, you can use this solution.

Here are the additional tweaks of my /etc/php/7.2/apache2/php.ini that are made according to few WordPress and MediaWiki manuals:

zlib.output_compression = On
max_execution_time = 600
max_input_vars = 3000
memory_limit = 512M
post_max_size = 256M
upload_max_filesize = 128M
allow_url_fopen = Off
pcre.backtrack_limit=1000000
session.cookie_secure = True
session.gc_maxlifetime = 14400
session.cache_expire = 540
mbstring.encoding_translation = On

Update 04.2019

I've used the above set of commands to install PHP 7.3 on Ubuntu 16.04 and everything went completely fine. Just replace 7.2 with 7.3 within these commands - the entire process took about 7 minutes.


Update 02.2020

I've migrated to PHP 7.4 and everything went well, during that process I've used the following steps (note, at this date NextCloud doesn't support php7.4 yet):

# Absolutely mandatory step
sudo apt update
# Generate list of php7.4 packages on tbe base of the installed 7.3 ones
# Remove 'echo' to install the packages
for pkg in $(sudo apt-cache policy *php7.3* | grep -P '^(lib.*|php)7.3.*:$' | sed -e 's/:$//' -e 's/7.3/7.4/');
do
    echo sudo apt install "$pkg" -y;
done 2>/tmp/php7.4.install.error.log
# Inspect the error log for some fatal errors, 
# Some old dependencies could missing - it's normal
cat /tmp/php7.4.install.error.log
# Mandatory step; If there is a trouble, tray: sudo apt install -f
sudo apt update && sudo apt upgrade
# Create a backup copy of the new php.ini 
sudo cp /etc/php/7.4/apache2/php.ini{,.default}
# Compare the old and the new php.ini files
# Apply the necessary changes to 7.4/apache2/php.ini
# Compare the files again in order to inspect for typos 
colordiff --side-by-side --left-column --width=240 --show-c-function /etc/php/7.{3,4}/apache2/php.ini | less -R
# Switch the version within Apache
sudo a2dismod php7.3 && sudo a2enmod php7.4
sudo systemctl restart apache2
# Switch the CLI version
sudo update-alternatives --set php /usr/bin/php7.4

Update 06.2020

Now I'm using Ubuntu 20.04 Server, it comes by default with php7.4, but I need php7.3. The method above, by using ppa:ondrej/php, solves my issue.


You can install a newer, manually. But in general, you don't want to do that, because you have to manually download updates and build again.

The point of LTS releases is stability. You know that the PHP version shipped today in 18.04 will be maintained with security updates for five years, without having to upgrade the version - which can lead to problems with programs that depends on current version.

18.04 has PHP 7.2 available. It may be a good upgrade path from 16.04, as both is LTS releases which has a five year support period.

Regarding security, don't rely on the version for this. Ubuntu maintainers and Debian maintainers backport security patches from upstream into supported packages for those five years.