Ubuntu has released the latest LTS (Long-Term Support) version 16.04 (Xenial Xerus) of their distribution. The official Ubuntu repositories include only PHP 7.0, since it is the official version (at the time of the 16.04 release). However, many developers still use PHP 5.5 or 5.6 in their production environments, so developing on these versions is frequently necessary. As usual, the PPAs come to our rescue for installing PHP 5.5 or 5.6 on Ubuntu 16.04. Here’s how.
sudo apt-add-repository -y ppa:ondrej/php sudo apt-get -y update
If you are familiar with these repositories, you’ll notice that the PHP version is not included in the PPA repository name. For Ubuntu 16.04, both PHP 5.5 and 5.6 are included in the PPA. (If you need PHP 5.4 or earlier, you’ll need to stay with Ubuntu 14.04 LTS Trusty Tahr.)
Now, we can install both PHP 5.6 (or 5.5) and 7.0, along with support for Apache 2 and MySQL, including various PHP modules, such as those required for Laravel framework:
sudo apt-get -y install php7.0 php5.6-mysql php5.6-cli php5.6-curl php5.6-json php5.6-sqlite3 php5.6-mcrypt php5.6-curl php-xdebug php5.6-mbstring libapache2-mod-php5.6 libapache2-mod-php7.0 mysql-server-5.7 apache2
As with any LAMP installation on Ubuntu, you’ll be prompted for the administrator (root) password for MySQL. Also, note that the package for XDebug (
php-xdebug) does not include a version, since it supports both PHP 5.6 (or 5.5) and 7.0. Most other packages are PHP version-specific. If you aren’t sure, just enter part of the name on the
apt-get command line and press <Tab> to see the various options.
Once we have PHP 5.6 (or 5.5) and 7.0 installed, we can easily switch between them from the shell using these commands. To enable PHP 5.6 (and disable PHP 7.0) use this command:
sudo a2dismod php7.0 ; sudo a2enmod php5.6 ; sudo service apache2 restart ; echo 1 | sudo update-alternatives --config php
Similarly, to switch from PHP 5.6 to PHP 7.0, use this command:
sudo a2dismod php5.6 ; sudo a2enmod php7.0 ; sudo service apache2 restart ; echo 2 | sudo update-alternatives --config php
alias phpv5='sudo a2dismod php7.0 ; sudo a2enmod php5.6 ; sudo service apache2 restart ; echo 1 | sudo update-alternatives --config php' alias phpv7='sudo a2dismod php5.6 ; sudo a2enmod php7.0 ; sudo service apache2 restart ; echo 2 | sudo update-alternatives --config php'
Now, you can just run
phpv7 to switch to PHP 5.6 or PHP 7.0, respectively.
In the commands above to switch/re-start PHP versions, the
update-alternatives command is used switch the version of PHP that is used when running PHP from the shell/command line. This is especially important when using PHP command-line tools, such as Composer and the Laravel Artisan commands.
If you run into problems starting Apache 2 web server (
sudo service apache2 restart) with the error “Apache is running a threaded MPM, but your PHP Module is not compiled to be threadsafe. You need to recompile PHP.”, then run the following commands in Ubuntu:
sudo a2dismod mpm_event sudo a2enmod mpm_prefork sudo service apache2 restart
This should work for both PHP 5.6 and PHP 7.0; therefore, you should not need to run this again when switching between PHP versions.
Let me know if you have any questions in the comments below.