3 ways to install PHPUnit in Ubuntu 22.04 or 20.04 LTS

PHPUnit is a software testing framework published under a GPL license for PHP coders. It is popular among PHP developers to write custom tests for their code to benchmark its performance. In this way, they can be identified whether their app or code written in PHP programming language can withstand different scenarios and how it will behave if some changes are made.

PHPUnit can help you to:

  • Allow testing a particular piece of PHP code in isolation.
  • Let’s review whether the code can work in different conditions as expected.
  • Helps in identifying issues before your users do, which ultimately saves time and money on debugging later.
  • Ensure backward compatibility across versions of your software.

Why Use it?

PHPUnit offers an automated diagnostics approach to unit testing that saves significant time and developers’ efforts to identify bugs. Also, automating testing not only saves time but furthermore reduces the cost of maintenance by providing faster feedback cycles. Hence, the problems can be found earlier in development life cycles or during future upgrades.

Not only time and cost, but this also, prior testing of the code ensures the software’s safety. Ensuring security and data integrity while testing rather than simply focusing on particular areas raised by human reviewers who may not be concerned with more subtle elements that could ultimately prove disastrous downstream if left unaddressed.

Steps to install PHPUnit on Ubuntu 22.04 | 20.04 Linux

There are three ways to install PHPUnit in Linux one is by directly downloading the required version of this testing framework from its official website and the others are by using APT and with the help of PHP-Composer.

1. Update Ubuntu Linux

Go to your Ubuntu command terminal and start running the system update command. That will install the latest available security and application packages.

sudo apt update && sudo apt upgrade

2. Install some PHP dependencies

Well to run PHPUnit to test code, PHP language must be installed on your system along with a few dependencies/extensions as shown in the below command:

sudo apt install php-cli php-json php-mbstring php-xml php-pcov php-xdebug

#1st method using APT package manager:

3. Install PhpUnit on Ubuntu 22.04 | 20.04

The long-term supported version of the PHPUnit software testing framework is available to install using the default system repository and APT packager manager in Ubuntu. Therefore, in your command terminal you just need to type:

sudo apt install phpunit
Install PhpUnit on Ubuntu 22.04

#2nd method using PHPUnit.phar

4. Download and configure Phar of PhpUbnit

In this method, we directly download the PHPUnit available in Phar format to easily run on Linux systems including Ubuntu. As compared to the APT method in this one we can download and use the latest as well as old versions of the PHPUnit, if required.

In your terminal use the given command. While writing the article, the latest stable version was Phpunit-10. If you want Phpunit-11 or PHPUnit-9 then replace the version number in the given command.

wget -O phpunit.phar https://phar.phpunit.de/phpunit-10.phar

Make the downloaded Phar file executable:

chmod +x phpunit.phar

Move it to the local bin directory, so that we can use it anywhere from the terminal.

sudo mv phpunit.phar /usr/local/bin/phpunit

Reload Bash session:


Check PHPUnit version

phpunit --version

#3rd Method using PHP-composer

5. Install PHP-Composer

For those who already have Composer on their system or want to use php-composer to install PHPUnit, here are the steps to follow:

Download the Composer installation script:

 curl -sS https://getcomposer.org/installer -o composer-setup.php

Start the installation process:

sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

6. Install PHPUnit using Composer

Once the Composer is on your system run:

composer global require phpunit/phpunit

To use PHPUnit globally after installing through composer, use:

echo 'export PATH="$PATH:$HOME/.config/composer/vendor/bin"' >> ~/.bashrc

reload session


7. Test some PHP code

Let’s confirm PHPUnit is working by testing a simple piece of PHP code. Here is that:

nano first.php

Add the following code:

In “class firstfirst is the name of the file. So, replace it if your code file name is different.

class Rectangle {
  public $width;
  public $height;

  public function area() {
    return $this->width * $this->height;

// PHPUnit test

class first extends \PHPUnit\Framework\TestCase {
  public function testArea() {
    $rectangle = new Rectangle();
    $rectangle->width = 5;
    $rectangle->height = 10;

    $this->assertEquals(50, $rectangle->area());

save the file by pressing Ctrl+X, Y, and then Enter key.

Now test your code using PHPUnit

phpunit first.php


PHPUnit Testing

For more info check out the official documentation.


Want to completely remove PHPUnit from your Ubuntu Linux, then as per the method you have used to install it, go for the uninstallation as well.

For the APT method:

sudo apt autoremove --purge phpunit


sudo rm /usr/local/bin/phpunit

For Composer

composer remove phpunit/phpunit

Other Articles

1 thought on “3 ways to install PHPUnit in Ubuntu 22.04 or 20.04 LTS”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.