How to install PHPUnit using Composer?

by jasen_gottlieb , in category: PHP General , 9 months ago

How to install PHPUnit using Composer?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer


by ryleigh , 8 months ago


To install PHPUnit using Composer, you can follow these steps:

  1. Install Composer: If you haven't already installed Composer, go to the Composer website ( and follow the instructions for installation on your operating system.
  2. Create a new project directory: Open a terminal, navigate to the directory where you want to create your project, and run the following command:
$ mkdir project-name
$ cd project-name

  1. Initialize a new Composer project: Run the following command to initialize a new composer project in the current directory:
$ composer init

This command will guide you through creating a composer.json file, which defines the dependencies for your project.

  1. Add PHPUnit as a dependency: Once the initialization is complete, open the composer.json file in a text editor and add PHPUnit as a dependency. Add the following code to the "require-dev" section:
"require-dev": {
  "phpunit/phpunit": "^9.0"

This will install PHPUnit as a development dependency for your project.

  1. Save the composer.json file and close the editor.
  2. Install PHPUnit: Run the following command to install PHPUnit and its dependencies:
$ composer install

Composer will download PHPUnit and all its dependencies to the vendor directory in your project.

  1. Verify PHPUnit installation: To verify that PHPUnit is installed correctly, run the following command in your terminal:
$ vendor/bin/phpunit --version

This should display the version of PHPUnit installed in your project.

Now you can start using PHPUnit in your project for writing and running tests.