PHP 7.0 (and 5.6) on Ubuntu
PHP 7 is here! Running Ubuntu? Don't want to wait for the official distro package? Check out this PPA for Ubuntu that allows both PHP 5.6 and PHP 7.0 to be installed and run side-by-side!
Join the DZone community and get the full member experience.Join For Free
PHP 7 is released but for those of us who don't usually compile our own PHP, it can be a long wait for our preferred distro to release the packages we want. For Ubuntu, I'm using a PPA which allows both PHP 5.6 and PHP 7.0 to be installed, including things like extensions, at the same time. It was very easy to set up (I'm running Ubuntu 15.10, but this process should also work on older versions back to at least 14.04 which is the previous LTS) so here's a quick walkthrough of what I did.
Add the PPA
The PHP 5.6 and PHP 7.0 packages are from a third party PPA, not provided by the official Ubuntu repositories from Canonical. The PPAs I'm recommending here are from Ondřej Surý who packages PHP for Debian (which is then used by Ubuntu) so while it's not an official repo, he's not exactly random! The PPA itself is here: https://launchpad.net/~ondrej/+archive/ubuntu/php
Complete aside, but an important one: The work of the packagers is voluntary and they enable the rest of us to do whatever it is that we do. If you want us to keep being able to have nice things, consider the donation page for this project, please. https://deb.sury.org/pages/donate.html
To add the PPA to your setup:
sudo add-apt-repository ppa:ondrej/php
Then we'll also want to grab information about what is on offer from this new PPA, so then run:
sudo apt-get update
Install New PHP Versions
I already had some of the
php5 packages installed, but I didn't uninstall anything, I just let apt work out what it wanted to do when I asked it to install the new versions:
sudo apt-get install php5.6 php7.0
This resulted in a lot of complaining from apt and lots of conflicts. The first suggested resolution was to remove all the stock
php5 packages so that PHP 5.6 could be installed—so I just accepted the first suggestion.
I use Apache, so this setup gave me Apache with both PHP 5.6 and PHP 7.0 modules available, and the PHP 5.6 module actually loaded.
As well as just PHP itself, all the extensions and other tools you'd expect with PHP are there for both versions of PHP, so it's very easy to add in the modules that you need. I was very, very impressed with how nicely this is done.
Configuring and Switching Versions
Now you have two completely separate versions of PHP installed on your system, so let's have a look at where all the pieces went!
The config files are all in
/etc/php/7.0 respectively—inside here is where you can configure which extensions are loaded, set the ini settings, and everything else for each version in isolation.
I'm an Apache user, and, as I mentioned, both modules are available. So, to switch from one to the other I need to do:
sudo a2dismod php5.6
sudo a2enmod php7.0
sudo service apache2 restart
For NGINX users, the changes are almost as easy. Digital Ocean have good documentation on this (they do have great docs!) so check out their guide: https://www.digitalocean.com/community/tutorials/how-to-upgrade-to-php-7-on-ubuntu-14-04 as it includes a section on reconfiguring NGINX to use another version of PHP.
From the commandline, I have both
php7.0 available as commands. I also still have a
php command—look in
/etc/alternatives to see that it symlinks to a particular version of PHP cli*. You can also quickly check which yours is using by running
* more specifically, run
which php to see which version of PHP is being used—but, this will probably point to
/usr/bin/php, which for me is itself a symlink to the
Working With Extensions
This PPA comes with the usual
php-pear package which offers the
pecl command for both versions of PHP, so any extensions that are available via PECL can be installed in the usual way. You will also need the relevant headers, so either
php7.0-dev should be installed.
When the pecl installation completes, you'll get a note to add the
*.so file to your
php.ini; in fact, the best thing to do here is to look at what's in
/etc/php/mods-available. There will be some modules already here, each in its own file named after the extension and ending in
.ini. You can copy one to use as a template or create your own and put all the relevant configuration for the extension in it (as a minimum, you need
Once the new extension is in mods available, enable and then check it by doing:
sudo phpenmod extension php -m
This will create the symlinks in the right places for your current version of PHP to load this module, and you should see it in the list of modules output by the
php -m. Pro tip: if you don't see it in the output, scroll all the way to the top of the output and see if there are any useful error messages there.
There are some extensions that aren't currently available either by default or via pecl, so I'll write a followup post on compiling an extension from source to use with your PHP.
Published at DZone with permission of Lorna Mitchell, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.