preferred-install: My favourite hidden composer feature

by Sebastian Kurfürst on 09.03.2022

I've just recently stumbled over a composer configuration which changed my life (for the better): preferred-install source.

The Problem

We are often developing open source packages in tandem with our projects. This means we add our own open source package (or a package which we contribute to) into the composer.json file - and then composer installs the package for us.

By default, composer installs in distribution mode, which means without the .git folder inside the folder. This is useful for production, but for developing packages, I'd rather like to have the .git folder around in my individual packages, so that I can push improvements easily to the upstream repository.

I've known about composer install --prefer-source - but this checks out every package via Git, being needlessly slow. And additionally I forget about this flag all the time.

The Solution: The global composer config file and preferred-install

Composer has a global config file (for me residing in ~/.composer/config.json) which can be modified via CLI:

composer config --global 'preferred-install.sandstorm/*' source

The line above says "for all packages starting with sandstorm/ in the composer name, please install them via source."

In my case, I've run the following commands:

composer config --global 'preferred-install.sandstorm/*' source
composer config --global 'preferred-install.flowpack/*' source
composer config --global 'preferred-install.neos/*' source

This automatically installs all sandstorm, flowpack and neos packages via source. Awesome :-)