54

so I've recently installed my first Ubuntu, to be precise, a daily build of 16.04, on my new laptop since the hardware was to new for 15.10. Now I'm in the process of setting it up and I want to install php5 and some extensions.

When I execute sudo apt-get install libapache2-mod-php5 I get the error message that there is no installation package available for php5 and libapache2.

The error message is in german, so I doubt that this would help, but anyways here it is. :-) E: For package »libapache2-mod-php5« existiert kein Installationskandidat.

Do I need to add a apt-get repository? Or what am I doing wrong?

oerdnj
  • 7,940

2 Answers2

75

TL;DR: If you want to stay with php5, you will need to stay at Ubuntu 14.04 LTS

Using PHP 7.0 on Ubuntu 16.04 LTS

Ubuntu 16.04 has switched to PHP 7.0 with a new infrastructure for PHP package. So, no, you can't install php5 on Ubuntu 16.04, but you can install PHP 7.0 packages with:

apt-get install libapache2-mod-php

That will install a virtual package that depends on the latest PHP version and pull libapache2-mod-php7.0 as a dependency.

If you are looking for extensions, always use a version-less variant as well (e.g. php-apcu instead of php7.0-apcu) as the PECL extensions are packaged without the version to allow smooth upgrades.

Using PHP PPA to coinstall PHP 5.6 with PHP 7.0

There's an option to co-install PHP 5.6 packages using ppa:ondrej/php.

For more information, please see that answer: https://askubuntu.com/a/762161/309221

Extracted from link above:

Assuming libapache2-mod-php is a suitable way to enable PHP in Apache for you, you can proceed in this way:

sudo add-apt-repository ppa:ondrej/php

sudo apt-get update

sudo apt-get install php7.0 php5.6 php5.6-mysql php-gettext php5.6-mbstring php-xdebug libapache2-mod-php5.6 libapache2-mod-php7.0

Switch PHP version ( Apache ):

  • from php5.6 to php7.0:
    sudo a2dismod php5.6 ; sudo a2enmod php7.0 ; sudo service apache2 restart

  • from php7.0 to php5.6:
    sudo a2dismod php7.0 ; sudo a2enmod php5.6 ; sudo service apache2 restart

oerdnj
  • 7,940
  • 2
    No, there are no PHP 5.x packages in Xenial repositories. Only in the PPA. – oerdnj Jul 12 '16 at 04:54
  • Looks like you're right: https://bugs.launchpad.net/ubuntu/+source/php5/+bug/1522422 – NoBugs Jul 12 '16 at 04:57
  • When I switch to php5.6, I got error "Job for apache2.service failed because the control process exited with error code. See "systemctl status apache2.service" and "journalctl -xe" for details.". – lyhong Nov 15 '16 at 08:05
  • And what does the journal say? – oerdnj Nov 15 '16 at 08:07
  • found something like this apache2: Syntax error on line 140 of /etc/apache2/apache2.conf: Syntax error on line 3 of /e – lyhong Nov 15 '16 at 08:22
  • Then fix the configuration error... this is not something you should be solving in the comments section here. – oerdnj Nov 15 '16 at 08:24
  • But it's ok if I switch to php7. This problem only happens when I switch to php5.6. – lyhong Nov 15 '16 at 08:27
  • Can we trust the non auth pkgs? WARNING: The following packages cannot be authenticated! libssl1.0.2 php-common php5.6-common php5.6-json php5.6-opcache php5.6-readline php5.6-cli libapache2-mod-php5.6 php5.6 – Efren Nov 17 '16 at 06:04
  • Something is broken on your system, remove the PPA and add it back again. – oerdnj Nov 17 '16 at 06:06
  • 5.6 is also possible for 16+ version: sudo apt-get install libapache2-mod-php5.6 – FieryCat Jul 19 '17 at 21:56
  • $ sudo add-apt-repository ppa:ondrej/php sudo: add-apt-repository: command not found ALSO THIS: $ sudo apt-get install libapache2-mod-php5.6 Reading package lists... Done Building dependency tree Reading state information... Done E: Unable to locate package libapache2-mod-php5.6 E: Couldn't find any package by glob 'libapache2-mod-php5.6' E: Couldn't find any package by regex 'libapache2-mod-php5.6' – Braian Mellor Jul 25 '17 at 14:15
  • Anyone else getting Module php5.6 does not exist when running a2enmod php5.6? I installed the following: sudo apt-get install php7.0 php5.6 php5.6-mysql php-gettext php5.6-mbstring php-mbstring php7.0-mbstring php-xdebug php5.6-mcrypt php5.6-mysql php5.6-curl php5.6-gd php5.6-xml php5.6-xmlrpc libapache2-mod-php5.6 libapache2-mod-php7.0 – Katie Feb 27 '19 at 00:17
25

EDIT: the accepted answer does not explicitly say how to co-install PHP 5.6 and 7.0, so I wanted to extend it.

For more information, please see that answer: https://askubuntu.com/a/762161/309221

Extracted from link above:

Assuming libapache2-mod-php is a suitable way to enable PHP in Apache for you, you can proceed in this way:

sudo add-apt-repository ppa:ondrej/php

sudo apt-get update

sudo apt-get install php7.0 php5.6 php5.6-mysql php-gettext php5.6-mbstring php-xdebug libapache2-mod-php5.6 libapache2-mod-php7.0

Switch PHP version ( Apache ):

  • from php5.6 to php7.0:
    sudo a2dismod php5.6 ; sudo a2enmod php7.0 ; sudo service apache2 restart

  • from php7.0 to php5.6:
    sudo a2dismod php7.0 ; sudo a2enmod php5.6 ; sudo service apache2 restart

  • 1
    FYI: The PPA you use is maintained by the person who wrote the accepted answer. And the last line in that answer: "There's an option to co-install PHP 5.6 packages using ppa:ondrej/php". Note that you aren't installing php5 packages either, but php5.6 – muru May 08 '16 at 21:49
  • 1
    There are a number of differences in the packaging and location of configuration files that anyone looking for the old php5 packages would be surprised in the new ones. – muru May 09 '16 at 17:27
  • I have edited my answer so it doesn't say that the accepted answer was incorrect, but only extends it. – FlavioEscobar May 09 '16 at 17:39
  • @NoBugs It does have imagick extension packaged. Please stop spewing things without at least basic checking whether they are true or not. – oerdnj Jul 13 '16 at 11:12
  • @FlavioEscobar I have merged your edits into my answer. Thanks for extending the answer. – oerdnj Jul 13 '16 at 11:20
  • @oerdnj Apparently purging that ppa does not remove packages from that ppa... and since it seems they did decide to not include 5.x, you are correct, my mistake. – NoBugs Jul 14 '16 at 03:08
  • but how switch it for CLI – Maximus Aug 25 '16 at 12:20
  • I am confused by the words 'EDIT: the accepted answer does not explicitly say how to co-install PHP 5.6 and 7.0, so I wanted to extend it.' I cannot see any reference to co installing or any reference to PHP 7. It seems to me a simpler answer would be to explain you can no longer install php5 and need to install php 5.6 - no offence intended - just curious. – kerry Jun 06 '17 at 09:31