Various ways of running PHP 5.2 on Ubuntu 10.04 Lucid Lynx

Published Sat, 2010/06/05 - 17:05, Updated Sun, 2010/07/25 - 22:41

The latest Long Term Support (LTS) release from Ubuntu has been with us for a bit over a month now, and I love it so far, on my laptop. This is Lucid Lynx 10.04, and will be supported until 2013 on the desktop, and more importantly, until 2015 on the server.

My servers are still on 8.04 though, the previous LTS, for good reason. 10.04 has PHP 5.3. While this is generally a better PHP, it has some compatibility issues with many Drupal, mainly for contributed modules, but some in core as well.

The long term solution to this is to find all PHP 5.3 compatibility issues and fix them in the code. This is a lengthy process though, and will not happen overnight.

So, in the meantime, there are several solutions for running PHP 5.2 on Lucid, some better than others.

In this post, we examine the different approaches, and the merits of each.

Approach 1: Installing Karmic's PHP 5.2 packages

The most common approach is to install Karmic 9.10's PHP 5.2 packages on 10.04. This has been reported to work for many, such as here, here and here.

The detailed instructions were originally posted here, and the following script is a refinement of that approach:

#!/bin/sh

# Script to install PHP 5.2 from 9.10 on 10.04
# And pin it so it does not get updated

PKGS=`dpkg -l | grep php | awk '{print $2}'`

apt-get remove $PKGS

sed s/lucid/karmic/g /etc/apt/sources.list | 
  tee /etc/apt/sources.list.d/karmic.list

mkdir -p /etc/apt/preferences.d/

for PACKAGE in $PKGS
do
  echo "Package: $PACKAGE
Pin: release a=karmic
Pin-Priority: 991
" | tee -a /etc/apt/preferences.d/php
done

apt-get update

apt-get install $PKGS

This is not the best solution though for many reasons, the main one is that these packages were compiled against libraries built for 9.10 and not 10.04. There is always a risk of unpredictable things happening when doing such a thing.

It also requires the packages to be "pinned" so no future security updates would be provided, unless 9.10 gets them too.

I also found no easy way to pin with aptitude, rather than with apt-get.

Approach 2: Proper PHP 5.2 packages, compiled for Lucid, installed manually

The fine folk at The Jibe took this one step further and built PHP 5.2 .deb packages for Lucid.

What this approach provides is that the packages are built against Lucid headers, and therefore solves the issue mentioned above.

What this solution suffers from is the lack of a repository. This means that the packages have to be downloaded and installed manually. Also, if you install them directly via the dpkg -i command, there may be unmet dependencies if you have not installed PHP 5.3 previously. This can be overcome by running the gdebi command instead, available in the "gdebi-core" package.

Still, the previous approach is too manual, and there is no way to check automatically for security updates for example.

Approach 3: Proper PHP 5.2 packages, compiled for Lucid, installed from a repository

So, the best solution, is to install from a repository
Ralph Janke's PHP 5.2 repository for Lucid

If you have the package "python-software-properties installed, you can easily add the repository using the command:

add-apt-repository ppa:txwikinger/php5.2

That will install the GPG key for security verification.

If you are using apt-get, then you need to create a file called /etc/apt/preferences.d/php for pinning the versions, so they remain at 5.2 and don't get upgraded to 5.3 when you run apt-get to upgrade your installation.

Put this in the file:

Package: libapache2-mod-php5
Pin: version 5.2.10*
Pin-Priority: 991

Package: libapache2-mod-php5filter
Pin: version 5.2.10*
Pin-Priority: 991

Package: php-pear
Pin: version 5.2.10*
Pin-Priority: 991

Package: php5
Pin: version 5.2.10*
Pin-Priority: 991

Package: php5-cgi
Pin: version 5.2.10*
Pin-Priority: 991

Package: php5-cli
Pin: version 5.2.10*
Pin-Priority: 991

Package: php5-common
Pin: version 5.2.10*
Pin-Priority: 991

Package: php5-curl
Pin: version 5.2.10*
Pin-Priority: 991

Package: php5-dbg
Pin: version 5.2.10*
Pin-Priority: 991

Package: php5-dev
Pin: version 5.2.10*
Pin-Priority: 991

