PHP 7.0 (and 5.6) on Ubuntu
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 php5.6 and php7.0 modules available, and the php5.6 module actually loaded.
As well as just the 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.
Or install docker and just get the right docker container for the version you want to work with.
I agree. Docker containers are great for this kind of thing.
Or, if you need something like an LXD/LXC container with PHP self contained on the container, this is still a great strategy. Especially when you’re dealing with WordPress client sites which want to run a plugin that doesn’t support PHP 7.x yet.
Thanks for sharing Lornajane!
One of the issues I had when I was adding this PPA a couple of months ago was that it kept failing. I managed to find out that you needed to specify UTF-8 as the language when adding the PPA:
LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php-7.0
Pingback: PHP7 on Ubuntu 15.10 - Chema Garrido
I’ve used the ondrej/php to upgrade a VVV box (the standard WordPress dev environment) – it works very well.
If anyone’s using Debian (assuming you’re on Jessie – the stable release) you want DotDeb – https://www.dotdeb.org/instructions/
On Debian, PHP 5.6 (from Jessie) and 7 (from DotDeb) co-exist nicely without any warnings, you just install the php70 packages on top.
I didn’t even bother to check if that was possible and went for an all or nothing approach; test all my sites in PHP7 on dev first, push the code on production, make all the nginx configs consistent (including moving the PHP section into a single shared file), then finally update that to point to the new php-fpm socket after php7 was installed.
What I could (and probably should) have done, is install php7 first, leave 5.6 running, then switch one site over at a time – easy with Nginx because it doesn’t know anything about PHP, it just sends the requests wherever you tell it.
I’ve not noticed a reduction in CPU load, but mine was very low to begin with. I’m yet to upgrade a production which is running WordPress, but it’s super fast on the dev VM.
Incidentally, surprisingly few people have watched this talk by Rasmus – loads of useful tips in it.
If you’re prepared to wait a couple of months, Ubuntu 16.04 (due out in April and a Long Term Support release) will have 5.6 and 7.0 and you won’t need to rely on a PPA.
If you are planning to upgrade but use the PPA in the meantime, make sure you keep a careful watch on the version numbers as I don’t think it’s particularly easy to switch from a PPA to the main repository for a package if the PPA version is higher.
It is definitely going to have at least one of 5.6 or 7.0 but there are still conflicting posts all over the place that suggest which one. But I have read that they will not have both. I hope they do though.
Ubuntu is going to drop either 5.6 or 7 before feature freeze (or actually after, as PHP got some kind of exception), but ATM it is looking as if they will drop 5.6 and go the php7 route. The thing with both versions was mentioned many times as transitional testing if php7 meets the required stability until free deadline, so ATM there is NO co-installability for xenial mentioned anywhere official!
I’d be *very* surprised if they don’t include both (and both are currently showing in the archive, so there’s only about a month to pull one or the other before release). An LTS release which doesn’t have 5.x would be useless for a lot of people who haven’t migrated their code to 7.
Not to be a spoilsport but check the corresponding bug and comment section. It is _very_ clear, that Xenial will drop one or the other for going live. Ubuntu won’t likely have both versions in main and wanting to support it. I even suggested it, but it doesn’t seem to give much.
So no, I’d be *very* surprised myself if they would include both. It would be very appreciated for sure (as I write from a hoster/hosting perspective) but likely won’t happen. That’s why I suggest keeping an eye out for Ondrej Sury and his co-installability repos with 5.6 and 7 and hope he will support xenial with his repo as well and not drop support for 5.6, too.
Pingback: PHP Annotated Monthly – February 2016 | PhpStorm Blog
To switch the php command you shouldn’t change the symlinks in /etc/alternatives manually. Use thze command line tool for this.
sudo update-alternatives –config php
This shows all available options and you can select the one you want.
Exactly what I was looking for! Thank you for this.
Thanks, that’s much cleaner than my manual linking and unlinking.
I added a little more magic for a noninteractive version. Here is a switch to 7.0 then back to 5.6:
$ php -version | head -1
PHP 5.6.23-2+deb.sury.org~xenial+1 (cli)
$ sudo update-alternatives –set php $(sudo update-alternatives –list php | grep “php7.0”)
update-alternatives: using /usr/bin/php7.0 to provide /usr/bin/php (php) in manual mode
$ php -version | head -1
PHP 7.0.8-3+deb.sury.org~xenial+1 (cli) ( NTS )
$ sudo update-alternatives –set php $(sudo update-alternatives –list php | grep “php5.6”)
update-alternatives: using /usr/bin/php5.6 to provide /usr/bin/php (php) in manual mode
$ php -version | head -1
PHP 5.6.23-2+deb.sury.org~xenial+1 (cli)
Pingback: Which version of PHP will be in Ubuntu 16.04? | PHP Developer
Thanks you, very useful!
Pingback: Installing PHP 7 on Ubuntu 16.04 | Shaun Freeman
Nice post. Where did you get the package php5.6-dev? it’s not in ondrej’s PPA.
Thanks for this, it is exactly what I needed while we make the move between 5.6 and 7.0
I didn’t get how to install extensions using pecl to php5.6 or php7.0.
For example I have already installed version of libevent or amqp in php5.6.
Now when I type `pecl install libevent` and my active php version is php7.0 (using update-alternatives), pecl returns message that libevent already installed.
But it was installed only for php5.6 (when this version was active) and now I want to do it for php7.0.
Which commands could help me?
I have asked this on stackoverflow but question still not answered..
Pingback: PHP 7 to PHP 5.6 downgrade on Ubuntu 16.04 – The Code Beginner
Hi, thanks for the guide.
How can i install mysql with php 5.6 compatible? i need to use the old Original MySQL API
I believe that Digital Oceans latest WordPress image has PHP7 installed by default but I could be wrong. I know that I am currently running PHP7/MySQL7 on that image.
Pingback: How to have both php 5.6 and 7 on Ubuntu and swap easily – Reeaz Blog
Ok, now I have php5.6 and php7.0 how can I set one of the two as the default one to start ?
I would like to use the PHP socket api and from your comment it seems I might be able to do this without having to recompile php 5.6
I’m using Ubuntu 16.04 with PPA. Any hints would be greatly appreciated
Ok. seems it is installed just not reading. Likely a Ubuntu loop back issue. socket_recv timesout and reads 0 bytes.
I would really like to avoid wiping the os to install and older version with stock php5. How can I do the above but also make it work with nginx?
Pingback: PHP downgrade version – TuyenLN
Here’s a small addition to your switching script that sets the command line alternative. This gets both the server and the command line in sync using the same version of PHP:
sudo a2dismod php5 php5.6 php7.0 php7.1;
sudo a2enmod php7.1;
sudo service apache2 restart;
sudo update-alternatives –set php /usr/bin/php7.1;
Run “sudo update-alternatives –config php” to see a list of possible values to set.
Pingback: encounter mysql problem when trying to install php5.6 along with php7.0 – 1OO Club
Pingback: How to use PHP 5.6 and PHP 7.0 alongside with one another in Ubuntu - Mehedi Hasan