How to Install PHPUnit on Ubuntu 24.04, 22.04 or 20.04

This guide will demonstrate how to install PHPUnit on Ubuntu 24.04, 22.04, or 20.04 LTS utilizing the command-line terminal and composer.

PHPUnit is a widely acclaimed unit testing framework for PHP, designed to provide developers with a systematic approach to writing and running tests for their PHP code. Its integration into the development workflow enhances code quality and reliability, ensuring that new changes don’t break existing functionality.

Here are some key features and highlights of PHPUnit:

  • Simplifies Test Creation: PHPUnit offers an intuitive interface for creating and managing test cases, making it accessible even for those new to testing.
  • Code Coverage Analysis: It provides detailed insights into which parts of your code are covered by tests, helping to identify untested or vulnerable areas.
  • Test Doubles: Supports mock objects and stubs, enabling developers to simulate and control the behavior of complex dependencies.
  • Data-Driven Testing: Facilitates the execution of a single test case with multiple data sets, enhancing test thoroughness.
  • Flexible Configuration: Allows customization of test execution, including filtering tests, testing database interactions, and more.
  • Integration Support: Seamlessly integrates with continuous integration tools and various development environments, streamlining the testing process.
  • Extensive Documentation: Offers comprehensive guides and resources, aiding developers in maximizing the framework’s capabilities.
  • Community Support: Benefits from a robust community, providing a wealth of shared knowledge, plugins, and extensions to extend its functionality.

Transitioning to the technical aspects, let’s dive into how you can install PHPUnit on your Ubuntu system, ensuring your PHP projects maintain high standards of code quality and robustness.

Install PHPUnit on Ubuntu via Composer

Installing Composer Globally

Begin by downloading the latest version of Composer using the command below. This action fetches the Composer installer script and executes it using PHP:

curl -sS https://getcomposer.org/installer | php

If you encounter any issues, ensure that curl is installed on your system by running:

sudo apt install curl

Once you have downloaded Composer, move the composer.phar file to a directory within your system’s PATH to run Composer from any location in your terminal:

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

This command transfers the composer.phar file to the /usr/local/bin directory and renames it to composer, making it executable globally.

To confirm that Composer is installed correctly, execute the following command:

composer --version

Installing PHPUnit with Composer

After setting up Composer, you can install PHPUnit globally on your system using the command:

composer global require phpunit/phpunit

Executing this command installs PHPUnit, allowing you to leverage its functionalities for enhancing your PHP project’s testing regime.

Example output when installing:

Terminal output showing PHPUnit installation via Composer on Ubuntu 24.04, 22.04, or 20.04.
Terminal snapshot of PHPUnit being installed via Composer on Ubuntu 24.04, 22.04, or 20.04.

Add Composer Global Bin Directory to PATH on Ubuntu

Configuring the PATH Environment Variable

To utilize PHPUnit globally on your Ubuntu system, it’s essential to include the Composer global bin directory in your PATH environment variable. This adjustment permits the execution of PHPUnit commands from any directory on your server.

Execute the command below in your terminal to append the Composer global bin directory to your PATH:

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

This command effectively adds the Composer global bin directory to the end of your PATH environment variable, ensuring that the terminal recognizes the PHPUnit commands globally.

Verifying the PATH Update

To confirm that the update to your PATH is successful, display the current PATH variable with the command:

echo $PATH

Running this command outputs the contents of your PATH environment variable, now including the path to the Composer global bin directory. This verification step ensures that your system recognizes the updated PATH, facilitating global access to PHPUnit.

Verify PHPUnit Installation on Ubuntu

Running a Test Case to Confirm Installation

Once you’ve installed PHPUnit via Composer, it’s crucial to validate its installation by executing a test case. Start by creating a new PHP file named test.php in your server’s document root. Open this file with your preferred text editor and insert the following PHP code:

<?php

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

This code defines a simple test class extending PHPUnit’s TestCase class, with a single test method that asserts 2 + 2 equals 4.

After saving test.php, execute the test by running the following command in your terminal:

phpunit test.php

This command instructs PHPUnit to execute the test defined in test.php. If PHPUnit is properly installed, it will process the test file, execute the test case, and display the results, indicating whether the test passed or failed.

Running a PHPUnit test on Ubuntu 24.04, 22.04, or 20.04 displayed in terminal.
Execution of a PHPUnit test on Ubuntu 24.04, 22.04, or 20.04, showcasing the testing process.

Additional Tips with PHPUnit on Ubuntu

Updating PHPUnit

Keeping PHPUnit up to date is essential to leverage the latest features and bug fixes. To update PHPUnit to the most recent version, execute the following command in your terminal:

composer global update phpunit/phpunit

This command instructs Composer to update the PHPUnit package globally to its latest version, ensuring you have access to the most current functionalities and improvements. Regular updates can enhance the testing efficiency and reliability for your PHP projects on Ubuntu.

Removing PHPUnit

If you need to remove PHPUnit from your system, you can do so using Composer. This might be necessary when you want to declutter your development environment or resolve conflicts with other versions. To remove PHPUnit, use the following command:

composer global remove phpunit/phpunit

This command tells Composer to remove the global installation of PHPUnit. After executing this, PHPUnit will no longer be available in your system’s global scope, effectively cleaning up your environment and making room for different testing frameworks or versions if needed.

Conclusion

We’ve walked through the steps to install PHPUnit on Ubuntu 24.04, 22.04, or 20.04 LTS, setting you up to boost your PHP code’s quality with robust testing. Remember, regular testing with PHPUnit isn’t just about catching bugs; it’s about peace of mind and making your code bulletproof. So, dive in, write those tests, and watch your code’s reliability soar. It’s all about making your development process smoother and more efficient. Happy testing!

Leave a Comment


Your Mastodon Instance
Share to...