Package: php5-gd
Pin: version 5.2.10*
Pin-Priority: 991

Package: php5-gmp
Pin: version 5.2.10*
Pin-Priority: 991

Package: php5-ldap
Pin: version 5.2.10*
Pin-Priority: 991

Package: php5-mhash
Pin: version 5.2.10*
Pin-Priority: 991

Package: php5-mysql
Pin: version 5.2.10*
Pin-Priority: 991

Package: php5-odbc
Pin: version 5.2.10*
Pin-Priority: 991

Package: php5-pgsql
Pin: version 5.2.10*
Pin-Priority: 991

Package: php5-pspell
Pin: version 5.2.10*
Pin-Priority: 991

Package: php5-recode
Pin: version 5.2.10*
Pin-Priority: 991

Package: php5-snmp
Pin: version 5.2.10*
Pin-Priority: 991

Package: php5-sqlite
Pin: version 5.2.10*
Pin-Priority: 991

Package: php5-sybase
Pin: version 5.2.10*
Pin-Priority: 991

Package: php5-tidy
Pin: version 5.2.10*
Pin-Priority: 991

Package: php5-xmlrpc
Pin: version 5.2.10*
Pin-Priority: 991

Package: php5-xsl
Pin: version 5.2.10*
Pin-Priority: 991

Then you do "apt-get update" and the right packages are either pulled in due to installing them directly or indirectly through dependencies.

That works fine for apt-get.

If you are using aptitude instead of apt-get, then skip the above pinning file, and tell aptitude to hold the version of PHP to what you have installed.

aptitude hold `dpkg -l | grep php | awk '{print $2}'`

Alternatively, you can do:

for PKG in `dpkg -l | grep php | awk '{print $2}'`
do
  echo "$PKG hold" | dpkg --set-selections
done

I am not sure if the above two commands are enough to stop aptitude from upgrading the packages to 5.3 if a new release comes by. If you find a better way to let aptitude handle this, please post it below.

Resources

Some further links that you may want to check:

  • Slicehost forum post on the topic of PHP 5.2 downgrade, since PHP 5.3 breaks many applications, such as Drupal, MediaWiki and others.
  • Another article on Lucid 10.04 for web development. Touches on the PHP downgrade topic.

error 404

Am I doing something wrong. I get an error 404 not found for all the repositories of these 5.2 packages. I can't find php5.2 any where on the internet.

oh its working now

Oh I guess even though they show as not found during apt-get update, they instal ok.

Built from source

On the couple of machines where I needed PHP 5.2 in addition to PHP 5.3, I built it from source and put it in /opt/php5.2. This makes some things a bit tricky (e.g. one has to pass --with-php-prefix=/opt/5.2 or something similar to ./configure when building extensions from source), but overall it seems most appropriate if a hybrid environment is needed. One then needs to use PHP-FPM or any of the CGI variants (my order of preference is PHP-FPM, fcgid) to configure each site appropriately. One can use mod_php in conjunction with this approach, but unless dealing with an overpowered server, that'd be a waste of resources.

All this said - PHP 5.2 has been EOL for a while, so it's better just to fix (or live with) the code and run PHP 5.3. Probably more value for money that way than changing the server environment.

Another possibility for

Another possibility for ubuntu 10 is to download 5.2.whatever-you-need sources from php.net & compile it. It worked for me, in contrary to Ralph's solution (with all respect :) Any ./configure errors can be resolved by apt-getting appropriate packages, mostly -dev sorts of. One who tries this must be however aware that apt will sometimes try to install php5 i.e. when installing zendframework package, so any addons to php must be installed manually I think.

Ubuntu Natty 11.04

The option #1, the script, still works in ubuntu Natty, just replace the word "lucid" by "natty" in the script and you are done!

The APT repository is no

The APT repository is no longer valid as it's latest update is almost a year ago. That's a lot of security bugs in PHP unfixed...

Has anyone been able to

Has anyone been able to install php5-memcached? It is part of the Mercury installation on this page http://library.linode.com/application-stacks/project-mercury/ubuntu-10.04-lucid.
I initially got this message when I tried to install it I got this message

php5-memcached: Depends: phpapi-20090626+lfs

Then I added the info below to my /etc/apt/preferences.d/php

