247

I'm new to the composer and I would like to know the difference between require and require-dev. The composer website doesn't offer a good explanation the difference between these two.

The part that I don't get is Lists packages required for developing this package, or running tests, etc. from Composer Official Docs.

slier
  • 5,855
  • 5
  • 31
  • 51

3 Answers3

239

The require-dev packages are packages that aren't necessary for your project to work and shouldn't be included in the production version of your project.

Typically, these are packages such as phpunit/phpunit that you would only use during development.

Scott Tesler
  • 38,148
  • 4
  • 23
  • 20
  • 32
    How does composer know the it is in a dev evironment and should use `require-dev`? – stephen Dec 25 '15 at 19:41
  • 10
    @surfer190 my understanding is that composer doesn't know; you need to execute composer install or update with the --no-dev option in your live environment to avoid installing the dev packages, but I might be wrong. – Jose B Jan 14 '16 at 16:23
  • Do you even want composer to know? – Cas Bloem Mar 02 '16 at 10:44
  • 4
    @surfer190 By default, "dev" dependencies are installed for the package where you're running `composer install` (what composer terms the "root package"), but not for other packages installed as dependencies. – Nathan Craike Apr 01 '16 at 04:27
  • I think the real question here is "Will having packages in `require-dev` make Composer complain on `composer update|require` for packages in `require`? – Brandon Dec 08 '16 at 15:35
  • @JoseB wow thanks, that's what I needed to know :) – A.Soliman Mar 09 '20 at 12:41
56

seems clear to me:

require

Lists packages required by this package. The package will not be installed unless those requirements can be met.

require-dev (root-only)

Lists packages required for developing this package (1), or running tests, etc. The dev requirements of the root package only will be installed if install is run with --dev or if update is run without --no-dev.

http://getcomposer.org/doc/04-schema.md


1. the packages used to develop a package

cyberhicham
  • 487
  • 1
  • 10
  • 24
  • 7
    It's still not clear which package "this package" is referring to. – Scott Tesler Oct 17 '13 at 04:16
  • 1
    @ScottDavidTesler **this package** the package you provide in your `composer.json` file like `"require-dev": { // this package('s) }` – Rahil Wazir Jun 25 '14 at 13:06
  • 10
    @Dagon Who cares how old the thread is? People will still look at it, possibly for many years. Stack overflow is not a normal kind of forum where messages get lost after a few days. – rjmunro Oct 06 '14 at 14:45
  • 5
    "The dev requirements of the root package only will be installed if install is run with `--dev`…" This has since been changed in the linked documentation to: "The dev requirements of the root package are installed by default." The above extract is out of date. – Nathan Craike Sep 15 '16 at 05:48
54

The key distinction is that Composer will only install require-dev dependencies for the "root package" – the directory where you run composer install. The documentation describes this as:

The root package is the package defined by the composer.json at the root of your project. It is the main composer.json that defines your project requirements.

…and the require-dev documentation specifies that it is "root-only".

In practice, this means that a package's require-dev dependencies aren't used if the package is being installed as a dependency for something else (ie it's installed to another project's vendor folder).

So if you have phpunit in the require-dev list for YourProject, and I clone down YourProject and run composer install in the yourproject/ directory, Composer will install phpunit to yourproject/vendor/, because it's likely I'm doing some development on YourProject. As part of doing development I'll probably want to run YourProject's test suite, and to do that I'll need phpunit.

But, if I add YourProject as a dependency of MyProject, installing the myproject package will install the yourproject package as well, but it will not install phpunit.

You can override this behaviour with the --dev and --no-dev options, but the default behaviour is based on whether the package concerned is the root package.

Nathan Craike
  • 4,377
  • 2
  • 21
  • 19