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.

37 Comments

  1. chris hickey
    Posted May 31, 2011 | Permalink

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

  2. Justin Dorfman
    Posted June 3, 2011 | Permalink

    Thanks! Great tutorial.

    - @jdorfman

  3. Edke
    Posted June 10, 2011 | Permalink

    This works flawlessly, thank you :)

  4. gricso
    Posted June 19, 2011 | Permalink

    Thanks for you! Finally the working tutorials ;)

  5. BillyBobThornton
    Posted July 12, 2011 | Permalink

    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.

  6. rakesh kumar jha
    Posted July 31, 2011 | Permalink

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

  7. Posted August 13, 2011 | Permalink

    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.

  8. Robert Knight
    Posted September 2, 2011 | Permalink

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

  9. Posted September 29, 2011 | Permalink

    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

  10. Posted October 2, 2011 | Permalink

    thank you, it works!

  11. david
    Posted December 30, 2011 | Permalink

    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?

    • Posted March 27, 2012 | Permalink

      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

  12. MatteoSp
    Posted January 25, 2012 | Permalink

    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?

  13. MatteoSp
    Posted January 25, 2012 | Permalink

    I forgot to mention I’m on Ubuntu 11.10

  14. charl
    Posted February 2, 2012 | Permalink

    Thanks guy, worked perfectly.

  15. crtv
    Posted February 10, 2012 | Permalink

    ty

  16. Yuri
    Posted February 23, 2012 | Permalink

    Excelente….

  17. Posted April 15, 2012 | Permalink

    Thanks, its helped me lot.

  18. Rashendra
    Posted April 22, 2012 | Permalink

    This is greate , works well

  19. Posted April 23, 2012 | Permalink

    Thanks, can i translate your tutorial to my blog?

  20. Florin
    Posted May 24, 2012 | Permalink

    Yet another big thank you!

  21. Posted May 25, 2012 | Permalink

    Thanks! Great Tutorial!!

  22. Posted May 26, 2012 | Permalink

    Very clean and straight forward tutorial. Thanks :)

  23. Seth
    Posted July 3, 2012 | Permalink

    Well done, thanks.

  24. josh
    Posted July 5, 2012 | Permalink

    you are the man

  25. Posted August 18, 2012 | Permalink

    Thanks, This was doing my head in

  26. Des
    Posted September 11, 2012 | Permalink

    This is an excellent and concise guide.

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

    des

  27. Posted October 6, 2012 | Permalink

    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

  28. Tran Khanh
    Posted November 26, 2012 | Permalink

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

  29. Ash
    Posted December 26, 2012 | Permalink

    Nice tut.

  30. Japhet Perez
    Posted December 27, 2012 | Permalink

    Great job!

  31. Boodaleh
    Posted January 2, 2013 | Permalink

    Great tutorial, thanx

  32. Posted January 22, 2013 | Permalink

    hey Dud, Thanks for your Guide .

  33. trupti
    Posted April 25, 2013 | Permalink

    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

3 Trackbacks

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • 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 nineteen and a current student at UC Irvine for Computer Science.