Package: php5-memcached
Pin: version 1.4.2*
Pin-Priority: 991

And now I'm getting this message

Package php5-memcached is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
E: Package php5-memcached has no installation candidate

I'm not sure of what to do anymore. I also have similar issue with php-apc.

Issues

Hello,

I am having a problem installing php5-mcrypt from repository in approach #3. The extension installed successfully but the .so file is looked for in /usr/lib/php5/20060613 instead of /usr/lib/php5/20090626.

I tried making a symlink in the 20060613 folder, however that resulted in this error:

PHP Warning: PHP Startup: mcrypt: Unable to initialize module
Module compiled with module API=20090626, debug=0, thread-safety=0
PHP compiled with module API=20060613, debug=0, thread-safety=0
These options need to match
in Unknown on line 0

It seems as if the extension is built incorrectly or php is looking for it in the wrong place. Is there a way to change where an .so file is looked for?

Thanks!

Installing for Maverick

I spent hours following this guide to install PHP 5.2 on Maverick, since I know there is Maverick folder in the Janke repository. But unfortunately the Packages files are empty.

So, anyone has the repository for Maverick?

zend 5.2 server ce

Just install Zend 5.2 Server Community Edition, and get php5.2

If you need xdebug, use approach 1

> Approach 3: Proper PHP 5.2 packages, compiled for Lucid, installed from a repository

The janke repository doesn't contain php5-xdebug. If you need that, I believe you need to use the karmic sources (Approach 1).

AWESOME JOB

Awesome, great, splendid, brilliant, 5 stars...

Approach 3 worked for me.

"apt-get update" did not installed php5.2

"apt-get update" did not installed php5.2

I have the package "python-software-properties" installed

after "add-apt-repository ppa:txwikinger/php5.2" I got :

robson@robson-desktop:~$ sudo add-apt-repository ppa:txwikinger/php5.2
Executing: gpg --ignore-time-conflict --no-options --no-default-keyring --secret-keyring /etc/apt/secring.gpg --trustdb-name /etc/apt/trustdb.gpg --keyring /etc/apt/trusted.gpg --primary-keyring /etc/apt/trusted.gpg --keyserver keyserver.ubuntu.com --recv F91BF66D1CA9C138944C09AF13C360CC9CC59506
gpg: requesting key 9CC59506 from hkp server keyserver.ubuntu.com
gpg: key 9CC59506: "Launchpad PPA for Ralph Janke" not changed
gpg: Total number processed: 1
gpg: unchanged: 1

Then I created the php file on /etc/apt/preferences.d/php.

And then after "apt-get update" I got :

