Composer require specific version while ignoring package dependencies

Want to help support this blog? Try out Oh Dear, the best all-in-one monitoring tool for your entire website, co-founded by me (the guy that wrote this blogpost). Start with a 10-day trial, no strings attached.

We offer uptime monitoring, SSL checks, broken links checking, performance & cronjob monitoring, branded status pages & so much more. Try us out today!

Profile image of Mattias Geniar

Mattias Geniar, April 16, 2020

Follow me on Twitter as @mattiasgeniar

I’m working on some new versions of PHP packages, and for that I’m working of a temporary branch. I wanted to update my local composer packages, but ran into a package constraint.

$ php -d memory_limit=2G /usr/local/bin/composer require spatie/crawler dev-master#84f0b2c8c7d90dfb0689f043d3fa6c6333ebafef
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.
  Problem 1
    - spatie/mixed-content-scanner 3.1.0 requires spatie/crawler ^4.1.1 -> satisfiable by spatie/crawler[v4.x-dev].
    - spatie/mixed-content-scanner 3.1.0 requires spatie/crawler ^4.1.1 -> satisfiable by spatie/crawler[v4.x-dev].
    - spatie/mixed-content-scanner 3.1.0 requires spatie/crawler ^4.1.1 -> satisfiable by spatie/crawler[v4.x-dev].
    - Can only install one of: spatie/crawler[v4.x-dev, dev-master].
    - Installation request for spatie/crawler dev-master#84f0b2c8c7d90dfb0689f043d3fa6c6333ebafef -> satisfiable by spatie/crawler[dev-master].
    - Installation request for spatie/mixed-content-scanner (locked at 3.1.0, required as ^3.0) -> satisfiable by spatie/mixed-content-scanner[3.1.0].

The goal was to install the package spatie/crawler and pin it to commit 84f0b2c8c7d90dfb0689f043d3fa6c6333ebafef, which lives in the v5 branch. But, other packages depend on that crawler and wouldn’t let me.

One workaround appears to be to “fake” the package version, by aliassing it.

You can do so in your composer.json file:

{
    "require": {
        "spatie/crawler": "dev-master#84f0b2c8c7d90dfb0689f043d3fa6c6333ebafef as 4.2",
    }
}

This fakes that specific commit to be interpreted as version 4.2, which then passes the package requirements for other packages.

Note: there’s a --ignore-platform-reqs flag, but that doesn’t allow you to force overwrite package requirements, just skip local PHP version or extension checks.



Want to subscribe to the cron.weekly newsletter?

I write a weekly-ish newsletter on Linux, open source & webdevelopment called cron.weekly.

It features the latest news, guides & tutorials and new open source projects. You can sign up via email below.

No spam. Just some good, practical Linux & open source content.