Is there a way to uninstall dev dependencies with composer?
The short answer is no, not in the way you might expect if you are trying to selectively remove specific packages from the vendor directory without altering your composer.json. When dealing with development tools like PHPUnit, Pest, or Laravel Debugbar, the workflow often dictates that these packages exist only when you need them for testing locally. However, simply running a command to "uninstall" a dev dependency while keeping it listed in your configuration file creates a state of inconsistency that Composer does not support as a persistent configuration option.
To understand why this is difficult, we have to look at how Composer manages dependencies. When you run composer install, the tool reads the require-dev section of your composer.json and installs every package listed there into the vendor directory. If you want to remove a specific package from that directory without changing the source of truth (composer.json), you are essentially fighting against Composer's internal state management.
There is a command, composer uninstall, but it operates by removing the dependency from your manifest files first. It will not silently delete a vendor package while leaving the JSON entry intact. This happens because Composer relies on the composer.lock file to ensure reproducibility across environments. If you manually deleted vendor/package-name but left the entry in composer.json, running composer update or even composer install again would immediately reinstall that package, potentially overwriting any manual changes you made to the filesystem.
The most reliable method to achieve a "temporary" removal of dev dependencies is to modify your configuration dynamically using an array syntax within your composer.json. This approach allows you to conditionally include packages based on environment variables or platform specifics without physically deleting them from the file system permanently.
Consider a scenario where you want to remove PHPUnit only when running on production but keep it for local development. You can update your composer.json as follows:
{
"require-dev": [
{
"name": "phpunit/phpunit",
"version": "*",
"extra": {
"installer-name": "phpunit"
}
}
]
}
While this example shows a basic structure, the more common pattern for conditional dependencies involves using platform checks or environment variables. For instance, you might define a specific condition where require-dev is empty if an environment variable is set, effectively telling Composer to ignore those packages during installation on that specific machine.
Alternatively, some developers prefer to physically remove the package from the vendor directory and then run composer dump-autoload. This clears the autoloader cache for those specific classes, but it creates a fragile state. If you commit this to version control, your CI/CD pipeline will fail because the lock file will not match the filesystem. If you do not commit the change, you are just introducing noise into your working directory that must be cleaned up before the next merge.
The trade-off here is between convenience and consistency. Keeping dev dependencies installed always ensures that your development environment matches your production requirements regarding tooling, even if those tools are only used for testing. Removing them saves disk space but introduces friction when switching contexts or running tests on a machine where they are not listed in the configuration.
If you need to manage this on a shared hosting environment or a specific server where you cannot modify composer.json globally, you might look into using Composer's platform requirements more aggressively or utilizing a separate composer.lock file that excludes dev dependencies for that specific path. However, these are advanced workarounds that generally indicate a need to rethink your dependency management strategy rather than a simple command-line fix.
For teams using Laravel specifically, managing these environments often involves ensuring that the phpunit/php-code-coverage or similar heavy tools are only pulled when explicitly needed. If you find yourself constantly toggling these packages on and off, it might be worth exploring if your CI pipeline can handle installing them temporarily via a script before running tests, rather than maintaining a split state in your local repository.
Ultimately, the architecture of Composer favors a declarative approach where composer.json is the single source of truth for what should be installed. Attempting to deviate from this by uninstalling packages without updating the manifest leads to an unstable dependency graph that requires manual intervention every time you update or deploy.