Hit http://br.archive.ubuntu.com lucid Release.gpg
Ign http://br.archive.ubuntu.com/ubuntu/ lucid/main Translation-en_US
Ign http://br.archive.ubuntu.com/ubuntu/ lucid/restricted Translation-en_US
Ign http://br.archive.ubuntu.com/ubuntu/ lucid/universe Translation-en_US
Ign http://br.archive.ubuntu.com/ubuntu/ lucid/multiverse Translation-en_US
Hit http://br.archive.ubuntu.com lucid-updates Release.gpg
Ign http://br.archive.ubuntu.com/ubuntu/ lucid-updates/main Translation-en_US
Ign http://br.archive.ubuntu.com/ubuntu/ lucid-updates/restricted Translation-en_US
Ign http://br.archive.ubuntu.com/ubuntu/ lucid-updates/universe Translation-en_US
Ign http://br.archive.ubuntu.com/ubuntu/ lucid-updates/multiverse Translation-en_US
Hit http://br.archive.ubuntu.com lucid Release
Hit http://br.archive.ubuntu.com lucid-updates Release
Hit http://br.archive.ubuntu.com lucid/main Packages
Hit http://br.archive.ubuntu.com lucid/restricted Packages
Hit http://br.archive.ubuntu.com lucid/main Sources
Hit http://br.archive.ubuntu.com lucid/restricted Sources
Hit http://br.archive.ubuntu.com lucid/universe Packages
Hit http://br.archive.ubuntu.com lucid/universe Sources
Hit http://br.archive.ubuntu.com lucid/multiverse Packages
Hit http://br.archive.ubuntu.com lucid/multiverse Sources
Hit http://br.archive.ubuntu.com lucid-updates/main Packages
Hit http://br.archive.ubuntu.com lucid-updates/restricted Packages
Hit http://br.archive.ubuntu.com lucid-updates/main Sources
Hit http://br.archive.ubuntu.com lucid-updates/restricted Sources
Hit http://br.archive.ubuntu.com lucid-updates/universe Packages
Hit http://br.archive.ubuntu.com lucid-updates/universe Sources
Hit http://br.archive.ubuntu.com lucid-updates/multiverse Packages
Hit http://br.archive.ubuntu.com lucid-updates/multiverse Sources
Hit http://security.ubuntu.com lucid-security Release.gpg
Ign http://security.ubuntu.com/ubuntu/ lucid-security/main Translation-en_US
Hit http://ppa.launchpad.net lucid Release.gpg
Ign http://ppa.launchpad.net/txwikinger/php5.2/ubuntu/ lucid/main Translation-en_US
Hit http://archive.canonical.com lucid Release.gpg
Ign http://archive.canonical.com/ubuntu/ lucid/partner Translation-en_US
Ign http://security.ubuntu.com/ubuntu/ lucid-security/restricted Translation-en_US
Ign http://security.ubuntu.com/ubuntu/ lucid-security/universe Translation-en_US
Ign http://security.ubuntu.com/ubuntu/ lucid-security/multiverse Translation-en_US
Hit http://security.ubuntu.com lucid-security Release
Hit http://ppa.launchpad.net lucid Release
Hit http://archive.canonical.com lucid Release
Hit http://security.ubuntu.com lucid-security/main Packages
Hit http://ppa.launchpad.net lucid/main Packages
Hit http://archive.canonical.com lucid/partner Packages
Hit http://security.ubuntu.com lucid-security/restricted Packages
Hit http://security.ubuntu.com lucid-security/main Sources
Hit http://security.ubuntu.com lucid-security/restricted Sources
Hit http://security.ubuntu.com lucid-security/universe Packages
Hit http://security.ubuntu.com lucid-security/universe Sources
Hit http://archive.canonical.com lucid/partner Sources
Hit http://security.ubuntu.com lucid-security/multiverse Packages
Hit http://security.ubuntu.com lucid-security/multiverse Sources
Reading package lists... Done

After this I restarted Apache and tried a test.php page, and the localhost gave no answer.
Did I miss something ? I'm new to Linux and this is my first LAMP instalation on Linux.

"apt-get update" did not installed php5.2

Sorry for the prior question. I have just realized that "apt-get update" simply update the repository.
Now I succeeded. The php5.2 is installed.

How did you succeed? Having

How did you succeed? Having similar problem.

Kind Regards

Mcrypt issues

For whatever reasons I had issues with mcrypt. Had to download a copy from here and install it manually http://packages.ubuntu.com/hu/karmic/i386/php5-mcrypt/download

Hi, Can you run me through

Hi, Can you run me through the method of installing mcrypt for this?

Thanks!

mcrypt/phpmyadmin

Thanks for really useful instructions. They pretty much worked for me.

I also had to install mcrypt manually. I followed the link above, but remember to check your architecture. I'm on amd64 so the link for me was:

http://packages.ubuntu.com/hu/karmic/amd64/php5-mcrypt/download

Download the package and then run:

sudo dpkg -i php5-mcrypt_5.2.6-0ubuntu2_amd64.deb

dpkg complained "Package libltdl7 is not installed"

so:

apt-get install libltdl7

which ran successfully, and also finished setting up php5-mcrypt.

then:

apt-get install phpmyadmin

and all is well.

Avoid installing from .deb

With Debian/Ubuntu, you should avoid installing from a downloaded .deb file. The reason is that you bypass the repository, which already has everything built with correct dependencies.

I will ping Ralph to add php5-mcrypt to his PPA repository, if it is missing.

If you must install something directly from a .deb file, then you should first install the gdebi-core package, and then use the command:

sudo gdebi yourpackage.deb

This will check the repository for dependencies, and install any that are required before installing your local .deb.

modules

HI there,

thanks for the great PPA the downgrade is working great.

There is still the issue with mcrypt and imap which cant be installed (or updated) due to missing dependencies or installation candidates.

