28

When you install or update a project with composer, you can tell it to skip the development related dependencies (tests, build tools, etc.) with the --no-dev flag

composer.phar update --no-dev

Without this flag, composer will always download the extra dependencies.

Is there any way (programmatically or otherwise) to tell composer to always skip the development dependencies? That is, is there anything real code that matches the pseudo code

//File: composer.json
//...
"no-dev":"true"
//...
dreftymac
  • 31,404
  • 26
  • 119
  • 182
Alana Storm
  • 164,128
  • 91
  • 395
  • 599

2 Answers2

23

In short: no - not, yet.

Composer's default installation mode is to install development dependencies.

As far as i know, there is only the CLI option --no-dev and no config option.

It's possible to define a config section in the composer.json of a project, see https://getcomposer.org/doc/04-schema.md#config

But a quick glance at the source code revealed, that there is no configuration directive for this. https://github.com/composer/composer/blob/master/src/Composer/Config.php#L22

{
    "config": {
        "no-dev": "true"
    }
}

+1 for this idea. It could be a useful addition to the Config class.

Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
  • 3
    agreed. Why isn't this added? Every time someone updates our production environment they tend to forget the --no-dev, installing a bunch of unneeded stuff – Blizz Apr 03 '15 at 06:31
  • 3
    I don't know. I referenced this Question over at [Composer Issues](https://github.com/composer/composer/issues/3008). It's now nearly a year old, but nobody addressed it, yet. Maybe i should contribute a little patch :) – Jens A. Koch Apr 03 '15 at 10:32
  • My guess is that because 'no-dev' is an installation option, not a project option (i.e. if you enshrined it in the `composer.json`, then you would be unable to install development libraries for... well... development.) Perhaps a better option would be the ability to set 'composer environments' with different installation options (so something like `"env" : { "production": { "no-dev": true, "optimize": true, "allow-update": false}, "testing": {"no-dev": false, "allow-update": false}}`) – samlev Aug 04 '16 at 22:10
  • The Composer file itself defines a list of DEV modules to be installed. Why should this same file be responsible for what packages are not installed on some systems? This is one file responsible for defining the packages of the code. – Jacques Jun 13 '22 at 13:25
  • "Why should this same file be responsible for what packages are not installed on some systems?" To install `dev` dependencies is the default, but the OP wants the opposite behaviour. If you hardcode the production config (no-dev=true), you basically toggle the default settings and require to set `--dev` to get the development dependencies. Putting it in `composer.json` is one way to make `no-dev` sticky, which is what the OP requested. Boils down to a dev/team decision, what the default install mode should be... "prod or dev by default?". – Jens A. Koch Jun 14 '22 at 15:30
11

This was really annoying, so I finally wrote a simple bash script which asks about environment and run correct command:

#! /bin/bash

read -p "Which environment use to deploy: (P)roduction (T)est (D)ev? (p/t/d): " -n 1 -r
echo

if [[ $REPLY =~ ^[^PpTtDd]$ ]]; then
    echo "Incorrect environment";
    exit 1;
fi

# tasks to run before composer install (svn up/git pull)

if [[ $REPLY =~ ^[Pp]$ ]]; then
    composer install --prefer-dist --no-dev --classmap-authoritative
elif [[ $REPLY =~ ^[Tt]$ ]]; then
    composer install --prefer-dist --classmap-authoritative
elif [[ $REPLY =~ ^[Dd]$ ]]; then
    composer install
fi

# additional tasks after composer install (clear cache, migrations, etc.)

Saved it in bin/deploy in project and added execute permissions. So now I'm using bin/deploy instead of composer install:

console

I also put other common tasks there (pull changes from VCS, clear cache, run migrations, etc.), so I have even less things to do and remember during deployment :).

rob006
  • 21,383
  • 5
  • 53
  • 74