Understanding What composer install/update/require Do

Contents

Introduction

composer install after cloning, composer require when I want to add a dependency, and composer update now and then. That was about as far as my understanding of Composer went. I never really understood what each one does, so I sorted it out.

The environment is Docker's php:8.4-cli (8.4.23) and Composer 2.10.2. The output shown here is what I got on my machine.

What each command does

Command What it does composer.json composer.lock vendor/
composer install Installs exactly what the lock says Untouched Untouched (written if missing) Created
composer update Re-reads the constraints and works out the answer again Untouched Rewritten Updated
composer require Adds a dependency Rewritten Rewritten Updated

composer install

It installs according to the lock. If you delete vendor/ and run it, you get this.

Installing dependencies from lock file (including require-dev)
Verifying lock file contents can be installed on current platform.
Package operations: 2 installs, 0 updates, 0 removals
  - Installing psr/log (3.0.2): Extracting archive
  - Installing monolog/monolog (3.10.0): Extracting archive
Generating autoload files

As from lock file says, it reads which versions to install from composer.lock, not from composer.json. The second line checks that the lock contents will work in the current environment, and at the end it creates the vendor/autoload.php set of files.

Since the answer is written in the lock, the logic that works out which versions to install does not run. It also does not fetch package information for resolving, so if the cache is warm it succeeds even with the network cut off.

$ COMPOSER_DISABLE_NETWORK=1 composer install
Installing dependencies from lock file (including require-dev)
Verifying lock file contents can be installed on current platform.
Package operations: 2 installs, 0 updates, 0 removals
  - Installing psr/log (3.0.2): Extracting archive
  - Installing monolog/monolog (3.10.0): Extracting archive
Generating autoload files

It does not rewrite the lock even when the lock is stale

When I loosened the monolog/monolog constraint from ^3.10 to ^3.0 and ran install, it printed a warning, but composer.lock was not rewritten.

Verifying lock file contents can be installed on current platform.
Warning: The lock file is not up to date with the latest changes in composer.json.
You may be getting outdated dependencies. It is recommended that you run
`composer update` or `composer update <package name>`.
Nothing to install, update or remove

So install only installs what the lock says, and does not re-read the constraints. If you want the lock to catch up, you need to run update.

It stopped at a warning because I had changed the constraint of a package that was already in the lock. If you add a package that is not in the lock and run install, you get the same warning, and then it also fails with an error and stops.

Warning: The lock file is not up to date with the latest changes in composer.json.
...
- Required package "psr/container" is not present in the lock file.
This usually happens when composer files are incorrectly merged or the
composer.json file is manually edited.

Adding a dependency to composer.json and then running install does not work.

Without a lock, install does not do an install

If you run composer install without that lock, the first line says this.

No composer.lock file present. Updating dependencies to latest instead of
installing from lock file.

It says there is no lock, so it will update. I ran install, but what ran was the update process.

That means if you have scripts like the ones below, you might get behavior you did not intend.

"scripts": {
    "post-install-cmd": ["@php scripts/hello.php"],
    "post-update-cmd": ["@php scripts/updated.php"]
}

When I ran install with no lock, the one that got called was post-update-cmd.

Generating autoload files
> @php scripts/updated.php
post-update-cmd が呼ばれた

If you run it again once the lock exists, this time post-install-cmd is called. This can be the cause of an accident where you wrote post-install-cmd but it does not run.

(Aside) Where npm intuition trips you up

In npm, npm install writes package-lock.json, so it feels natural for install to create the lock. The difference is that Composer, when there is no lock, decides "the answer has not been settled yet" and falls back to update. Same name, different role.

composer update

If you run composer update in the same project, the output changes.

Loading composer repositories with package information
Updating dependencies
Nothing to modify in lock file
Writing lock file
Installing dependencies from lock file (including require-dev)
Nothing to install, update or remove
Generating autoload files
No security vulnerability advisories found.

There are three lines that install did not have.

Line What it does
Loading composer repositories with package information Fetches package information from Packagist
Updating dependencies Computes a combination of versions that satisfies the constraints
Writing lock file Writes the result to composer.lock

The easy-to-see part is that Installing dependencies from lock file follows after them. update resolves, writes the lock, and then installs. That means install runs only the second half of it.

No security vulnerability advisories found. also appears. It seems a vulnerability check also runs during update.

Where does the package information come from?

You can see what Loading composer repositories fetches by adding -vvv.

composer update -vvv

In a project with just one dependency, guzzlehttp/guzzle at ^8.0, extracting the fetched URLs gave this.

https://repo.packagist.org/packages.json
https://repo.packagist.org/p2/guzzlehttp/guzzle.json
https://repo.packagist.org/p2/guzzlehttp/promises.json
https://repo.packagist.org/p2/guzzlehttp/psr7.json
https://repo.packagist.org/p2/psr/http-client.json
https://repo.packagist.org/p2/psr/http-factory.json
https://repo.packagist.org/p2/psr/http-message.json
https://repo.packagist.org/p2/symfony/polyfill-php80.json
https://repo.packagist.org/p2/symfony/polyfill-php82.json

