How to Install PHPUnit on Ubuntu 22.04 | 20.04

PHPUnit is a widely-used testing framework for PHP developers, ensuring the reliability and functionality of their code by enabling automated testing. With PHPUnit, developers can easily create and execute unit tests for their PHP code, eliminating the need for manual testing. PHPUnit is a robust, reliable, and efficient testing framework trusted by developers worldwide.

This guide will demonstrate how to install PHPUnit using Composer on Ubuntu 22.04 Jammy Jellyfish or Ubuntu 20.04 Focal Fossa via the command-line terminal. Additionally, we will show you how to test if PHPUnit works correctly, so you can confidently use it to test your PHP code.

Step 1: Install PHPUnit via Composer

To install Composer globally on your system and make the installation permanent, follow these steps:

  1. Download the latest version of Composer using the following command:
curl -sS https://getcomposer.org/installer | php

If the command does not work, make sure curl is installed:

sudo apt install curl

This will download the Composer installer script and run it using PHP.

  1. Move the composer.phar file to a directory in your system’s PATH, allowing you to run Composer from anywhere in your terminal.
sudo mv composer.phar /usr/local/bin/composer

This command moves the composer.phar file to the /usr/local/bin directory and renames it to composer, making it executable from anywhere in your terminal.

  1. Verify that Composer is installed correctly by running the following command:
composer --version

Next, install PHPUnit with the following command:

composer global require phpunit/phpunit

Example output when installing:

Step 2: Add Composer Global Bin Directory to PATH

Add the Composer global bin directory to your system’s PATH environment variable to use PHPUnit globally. This will allow you to execute PHPUnit commands from any directory on your server.

To add the Composer global bin directory to your PATH, run the following command in your terminal:

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

This command appends the Composer global bin directory to your PATH environment variable.

You can verify that the Composer global bin directory has been added to your PATH by running the following command in your terminal:

echo $PATH

This command will display your system’s PATH environment variable, including the Composer global bin directory.

Step 3: Verify PHPUnit Installation

After installing PHPUnit via Composer, verify that it has been installed correctly and is working as expected. You can create a simple test file and run PHPUnit against it.

First, create a new file named test.php in your server’s document root directory using your preferred text editor. Then, copy the following code into the file:

<?php

class Test extends PHPUnit\Framework\TestCase
{
    public function testAddition()
    {
        $this->assertEquals(2+2, 4);
    }
}

Save the file and run the following command in your terminal:

phpunit test.php

If PHPUnit has been installed correctly, it will run the test and display the output.

Step 4: Updating PHPUnit

PHPUnit is constantly updated with bug fixes and new features. To update PHPUnit, you can run the following command in your terminal:

composer global update phpunit/phpunit

This command will update PHPUnit to the latest version available.

Conclusion

In summary, PHPUnit is a powerful testing framework that allows developers to test their PHP code efficiently and effectively. Following the step-by-step guide outlined above, you can easily install PHPUnit using Composer on Ubuntu 22.04 Jammy Jellyfish or Ubuntu 20.04 Focal Fossa via the command-line terminal. You can also test your installation to ensure that PHPUnit is working correctly. With PHPUnit, you can write automated tests that help you identify and fix errors in your code, leading to more reliable and functional PHP applications.

Additional Resources

If you’re interested in learning more about installing PHPUnit on Ubuntu or other related topics, here are some official links and resources you may find helpful:

Share to...