installing PHPUnit

installing PHPUnit

Created:18 Feb 2017 15:10:51 , in  Host development,  Web development

If you are any serious about writing code in PHP computer language you have tried some unit testing frameworks by now. Some of them are better than others, one that really stands out, both in terms of code quality and documentation is Sebastian's Bergmann PHPUnit. It's been around for more than a decade and has proven an invaluable tool for many PHP developers in this time.

PHPUnit can be downloaded from Sebastian's website. It comes in the form of a PHAR archive. PHP PHAR archives are whole applications reduced to a single file. What's cool about them, they can be installed easily and executed both via a web server or on the command line. PHPUnit is a command line application.

In this article I present a BASH script which makes it a breeze to install PHPUnit.

PHPUnit installing and executing script

The script below, called, can be run with no arguments, in such circumstances it installs current stable release of PHPUnit. It checks for root privileges before it begins installation process. In the case is invoked with an argument, which is PHPunit version, it makes a check for existence of PHPUnit in that version on Sebastian's Bergman website before it embarks on downloading and installing the software. The version bit is important here because not every PHPUnit version is compatible with every PHP version. For example, PHPUnit version 6.0 is not compatible with PHP in a version below 7.0. So make sure you know your PHP version before you begin. For PHP 5.6 you would want PHPUnit in version 5.7


#!/usr/bin/env bash

# This script installs PHPunit PHP testing framework by Sebastian Bergmann
# author : Sylwester Wojnowski
# www :

# Usage: [phpunit_version]
# When invoked with no parameter, installs the latest version (latest stable release) of PHPUnit
# If passed a version of PHPunit will try to install it. 
# Versions of PHPUnit can be found at

  [[ $(id -u) -ne 0 ]] && {
    echo "This script requires root privileges! Exiting ..."
    exit 1

  echo "installing phpunit ..."

  local puv=$1
  # download from
  wget -qO /dev/null -o /dev/null "${puv}.phar" 
  [[ $? -ne 0 ]] && {
    echo "Error!" 
    echo "Incorrect version of PHPunit given. Check for PHPunit versions."
    echo "Exiting ..."
    exit 1
  wget "${puv}.phar"
  chmod +x "phpunit${puv}.phar"
  mv "phpunit${puv}.phar" /usr/local/bin/phpunit

  [[ $? -eq 0 ]] && {
    echo "PHPunit installation process complete!"
  } || {
    echo "PHPunit installation process failed!"

download_and_install $1

Now, what you need to do to make work for you are the usual steps you carry out before executing a script on the command line. That is, you save the code above in a file called Then you make executable by changing its mode with chmod command. Finally, you execute the script as a privilaged user. It will install PHPUnit in /usr/local/bin with executable being PHPUnit.

chmod +x ./

The code above installs the latest stable version of PHPUnit. If, for example you need version 5.7, to supports PHP in version 5.6 or 7.x, then you would execute code below:

chmod +x ./
./ 5.7

To confirm that the installation process worked, execute:

phpunit --version


PHPUnit installation process is rather simple, still it can fail if you are not careful with it. My script makes the possibility of failure a bit less likely. The script has been designed to be a simple part of a broader suite of programs but it is also perfectly usable on its own. One thing to be aware of about it is, it contains no code, which verifies PHPUnit PHAR release using PGP signatures and SHA1 hashes. If you need that, Sebastian Bergman describes the process clearly in PHPunit documentation. He even provides a sample BASH script for the purpose.

This post was updated on 27 Apr 2017 16:13:16

Tags:  BASH ,  php ,  PHPUnit 

Author, Copyright and citation


Sylwester Wojnowski

Author of the above article, Sylwester Wojnowski, enjoys sWWW writing computer code in PHP, JavaScript and BASH, and some other things he wrote more on on the About page of this website.


©Copyright, 2021 Sylwester Wojnowski. This article may not be reproduced or published as a whole or in parts without permission from the author. If you share it, please give author credit and do not remove embedded links.

Computer code, if present in the article, is excluded from the above and licensed under GPLv3.


Cite this article as:

Wojnowski, Sylwester. "installing PHPUnit." From sWWW - Code For The Web .

Add Comment

Allowed BB Code - style tags: [b][/b], [i][/i], [code=text][/code],[code=javascript][/code],[code=php][/code],[code=bash][/code],[code=css][/code],[code=html][/code]

I constent to processing my data given through this form for purposes of a reply by the administrator of this website.

Recent Comments

Nobody has commented on this post yet. Be first!

Post navigation

  Arrays in BASH

  Skeleton for unit testing with PHPUnit