The following packages have unmet dependencies:
php5-imap: Depends: phpapi-20090626+lfs
E: Broken packages

E: Package php5-mcrypt has no installation candidate

also phpmyadmin cant be installed due to issue with mcrypt.

Please be so kind and notify the maker of the PPA to update it and perhaps adds all other common php5 modules

kind regards
Tibor

php5-mcrypt now available

Ralph has made php5-mcrypt available on the PPA, so that should work fine now.

As for php5-imap, it is more complicated because of dependencies. He will work on it as his time permits.

Mcrypt still not working, unmet dependencies

Ralph seems to have added the php5-mcrypt package to the PPA but it is not working

See following errors when installing php5-imap php5-mcrypt

The following packages have unmet dependencies:
php5-imap: Depends: phpapi-20090626+lfs
E: Broken packages

I cannot install

mcrypt is not working

mcrypt is not working yet.

Asking for the same dependencies.

php5-mcrypt and php5-imap

I have finally managed to fix the problems with the installation of php5-mcrypt and php5-imap from my repository. However, it needs to be noted that the versions of those packages are 5.2.6, hence the pinning in the /etc/apt/preferences file must be done accordingly! ( original post)

preferences pinning example

For those that you do not understand what txwikinger is talking about (as he neither does not put an example in the link) here you are:

Package: php5-imap
Pin: version 5.2.6*
Pin-Priority: 991

Package: php5-mcrypt
Pin: version 5.2.6*
Pin-Priority: 991

It lets me install both packages from his repository.

I am not sure if it is actually needed but I have run:

apt-get update

after doing the change.

Adrian Gibanel
IT Manager

When I use Drush, I now get

When I use Drush, I now get this error (and approximately the same thing in phpmyadmin):

PHP Warning: PHP Startup: mcrypt: Unable to initialize module
Module compiled with module API=20090626, debug=0, thread-safety=0
PHP compiled with module API=20060613, debug=0, thread-safety=0
These options need to match
in Unknown on line 0

PHP Warning: PHP Startup: mcrypt: Unable to initialize module
Module compiled with module API=20090626, debug=0, thread-safety=0
PHP compiled with module API=20060613, debug=0, thread-safety=0
These options need to match
in Unknown on line 0

Any ideas why anyone?

Ran into a situation where I

Ran into a situation where I HAD to get mcrypt working properly and what do you know, installing the karmic package did the trick...until the Lucid version's fixed, I guess that is the way to go.

xdebug

Hi guys, thanks for the great repo.

