236

I am trying to update to install PHP 5.5, 5.6 or 7.0 in Ubuntu 12.04/14.04/16.04 LTS, and I can only get PHP 5.3.10 for Ubuntu 12.04, PHP 5.5.9 for Ubuntu 14.04 or PHP 7.0.4 for Ubuntu 16.04 using official repositories (using apt-get).

I'm not really sure how to do a manual update – as I need:

  • to play around with the new(est) PHP features
  • to install older PHP version due support in the software
oerdnj
  • 7,940
kavisiegel
  • 2,517

2 Answers2

302

You could use a PPA to stay up-to-date with PHP or install previous version of PHP (f.e. PHP 5.6 on Ubuntu 16.04 LTS).

The most widely used repositories come from Ondřej Surý, the Debian PHP maintainer:

  • ppa:ondrej/php Launchpad logo (for PHP 5.6/7.0/7.1 co-installable versions, PHP 5.5 is no longer supported)

The php5 compat packages:

(Click here for instructions on using PPAs.)

PHP PPAs previously contained Apache 2.4 update. This is no longer a case, you need to add separate Apache 2.4 repository:

If you want use these PPAs, do this:

ppa:ondrej/php Launchpad logo (for PHP 5.6/7.0/7.1)

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install php7.1   # for PHP 7.1
sudo apt-get install php7.0   # for PHP 7.0
sudo apt-get install php5.6   # for PHP 5.6

To switch between installed versions use

sudo update-alternatives --config php

Then you must set Apache to work with right version:

sudo a2dismod php7.1         # unload the current version
sudo a2enmod  php5.6         # load the version you need
sudo service apache2 restart # restart webserver to apply

ppa:ondrej/php5-compat Launchpad logo (for php5 compat)

sudo add-apt-repository ppa:ondrej/php
sudo add-apt-repository ppa:ondrej/php5-compat
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install php5 # this will pull php5.6 package 

If you don't have add-apt-repository binary do the following:

sudo apt-get install python-software-properties

Precautions:

PHP 5.4

Prepackaged latest PHP 5.4 now resides in separate PPA: ppa:ondrej/php5-oldstable Launchpad logo

Please be aware that PHP 5.4 has reached its end of life on 3. September 2015 and it doesn't receive any security updates. It's recommended to migrate to at least PHP 5.6 that will receive security updated till 31. December 2018.

muru
  • 197,895
  • 55
  • 485
  • 740
  • Installing this PPA works for the most part, but packages that rely on phpapi-20090626+lfs, like php-apc for example, will fail installation as this PPA does not include that particular virtual package. I'm guessing a new PHP version has a new phpapi virtual pacakge, but this is not included if so. – Ian Mar 04 '12 at 16:54
  • I ran sudo add-apt-repository ppa:ondrej/php5 - but when I run apt-get chanelog php5 it tells me it's still at version 5.3.6. Do I need to specify the PPA in apt-get? – kavisiegel Mar 05 '12 at 21:23
  • 5
    Turns out you gotta run sudo apt-get update first! – kavisiegel Mar 05 '12 at 21:40
  • I just asked for php5, and I got php 5.4.0, but it also installed apache 2.2. I did not ask for it, to install apache. I just want php 5.4.0 cli. – Mark Tomlin Mar 07 '12 at 09:06
  • 3
    there is the php5-cli package for that! – matteosister Mar 07 '12 at 21:17
  • Hmm... I wish I could do it all on the CLI... is it possible? – Felipe Sep 22 '12 at 18:19
  • 11
    After adding the ppa, you could do: sudo apt-get dist-upgrade – matteosister Sep 23 '12 at 08:03
  • 2
    After installing the ppa, upgrading php and running php -v, the version information starts with this error message: Failed loading /usr/lib/php5/20090626/xdebug.so: /usr/lib/php5/20090626/xdebug.so: cannot open shared object file: No such file or directory Annoying. – Geoffrey Oct 24 '12 at 11:55
  • 2
    that's normal. Php 5.4 uses a new version of the zend library. When you upgrade to 5.4 the xdebug extension gets recompiled. You need to check inside /usr/lib/php5 the name of the folder. Mine is 20100525, should be yours too, and check if there is a xdebug.so file. Now edit the xdebug.ini file in /etc/php5/mods-available/xdebug.ini (or the symlinked /etc/php5/conf.d/**-xdebug.ini) and set the correct path to the xdebug.so file – matteosister Oct 24 '12 at 15:14
  • There's xdebug package in my PPA together with php5 5.4, so you just need to install it from the PPA. – oerdnj Mar 05 '13 at 15:16
  • Just a forewarning – the repository will be updated to 5.5 as soon as it is out... – oerdnj Mar 05 '13 at 15:19
  • 2
    If Ubuntu tells you the packages have been "kept back", run apt-get dist-upgrade. – Ashley Strout Mar 13 '13 at 21:34
  • Running php -a starts 'Interactive mode', not 'Interactive shell' Can I use 'Interactive shell' ? – Md Enzam Hossain Jul 16 '13 at 18:45
  • This PPA now offers 5.5 which comes with an upgrade to Apache 2.4 that might break some installations. More details here:

    http://www.justincarmony.com/blog/2013/07/31/ubuntu-12-04-php-5-4-apache2-and-ppaondrejphp5/

    – markdwhite Aug 09 '13 at 02:28
  • This PPA is lacking the proper packages in order to install Virtualmin. libapache2-mod-fcgid : Depends: apache2.2-common but it is not going to be installed libapache2-mod-ruby : Depends: apache2.2-common but it is not going to be installed libapache2-svn : Depends: apache2.2-common but it is not going to be installed –  Jan 14 '14 at 03:31
  • Well, I used this and it worked so far as getting me PHP5.5 and Apache 2.4. My Apache is indeed messed up. Really having an adventure in trying to figure out WTF's Apache's issue is. It's like it no longer has php support, but the php mod is enabled. WTF? – James T Snell Feb 22 '14 at 18:40
  • Figured it out: When this installed Apache 2.4, my pre-existing site config was no longer functional. Turns out they added some new security setting to apache. I had to add "Require all granted" to my vhost's directory section (thanks to: http://stackoverflow.com/questions/10351167/apache-client-denied-by-server-configuration) – James T Snell Feb 22 '14 at 18:58
  • I've done everything as it was written and still I'm getting 5.5, not 5.4 :( – Eugene Apr 04 '15 at 18:41
  • 3
    Use apt-get install software-properties-common if apt-get install python-software-properties doesn't provide the add-apt-repository command. – Meetai.com Apr 11 '16 at 03:25
  • 1
    As of Dec 12, 2016 this no longer works for PHP 5.5. The repository removed support for the 5.5 packages according to this tweet. https://twitter.com/debsuryorg/status/808284372356825088 How can I reliably install PHP 5.5.31 now after this? – Jerry Saravia Dec 14 '16 at 21:47
  • For PHP 7 there are many extensions unavailable such as GD ! – SaidbakR May 10 '17 at 02:26
  • is PPA package support PHP 5.6 on ubuntu 18.04? when i try using it showing forbidden. – Shanteshwar Inde Nov 21 '19 at 06:08
4

It will take some time for PHP 5.4 packages to make their way into Ubuntu, as there are an awful lot of dependency checks to do. Ubuntu 12.04 is now in beta so I doubt they'll upgrade it to PHP 5.4 (although I have no official or inside knowledge of this) -- it probably won't appear until Ubuntu 12.10 in October this year. If you want it sooner than that, you'll either have to find a third-party package or learn to build it yourself from the source code.

Mike Scott
  • 2,194