Installing PHPUnit on Ubuntu 11.04 (Natty Narwhal)

Unfortunately, PHPUnit does not work out of the box on Ubuntu 11.04 through apt-get. We also need to use PEAR to install the dependencies.

The Problem

The standard approach is to simply use apt-get or pear to install everything:

$ sudo apt-get install phpunit

After installing the package, you will most likely receive the following error:

PHP Fatal error: require_once(): Failed opening required 'PHP/CodeCoverage/Filter.php' (include_path='.:/usr/share/php:/usr/share/pear') in /usr/bin/phpunit on line 38

Installing Dependencies

Obviously, we’re missing a few dependencies as shown in the previous error. Fortunately, PEAR provides the --alldeps flag while installing so that we automatically download and install dependencies that are necessary.

Upgrade PEAR First

There’s one more problem: Ubuntu 11.04 comes with PEAR version 1.9.1 when we need version 1.9.2 for all dependencies to install. Before we go any further, we need to upgrade PEAR:

$ sudo pear upgrade pear

Install PHPUnit Dependencies

Now, we can install PHPUnit and it’s dependencies through PEAR.

$ sudo pear channel-discover pear.phpunit.de
$ sudo pear channel-discover components.ez.no
$ sudo pear channel-discover pear.symfony.com
$ sudo pear install --alldeps phpunit/PHPUnit

Make Sure It Works

After everything is complete, verify that phpunit works:

$ phpunit --version
PHPUnit 3.5.5 by Sebastian Bergmann.

Have fun unit testing with PHPUnit.

