How to deploy correctly when using Composer's develop / production switch?
Composer introduced a distinct separation between core dependencies and development tools through the require-dev block. In theory, this allows you to keep your live environment lean by installing debugging tools, testing frameworks, or data factories only when working locally. The canonical approach involves defining these utilities in a dedicated block within composer.json and invoking composer install --dev. This command ensures that packages listed under require-dev are fetched and installed, while omitting those strictly required for runtime.
However, the deployment landscape shifted significantly around 2013 when Composer altered the default behavior of its core commands. Prior to this change, running composer install without flags would respect the environment or ignore development packages depending on configuration. Post-change, composer install and composer update automatically resolve and install dependencies from both the require and require-dev sections unless explicitly told otherwise.
This shift creates a friction point for modern deployment pipelines. The most accepted strategy for production is to push the composer.lock file to your version control repository alongside your source code. This lock file acts as a contract, ensuring that every server in your infrastructure resolves to the exact same versions of every package defined in your project. On the production server, you execute composer install after pulling the latest code and lock file.
The problem arises immediately: if your composer.json contains any entries under require-dev, a standard composer install will pull them down onto your live server. This violates the principle of keeping production environments minimal and secure. It introduces unnecessary overhead, potential security risks from unused code, and confusion regarding which tools are actually available in the production context.
So, what is the correct way to deploy without installing -dev dependencies? The answer lies not in how you configure your composer.json, but in how you invoke the install command on the target server. You do not need to alter your dependency definition files; instead, you must adjust your deployment script or CI/CD pipeline configuration.
To achieve a clean production environment, you have two primary options depending on your workflow:
Use the
--no-devflag: The most direct solution is to executecomposer install --no-devon the production server. This command instructs Composer to resolve dependencies from bothrequireandrequire-devsections but explicitly skips installing the packages listed inrequire-dev. Yourcomposer.lockfile must be generated on a machine where these dev tools were present, or you must ensure your lock file is updated to reflect a state where dev packages are ignored during the install phase.Manage the Lock File Separately: A more robust strategy often involves generating two separate lock files. You can create a
composer.lockfor development that includes all tools needed for local work, and a separatecomposer.lock(sometimes namedcomposer.lock.prod) for production. During deployment, you push the production-specific lock file to your repository or download it directly from your artifact storage before runningcomposer install. This ensures the production server never sees the dev dependencies in its resolution graph at all.
If you choose the first option, be aware that your composer.lock file on your local machine will contain entries for development packages. When deploying, ensure your deployment script specifically calls composer install --no-dev after verifying the lock file integrity. Some teams prefer to remove dev dependencies from the lock file entirely before pushing to production by running composer update --no-dev, though this requires careful handling to avoid breaking version constraints for shared libraries.
It is also worth noting that tools like Laravel Company often provide dedicated commands or scripts to handle these distinctions, abstracting the flag usage into more intuitive deployment steps. If you are using a framework with built-in artisan tasks, you might find it helpful to define a custom command that runs composer install --no-dev specifically for the production environment.
Ultimately, the decision rests on your deployment philosophy. If you want to guarantee zero dev dependencies on the live server, the --no-dev flag is the standard, reliable mechanism provided by Composer itself. It preserves the separation of concerns without requiring complex lock file management, provided your local development workflow accepts that the production build will be slightly different from the local one.