Only the first one, packages.json, is different in nature. It contains a template for where to fetch from.

$ php -r '$d = json_decode(file_get_contents("https://repo.packagist.org/packages.json"), true); echo $d["metadata-url"];'
https://repo.packagist.org/p2/%package%.json

The mechanism is to replace %package% with the package name and fetch only what is needed, one after another. Since the URLs are not written directly, you can change where it fetches from by writing repositories.

This count changes with how loose the constraints are. With the same guzzle, dropping ^8.0 for * made it 44. Even doctrine/annotations and symfony/validator show up. That is because old versions of guzzle remain as candidates, so it also reads the dependencies from that era.

Where is the fetched data stored?

What it fetches is cached. You can look up the location from the config.

$ composer config --global cache-dir
/root/.composer/cache

$ ls /root/.composer/cache
files
repo

The path is this one because I am running as root in a container, but the location changes by environment. When an XDG environment variable such as XDG_CONFIG_HOME was defined, it became ~/.cache/composer.

repo/ held the package information, and files/ held the downloaded packages themselves.

$ du -sh /root/.composer/cache/*
748K	/root/.composer/cache/files
2.1M	/root/.composer/cache/repo

Once fetched, things stay, so reinstalling the same version does not download it again. Even after deleting vendor/ and running install again, not a single Downloading appeared in the output. If something goes wrong, you can clear it with composer clear-cache.

The latest version is not always chosen

I thought update was a command to upgrade to the latest, but it actually searches for a combination that satisfies all the constraints.

I will leave the guzzle version unspecified and pin only psr/http-message to ^1.0.

{
    "require": {
        "guzzlehttp/guzzle": "*",
        "psr/http-message": "^1.0"
    }
}
guzzlehttp/guzzle              7.15.2      ← latest is 8.0.1
guzzlehttp/psr7                2.13.0
psr/http-message               1.1

The latest guzzle, 8.0.1, uses guzzlehttp/psr7 3.x internally, and that psr7 requires psr/http-message 2.x. Since I wrote ^1.0, the two cannot both hold. So the result was that it lowered guzzle itself to 7.x and brought in a combination that works.

When an old version gets installed and causes trouble, the first thing to suspect is the constraints you wrote yourself. You can ask for the reason with composer why-not.

composer why-not guzzlehttp/guzzle 8.0

composer require

When I ran composer require monolog/monolog, the first two lines were these.

./composer.json has been updated
Running composer update monolog/monolog

It writes to composer.json and then calls update. What require really is, is an update narrowed to the target. From there it follows the same flow as update.

Loading composer repositories with package information
Updating dependencies
Lock file operations: 2 installs, 0 updates, 0 removals
  - Locking monolog/monolog (3.10.0)
  - Locking psr/log (3.0.2)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 2 installs, 0 updates, 0 removals
  - Installing psr/log (3.0.2): Extracting archive
  - Installing monolog/monolog (3.10.0): Extracting archive
Generating autoload files
Using version ^3.10 for monolog/monolog

The difference is the last line. If you require without specifying a version, Composer looks up the latest version and decides the constraint for you.

// before running it
{ "name": "example/app", "require": {} }

// after running it
{ "name": "example/app", "require": { "monolog/monolog": "^3.10" } }

The result is the same as writing it into composer.json by hand and running composer update monolog/monolog, but with require you do not have to think about how to write the constraint. Running composer update without a package name re-resolves packages you did not ask for and upgrades them along with it, so that is a different thing. For things you only need during development, adding --dev puts them in require-dev.

composer require --dev phpunit/phpunit
(Aside) If it fails, composer.json is restored

I wondered which comes first, writing to composer.json or resolving, so I passed a constraint that cannot be resolved.

composer require "monolog/monolog:^99.0"
./composer.json has been updated
Running composer update monolog/monolog
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.
...
Installation failed, reverting ./composer.json and ./composer.lock to their
original content.

It gets as far as ./composer.json has been updated, then fails, and at the end it says itself that it reverted. It writes first and rolls back if it fails. It restores not only composer.json but also composer.lock.

Closing

I summarized what each command does.

composer update  = fetch package info → resolve → write lock → place in vendor → generate autoload
composer install =                                read lock  → place in vendor → generate autoload
composer require = add to composer.json → update narrowed to the target

Where I ended up: install is fast because it runs only the second half of update, and because there is a lock, the same combination gets installed no matter how many times you run it.

Translated from the Japanese original.

Thanks. I'll look into it.

Spotted a mistake?

Tell me what's wrong and I'll fix it. Only I see what you send.