This entry was posted in Ingenuity and tagged , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
  • chris hickey

    Thank you for this! This has been a thorn in my side for weeks.

  • Justin Dorfman

    Thanks! Great tutorial.

    - @jdorfman

  • Edke

    This works flawlessly, thank you :)

  • gricso

    Thanks for you! Finally the working tutorials ;)

  • BillyBobThornton

    It won’t work.

    Still the same :
    PHP Fatal error: require_once(): Failed opening required ‘PHP/CodeCoverage/Filter.php’ (…)

    Both the PEAR and PHPUnit paths are properly det in the include_path but it’s just as if PHPUnit ignores it.

  • rakesh kumar jha

    It won’t work.
    if u know right syntax then please sent email i wait for you result….

  • http://blog.montmere.com Stephen J. Fuhry

    If you get this error:

    Failed to download pear/HTTP_Request2 within preferred state “stable”, latest release is version 2.0.0RC1, stability “beta”, use “channel://pear.php.net/HTTP_Request2-2.0.0RC1″ to install

    Do this instead of the last command:

    sudo pear install –alldeps phpunit/PHPUnit-beta

    It’s a weird pear thing with versioning.

  • Robert Knight

    Thank-you for documenting this – it saved me a fair amount of time.

  • http://scribu.net scribu

    Thanks for this tutorial.

    For reference, sudo apt-get install phpunit still doesn’t work properly in Ubuntu 11.10 (oneiric).

    Here’s the code from this walkthrough as a bash file, for convenience:

    https://gist.github.com/1252336

  • http://ertankayalar.com ertan kayalar

    thank you, it works!

  • david

    I am getting this error

    PHP Warning: require_once(File/Iterator/Autoload.php): failed to open stream: No such file or directory in /usr/share/php/PHPUnit/Autoload.php on line 45
    PHP Fatal error: require_once(): Failed opening required ‘File/Iterator/Autoload.php’ (include_path=’/usr/share/php/libzend-framework-php’) in /usr/share/php/PHPUnit/Autoload.php on line 45

    an ideas?

    • http://www.group64bits.co.cc Kamilo

      I got an error like this one too

      PHP Warning: require_once(File/Iterator/Autoload.php): failed to open stream: No such file or directory in /usr/share/php/PHPUnit/Autoload.php on line 45
      PHP Fatal error: require_once(): Failed opening required ‘File/Iterator/Autoload.php’ (include_path=’/usr/share/php/libzend-framework-php’) in /usr/share/php/PHPUnit/Autoload.php on line 45

      It requires the File library which is outside of the PHPUnit folder so i made a copy of File folder and placed it inside PHPUnit location and it worked smooth

  • MatteoSp

    still does not work, struggling with

    PHP Fatal error: Call to undefined method PHP_CodeCoverage_Filter::getInstance() in /usr/bin/phpunit on line 39

    any ideas?

    • Xueqiang Mi

      Have you solved the phpunit installation problem?
      I get the same issue now.

    • http://ricardomartins.net.br Ricardo Martins

      Just take a look at http://stackoverflow.com/questions/9186000/call-to-undefined-method-php-codecoverage-filtergetinstance at the Anthony’s reply…

      This fixed for me, either on 12.04.
      sudo apt-get remove phpunit

      sudo pear channel-discover pear.phpunit.de

      sudo pear channel-discover pear.symfony-project.com

      sudo pear channel-discover components.ez.no

      sudo pear update-channels

      sudo pear upgrade-all

      sudo pear install –alldeps phpunit/PHPUnit

      sudo pear install –force –alldeps phpunit/PHPUnit

  • MatteoSp

    I forgot to mention I’m on Ubuntu 11.10

  • charl

    Thanks guy, worked perfectly.

  • crtv

    ty

  • Yuri

    Excelente….

  • Pingback: » Resources for Installing PHPUnit PHP Software Craftsmanship

  • http://thiyagarajan.wordpress.com/ Thiyagarajan V

    Thanks, its helped me lot.

  • Rashendra

    This is greate , works well

  • http://chars19.blogspot.com Ardi

    Thanks, can i translate your tutorial to my blog?

    • http://www.giocc.com Gio Carlo Cielo

      Sure.

  • Florin

    Yet another big thank you!

  • http://henriquelopes.com.br Henrique

    Thanks! Great Tutorial!!

  • http://steffe.se Stefan

    Very clean and straight forward tutorial. Thanks :)

  • Pingback: Installing PHPUnit | Baligena.com

  • Seth

    Well done, thanks.

  • josh

    you are the man

  • http://blog.peterfisher.me.uk Peter Fisher

    Thanks, This was doing my head in

  • Des

    This is an excellent and concise guide.

    I owe you a cold drink if you ever visit spain… :-)

    des

  • http://javierbracero.blogspot.co.uk/ Javier

    Great tutorial.
    Just a comment, I needed to added the following channel as well in my Ubuntu: 12.04
    sudo pear channel-discover pear.symfony.com

  • Tran Khanh

    Gr8 post, however, you should change”
    sudo pear channel-discover pear.symfony-project.com
    to
    sudo pear channel-discover pear.symfony.com.

  • Ash

    Nice tut.

  • Japhet Perez

    Great job!

  • Boodaleh

    Great tutorial, thanx

  • http://tasit.com amir

    hey Dud, Thanks for your Guide .

  • trupti

    for folks that the above guide did not work, try doing this on top of all those commands on the top, it worked for me, also after the installation, i had to check that all my php installation files were intact, they had got messed up, and luckily i could copy them over from a colleague.
    sudo pear install –force –alldeps phpunit/PHPUnit

  • Pingback: The Power of FuelPHP: Oil, ORM, Validation (Part 1)

  • Pingback: Installing PHPUnit On Ubuntu (voca – thorn, flawlessly, concise) | BUYWORKSELL

  • Sethathi

    Thank you!

  • http://www.smattox.com Smattox

    You sir are a gentlemen and a scholar. This worked on my Ubuntu 12.04 running PHPUnit 3.5.5-2

  • ghostzero

    After finished install I had erro : “PHP Deprecated: Comments starting with ‘#’ are deprecated in /etc/php5/cli/conf.d/ming.ini on line 1 in Unknown on line 0″
    fix this error: “find /etc/php5/cli/conf.d/ -name “*.ini” -exec sed -i -re ‘s/^(\s*)#(.*)/\1;\2/g’ {} \;”
    find from: http://stackoverflow.com/questions/14074101/getting-comments-starting-with-are-deprecated-message-via-cli

  • About Gio

    I am a torrent of ingenuity (or insanity) with a myriad of innovations (sometimes fallacies) and a wealth of inspiration (possibly naiveté). My name is Gio Carlo Cielo Borje and I like puffer fish because they're just cooltalkin', highwalkin' and fastlivin'.

    I'm also twenty and a current student at UC Irvine for Computer Science.