Downgrade PHP on Ubuntu

Thursday, January 6, 2011 - 09:36

You just upgraded or installed the latest version of Ubuntu (10.04 or 10.10) on your PC, but it comes with PHP 5.3 and for Drupal development this is not optimal. Why is this not optimal, because not all contrib modules are compatible with PHP 5.3. More information can be found on the Drupal website (PHP section).

So you want to downgrade back to PHP 5.2 and lock it so you don't have to uncheck it everytime the update screen pops up.

Here is how I did it with a simple script:

  • Create a file (or download it below), for example 'downgrade_php.sh' and paste the following code in to it, then close the file:
    #!/bin/bash
    # Originally Posted by Bachstelze <a href="http://ubuntuforums.org/showthread.php?p=9080474#post9080474" title="http://ubuntuforums.org/showthread.php?p=9080474#post9080474">http://ubuntuforums.org/showthread.php?p=9080474#post9080474</a> OK, here&#39;s how to do the Apt magic to get PHP packages from the karmic repositories:
    #
    # modified by Ryein C. Bowling (Cardale) &lt;a data-cke-saved-href=&quot;http://www.kinggoddard.com&quot; title=&quot; href=&quot;http://www.kinggoddard.com title=&quot; www.kinggoddard.com&quot;=&quot;&quot;&gt;www.kinggoddard.com December 15, 2010
    echo &quot;Am I root? &quot;
    if [ &quot;$(whoami &amp;2&gt;/dev/null)&quot; != &quot;root&quot; ] &amp;&amp; [ &quot;$(id -un &amp;2&gt;/dev/null)&quot; != &quot;root&quot; ] ; then
    echo &quot; NO!
     
    Error: You must be root to run this script.
    Enter
    sudo su
    &quot;
    exit 1
    fi
    echo &quot; OK&quot;;
     
    # finish all apt-problems:
    apt-get -f install
     
    # remove all your existing PHP packages. You can list them with dpkg -l| grep php
    PHPLIST=$(for i in $(dpkg -l | grep php|awk &#39;{ print $2 }&#39; ); do echo $i; done)
    echo these pachets will be removed: $PHPLIST
    # you need not to purge, if you have upgraded from karmic:
    apt-get remove $PHPLIST
    # on a fresh install, you need purge:
    # aptitude remove --purge $PHPLIST
     
    #Create a file each in /etc/apt/preferences.d like this (call it for example /etc/apt/preferences.d/php5_2);
    #
    #Package: php5
    #Pin: release a=karmic
    #Pin-Priority: 991
    #
    #The big problem is that wildcards don&#39;t work, so you will need one such stanza for each PHP package you want to pull from karmic:
     
    echo &#39;&#39;&gt;/etc/apt/preferences.d/php5_2
    for i in $PHPLIST ; do echo &quot;Package: $i
    Pin: release a=karmic
    Pin-Priority: 991
    &quot;&gt;&gt;/etc/apt/preferences.d/php5_2; done
     
    # duplicate your existing sources.list replacing lucid with karmic and save it in sources.list.d:
    #sed s/lucid/karmic/g /etc/apt/sources.list | sudo tee /etc/apt/sources.list.d/karmic.list
     
    # better exactly only the needed sources, cause otherwise you can get a cachsize problem:
    echo &quot;# needed sources vor php5.2:
    deb http://de.archive.ubuntu.com/ karmic main restricted
    deb-src http://de.archive.ubuntu.com/ karmic main restricted
     
    deb http://de.archive.ubuntu.com/ karmic-updates main restricted
    deb-src http://de.archive.ubuntu.com/ karmic-updates main restricted
     
    deb http://de.archive.ubuntu.com/ karmic universe
    deb-src http://de.archive.ubuntu.com/ karmic universe
    deb http://de.archive.ubuntu.com/ karmic-updates universe
    deb-src http://de.archive.ubuntu.com/ karmic-updates universe
     
    deb http://de.archive.ubuntu.com/ karmic multiverse
    deb-src http://de.archive.ubuntu.com/ karmic multiverse
    deb http://de.archive.ubuntu.com/ karmic-updates multiverse
    deb-src http://de.archive.ubuntu.com/ karmic-updates multiverse
     
    deb http://security.ubuntu.com/ karmic-security main restricted
    deb-src http://security.ubuntu.com/ karmic-security main restricted
    deb http://security.ubuntu.com/ karmic-security universe
    deb-src http://security.ubuntu.com/ karmic-security universe
    deb http://security.ubuntu.com/ karmic-security multiverse
    deb-src http://security.ubuntu.com/ karmic-security multiverse
    &quot; &gt;&gt; /etc/apt/sources.list.d/karmic.list
     
    apt-get update
     
    apache2ctl restart
     
    echo install new from karmic:
    apt-get -t karmic install $PHPLIST
     
    # at the end retry the modul libapache2-mod-php5 in case it didn&#39;t work the first time:
    apt-get -t karmic install libapache2-mod-php5
     
    apache2ctl restart
  • Now make the script executable:
    sudo chmod a+x downgrade_php.sh
  • Execute the script and we're done:
    sudo ./downgrade_php.sh

Note:
When installing a package for php5 after this script has run we need to lock it to PHP 5.2 manually.

Here is how we do that:

  • Open file '/etc/apt/preferences.d/php5_2':
    sudo nano /etc/apt/preferences.d/php5_2
  • Paste the following code in the file and replace 'package-name' with the package you want to install.
    Package: package-name
    Pin: release a=karmic
    Pin-Priority: 991
  • Update the cache of aptitude:
    sudo apt-get update
  • Now you can install the package without any problems.

 

That's it, now you can enjoy your Drupal development.

Source: Ubuntu forums

Comments

Excellent. I was searching for information like this. Helps me a lot. Will try it.

Why don't you upgrade your target platform to Drupal 7, which supports PHP 5.3? :)
If you must use PHP 5.2, I would recommend some fixes to your config.
Your proposed configuration doesn't pick security releases, and the original karmic PHP packages are vulnerable. You must use Pin: release a=karmic-security instead. You can really cut down your sources file by using http://archive.ubuntu.com/ubuntu karmic main restricted universe multiverse.
The markup in your example sources.list has some HTML markup in there, which looks wrong.
Keep in mind that Karmic (9.10) will be end of lifed come the end of April 2011, as ubuntu only supports non LTS releases for 18 months.

Drupal 7 is indeed fully supported, but current build site that need maintenance are still drupal 6, so we need PHP 5.2 for this.
The security pin is a good point, will that test soon and upddate if it works.
I got the sources list from the ubuntu forums and it just worked for me so I didn't feel the need to adjust it.
Karmic will indeed come to an end in April 2011, but you will still be able to use the packages (however there will be no more updates for these packages).

Thanks for the tip.
Will try these repositories soon.
Greets,
Stijn

...can't get past this line in the author's bio: "He knows everything."
Who really says or believes that about themselves, especially when it comes to technology. Sure, OK, maybe it's not meant to be taken verbatim.
Oh wait, I get it. That's a practical joke. Good one.

Just checkout all our bio's on http://krimson.be/about and than you'll see that we never take ourselves seriously.
Except when it comes to Drupal. We do know 'EVERYTHING' about Drupal :P

Hi Matthias,
I don't see a difference between the two lines..
Could it be that our input format automatically puts a href around the "http:// .. " ?

I ran a test copy of my Drupal 6.19 system, with about 25 contributed modules on an Ubuntu system 10.04 LTS Lucid Lynx (with PHP 5.3) and so far it seems to be running fine. I keep hearing that some modules don't properly support PHP 5.3, but other than looking for truly obvious problems I don't know how to confirm that there are no PHP problems with my modules. Other than going to each module's page on drupal.org and looking for trouble reports or assurances that the module is ok with 5.3, is there any way to check this?

Since everyone tests on a copy of their sites on their local PC, it should be fairly easy to find them, especially if you use E_ALL.
The only errors I have seen at all merely required removing an "&" from the function definition. I suspect even non-coders can handle that fix.
This is hardly a cause for great alarm.

Don't do this. Instead, fix the problems in the contrib modules. PHP 5.2 is end of life!

I've had the same Drupal 6 problems in the past and wrote a less easy method to downgrade PHP at http://www.chriswiegman.com/2010/05/php-5-2-on-ubuntu-10-04/. Over time though I've found that I've been able to upgrade all my servers to PHP 5.3 with Drupal 6 without problem (overall there are a little over 100 modules being used).
My advice at this stage is to make sure you really need to downgrade. Check to make any unsupported modules are first necessary and second, if they are look for alternatives (often there is more than one module to perform a given function).
With just the above 2 steps alone you should have very few problems with PHP 5.3 and Drupal 6.

Create issues for any modules incompatible with 5.3. Keeping 5.2 alive as a private zombie is not the way.
I really think there should be a php compatibility tab on d.o's project pages to guide people in this.

It is interesting that so far no one has been able to name ANY popular drupal module that isn't compatible with PHP 5.3.
I have been running many high trafficed drupal sites on PHP 5.3 for more than 10 months now and I haven't encounter any issue with PHP 5.3
I feel it is unfair to keep propagating myth that Drupal 6 still doesn't work with PHP 5.3 without providing much evidence.
--
Abhi

I have encountered about 3 modules with "problems." In every case, removing one "&" fixed the problem, which is actually the proper way to be coded, not really an error. Opening an issue on DO takes maybe one minute. I'd rather have the modules fixed.

If everyone just fixed the module and did not create an issue with the fix there would be no community and you would have nothing to use.
The community is created by people spending the extra minutes so it is valuable.
Be part of the bigger picture and we all win.

We outlined various ways of running PHP 5.2 with Ubuntu 10.04 Lucid Lynx and the pros and cons of each.
But for sites that don't have a lot of modules, you may be able to get away with PHP 5.3. We did just that for a huge site that has only 43 modules in total, and it runs fine. Of course, the specific modules matter more than the number of modules.

After I run
sudo apt-get update
I get an error message:
Malformed line 20 in source list /etc/apt/sources.list.d/karmic.list
I followed all of the instructions, however I am running 10.10 Is this the issue?

one thing though, after reverting to 5.2 with this method - installing xdebug via pecl didn't work anymore, so i had to compile it from source.

It might work to downgrade PHP, but the better solution is still to fix the broken modules so your sites works fine on 5.3.
My personal approach to supporting both 5.2 and 5.3 on 10.04 LTS is to manually install PHP 5.2 in /usr/local on my Apache2+mod_fcgid system and point the FCGI wrapper to the 5.2 bin for sites that can't be fixed for 5.3.
And I have to say, the only sites I've had to downgrade like that are Joomla sites.

What about 1.7.1 of XAMPP? It's like WAMP for Linux including:

Apache 2.2.11
MySQL 5.1.33
PHP 5.2.9

Here you can download the version 1.7.1:
http://sourceforge.net/projects/xampp/files/XAMPP%20Linux/1.7.1/
And here are the release notes:
http://sourceforge.net/project/shownotes.php?release_id=675563
If you need to install any extension, also install the devel package to build and config with phpize.
Hope this helps!