Upgrade PHP on Ubuntu

This guide will not cover Ubuntu 12.04 at this time as the PPA from ondrej appears to upgrade Apache 2.2 to 2.4, and I have not been able to install this cleanly. If PHP 5.5 or 5.6 is needed, I am going to have to suggest migrating to Ubuntu 14.04 for the time being.

The version of PHP that ships with Ubuntu 14.04 is PHP 5.5, which is starting to get a bit outdated. Oftentimes, people will want to use a newer version of PHP, such as PHP 5.6 or PHP 7.0. This is easy enough to do, however you should always test this out on a dev server before applying to production just in case you run into problems.

On Ubuntu, it looks like the preferred method is to use the PPA from ondrej. So to get started, first update your existing repos, then add the new PPA:

# Update PHP 5.5 on Ubuntu 14.04 to PHP 5.6
[root@web01 ~]# apt-get -y update
[root@web01 ~]# apt-get install software-properties-common
[root@web01 ~]# add-apt-repository ppa:ondrej/php
[root@web01 ~]# apt-get -y update
[root@web01 ~]# apt-get -y install php5.6 php5.6-cli php5.6-mysql php5.6-mcrypt php5.6-mbstring php5.6-curl php5.6-gd php5.6-intl php5.6-xsl php5.6-zip
[root@web01 ~]# a2dismod php5
[root@web01 ~]# a2enmod php5.6
[root@web01 ~]# service apache2 reload

# Update PHP 5.5 on Ubuntu 14.04 to PHP 7.0
[root@web01 ~]# apt-get -y update
[root@web01 ~]# apt-get install software-properties-common
[root@web01 ~]# add-apt-repository ppa:ondrej/php
[root@web01 ~]# apt-get -y update
[root@web01 ~]# apt-get -y install php7.0 php7.0-cli php7.0-mysql php7.0-mcrypt php7.0-mbstring php7.0-curl php7.0-gd php7.0-intl php7.0-xsl php7.0-zip
[root@web01 ~]# a2dismod php5
[root@web01 ~]# a2enmod php7.0
[root@web01 ~]# service apache2 restart

Perhaps you find that your application doesn’t work with PHP 5.6 or PHP 7.0, so you want to roll back to stock PHP 5.5 instead:

# Downgrade PHP 5.6 on Ubuntu 14.04 to PHP 5.5
[root@web01 ~]# apt-get install ppa-purge
[root@web01 ~]# add-apt-repository -r ppa:ondrej/php
[root@web01 ~]# ppa-purge ppa:ondrej/php
[root@web01 ~]# apt-get remove php5.6-common php5.6-cli
[root@web01 ~]# apt-get autoremove
[root@web01 ~]# apt-get install php5 php5-cli php5-common
[root@web01 ~]# a2enmod php5
[root@web01 ~]# service apache2 restart

# Downgrade PHP 7.0 on Ubuntu 14.04 to PHP 5.5
[root@web01 ~]# apt-get install ppa-purge 
[root@web01 ~]# add-apt-repository -r ppa:ondrej/php
[root@web01 ~]# ppa-purge ppa:ondrej/php
[root@web01 ~]# apt-get remove php7.0-common php7.0-cli
[root@web01 ~]# apt-get autoremove
[root@web01 ~]# apt-get install php5 php5-cli php5-common
[root@web01 ~]# a2enmod php5
[root@web01 ~]# service apache2 restart