could you add some time xdebug? it depends on a newer phpapi version and doesnt install :(

thanks so much
Tibor

Use pecl

You can install XDebug directly from pecl

This command should do it,

pecl install xdebug

More instructions in our other article: Setting up XDebug/DBGp on Debian/Ubuntu. Although it was written for older versions, it should work with a few modifications.

fix

Hi,

I corrected the script to correct some problems :
1/ pin packets not installed for futures install
2/ use the /etc/apt/preferences instead /etc/apt/preferences.d/php (bug aptitude)
3/ test the presence of the pin in the preference file


#! /bin/sh -e
# Script to install PHP 5.2 from 9.10 on 10.04
# And pin it so it does not get updated
# package available and may be installed later
PKGSAVAILABLE="`aptitude search -F '%p' php5` php-pear"
PKGSINSTALLED=`dpkg -l | grep php | awk '{print $2}'`
aptitude remove $PKGSINSTALLED
# disable duplicate entries
grep 'ubuntu.com' /etc/apt/sources.list | sed s/lucid/karmic/g > tee /etc/apt/sources.list.d/karmic.list
for PACKAGE in $PKGSAVAILABLE
do
# aptitude seems to use /etc/apt/preferences but not /etc/apt/preferences.d/*
# cf https://bugs.launchpad.net/ubuntu/+source/aptitude/+bug/508545
if egrep -q "Package: ?${PACKAGE}$" /etc/apt/preferences
then
echo "$PACKAGE already in preference"
else
echo "\nPackage: ${PACKAGE}\nPin: release a=karmic\nPin-Priority: 991" >> /etc/apt/preferences
fi
done
aptitude update
aptitude install $PKGSINSTALLED

Hey thanks, this script fixed

Hey thanks, this script fixed problems when I did a upgrade recently with aptitude. I also added to preferences instead of under preferences.d

Had to use pinning

I am happy that Ralph Janke made the effort of building a PHP 5.2 repository for Lucid, but unfortunately it's missing an APC package. Until he adds one, I go the route of pinning PHP to the Karmic packages.

Alternate way

No need for installing APC as a debian package.

You can always install it using pecl.

See installing APC on GNU/Linux Ubuntu and Debian. Although it says 7.10, this process works with 8.04, and should work with 10.04 as well.

The moral of the story..

... is I think, to always develop with your PHP error reporting set to E_ALL rather than the more common E_ALL & ~E_NOTICE. This will show you where you are using depreciated techniques which would've caught 90% of problems of moving from 5.2 to 5.3.

The moral of the Story

You're right. But in my case ZEND is involved too. Zend has a new Loader/Optimizer for PHP5.3, but the PHP Code i develop has to run on older PHP Versions too. I cannot maintain 2 Versions, so im stuck with PHP5.2 to be the maximum Version - unfortunately!

Good advice

That is really good advice, and everyone should be doing that.

The reality is, not many have been doing it.

Also this applies only to your own code, not code that you download from drupal.org.

When you download the code, you want to think about long term maintenance for it. Forking it makes you an island, and makes it hard to get synched up with future releases. You can mitigate some of that by having them under revision control, but it is never fully automated nor labor free.

Therefore, for less work in the future, either use PHP 5.2 for now, and wait for the problem modules to be 5.3 compatible.

Yeah I didn't mean to imply

Yeah I didn't mean to imply forking. I meant "we module maintainers", and "we custom module writers" should be doing our development with all PHP errors, warnings, notices, etc. blazing in our faces.

Why?

Why should I downgrade a perfectly stable and powerful system for a few lazy programmers who had one year to make minor fixes to their code. I approve with Ubuntu which in 10.04 is forcing them to do this. I am using Drupal which for me does not have any problem. Only a few modules I use are problematic and give errors but they can be avoided, and I made them hide the errors for the general public and record them only o=in the log. Everywhere I hear how to downgrade, downgrade, downgrade... People, upgrade is good...

Reality vs. theory

Stability trumps shininess ...

When you are faced with several modules that you are running not working on the new LTS, then what are your options?

a) Stay on the old LTS (it is still supported for many years to come).

b) Fix all the modules that are broken, and then you are either left with your own fork of them, or have to do yet extra work to contribute back the patches.

c) Downgrade to PHP 5.2.

All these are temporary solution, and will vanish when the modules are upgraded to be PHP 5.3. It is just a matter of time. Until then, these are the options.

So ultimately, which distro version?

I am using 9.10 on my dev box. Working flawlessly.
Would you recommend using 9.10 in production, or moving to 10.04?

Thank you - and I really appreciate the great articles and presentations you have given!

-johnc

LTS vs. non-LTS, PHP 5.2 vs. PHP 5.3

I always run production sites on an LTS version. At present 8.04 LTS has all what I need. This means PHP 5.2, not PHP 5.3 which has issues with some applications (e.g. some contributed modules in Drupal).

Obviously 10.04 is also LTS and will last 2 years longer than 8.04, but it has PHP 5.3 only on it. PHP 5.2 is available through Ralph Janke's PPA, but I am hesitant to try that for production sites.

So, I am staying with 8.04 until the above modules are PHP 5.3 safe, then I will jump to 10.04.

In all cases, don't use non-LTS versions, since they are short lived and you will lose even the repository after 18 months from release.

Problems with Firebird

See following errors when installing php5-interbase

The following packages have unmet dependencies:
php5-interbase: Depends: phpapi-20090626+lfs
E: Broken packages

I cannot install

You need to be sure that all

You need to be sure that all packages are added to the file in /etc/apt/preferences.d or you will run into broken dependencies.

Any further luck with this? I

Any further luck with this? I am really desperate to get php5-interbase to install.

Is your Drupal or WordPress site slow?
Is it suffering from server resources shortages?
Is it experiencing outages?
Contact us for Drupal and WordPress Performance Optimization and Tuning Consulting