5

i am working on multiple composer packages and a application which dose requries all the packages that i develop.

I would like to know how i can have a package with multiple version

  1. stable version for production
  2. development / master version for local development

i tried following config but it din't work

{
  "minimum-stability" : "dev",
  "require"           : {
    "varunsridharan/vsp-framework" : "^1.0",
    "wponion/wponion"              : "^1.0"
  },
  "require-dev"       : {
    "varunsridharan/vsp-framework" : "dev-master",
    "wponion/wponion"              : "dev-development"
  }
}

When i run composer install or composer install --no-dev i get the below output

Loading composer repositories with package information
Updating dependencies


  [Composer\DependencyResolver\SolverProblemsException]
  Problem 1
      - The requested package varunsridharan/vsp-framework ^1.0 exists as varunsridharan/vsp-framework[dev-master] but these are rejected by your constraint.
    Problem 2
      - The requested package wponion/wponion ^1.0 exists as wponion/wponion[dev-development] but these are rejected by your constraint.


install [--prefer-source] [--prefer-dist] [--dry-run] [--dev] [--no-dev] [--no-custom-installers] [--no-autoloader] [--no-scripts] [--no-progress] [--no-suggest] [-v|vv|vvv|--verbose] [-o|--optimize-autoloader] [-a|--classmap-author
itative] [--apcu-autoloader] [--ignore-platform-reqs] [--] [<packages>]...

Varun Sridharan
  • 1,983
  • 2
  • 20
  • 54

2 Answers2

8

Reason why composer has require and require-dev is to allow developers install packages that will allow them better debugging by using packages that are not suggested to be used in production. By adding a package on require-dev this package will not be required on production.

Ex. If you want to optimize you queries, see if you have any n+1 query problem etc you might want to install php-debugbar that will show you this info, but having this on production might expose your queries and will give sensitive info to bad guys.

Packages included on require will always be installed in development and in production and this is the reason you see the error The requested package xxxxx exists as xxxxx[dev-development] but these are rejected by your constraint.

To have different packages in DEV and PROD you need different composer.json files for each environment like @George said.

You can have one composer.json file that will be the production composer and one composer-dev.json that you can use for development.

On your local environment you will need to set value of COMPOSER env variable to composer-dev.json.

Simple way to do this is by running composer like this: COMPOSER=composer-other.json php composer.phar install

If you want to require a new packages you need to run the command twice, one time normally and one time by adding COMPOSER=composer-other.json before composer, to ensure that the package will be added both in dev and production.

P.S I do not know the reason why you need different packages for development and production, but it is suggested to have the same version of packages in dev and prod as having different versions might bring up bugs

Zenel Rrushi
  • 2,346
  • 1
  • 18
  • 34
  • The reason stems from the fact on working with a Main-Website AND Vendor Libraries as well. During the development cycle of new features your website required `dev-development` (or similar) and once you're having it stable to give it a tag. That means you have to do many changes/merges to composer.json just to being able to properly tag. And once that's done you hve to go back to dev-develop again. That's annoying to do. Having two different composer.json for dev/prod makes this much easier. – Sam Dec 08 '19 at 15:11
  • We've now resorted to using composer-scripts to manage this. Our `composer.json` is now the default we work with. And it's the development version because that's the one we're working on 99% of the time. `composer-production.json` (and .lock) are used for live-deployments . These are the scripts we use for people who may not know how to run the example commands: `"scripts" : { "checkout-prod": "git checkout master; COMPOSER=composer-production.json composer install", "checkout-dev": "git checkout development; composer install" }` And then simply `composer checkout-dev` – Sam Dec 08 '19 at 15:14
  • Since you are using `DEV` packages for development of new unstable features makes sense to have different versions. Happy to know that using 2 composer files is a perfect solution for you :) – Zenel Rrushi Dec 08 '19 at 15:44
  • 1
    Long short, the reason why you would need "different packages" in dev and prod is not that you want different *versions* per se, but rather that, on dev, you want to *develop*, and you do that on a git branch-- so that necessitates a composer that installs that git branch. And, of course, you don't want development branches on production, nor do you want development branches of stuff you aren't working on, so there you want a version (range) defined. – user151841 Jul 13 '21 at 12:33
6

You can try changing composer.json file in your dev environment. See Composer

COMPOSER=composer-dev.json php composer install

Reference Composer - specify `composer.json` path

George Myl
  • 301
  • 2
  • 5