8.3 in the file, 8.2 on the server: what Composer really checks before it installs

PHP | Composer

I have a personal server that I use to host a few nice applications and for various experiments: an environment I develop on to try things out and learn, a place where it is fine to break things. One evening I updated a single package there. Composer finished without a warning and without an error. I refreshed the browser, and every page on the site returned an error. Even pages that have nothing to do with that package.

The failure itself bothered me less than the silence that came before it. The tool that is supposed to keep the packages compatible with the PHP version said everything was fine, and a second later the very same tool stopped the site because of a mismatch. How does Composer manage to disagree with itself?

The answer was a single line in the configuration file, which told it that the project runs on PHP 8.3 while the server was running 8.2. It believed the line. That led to three questions this post answers: when Composer checks the PHP version, which version it checks against, and which command checks the version that really runs.

I am writing this post as a record of learning: what happened, what I opened in order to understand, and what working order has stayed with me since.
You do not need to know Composer in depth to read it. And if names like composer.json and composer.lock are completely new, there is an appendix that starts from zero at the end of the post. You can jump to it and come back to the story.

Part one — what happened, step by step

1. Three moments when the version is checked

The PHP version is checked three times on the way from the update to the visitor. I think of it as three guards. Two of them take instructions from you and trust what you wrote for them. The third checks for itself, but it stands at the front door, facing the visitors.

The moment Who checks Against which version What happens when there is no match
composer update or require The dependency resolver The value in config.platform, and if there is none, the PHP that runs Composer Composer chooses another version of the package, or stops with an error
composer install The check of the requirements recorded in composer.lock The same The installation stops
The first request to the site The file vendor/composer/platform_check.php, which is loaded together with the autoloader The PHP that runs the request The request fails with the message "Composer detected issues in your platform"

This is the heart of the matter. The first two checks happen at a convenient time, when you are at the terminal and no visitor has seen anything yet, but they can be pointed at a version that is not the real one. The third cannot be fooled, and it is also the only one that runs in front of visitors.

And one more detail that is easy to miss, and I missed it: the PHP that runs Composer on the command line is not necessarily the PHP that serves the site. On a server with several versions installed, and in a test environment that is almost always the case, these can be two different versions.

🔍

What are the three guards called? The first is the Dependency Resolver: the part of Composer that chooses which versions to install. The second is the same mechanism, when it verifies what is recorded in the Lock File, the file composer.lock. The third is called Platform Check. And PHP itself, together with its extensions, is called Platform Packages in Composer's world: packages that cannot be installed, but can be required.

2. What the platform setting does

The config.platform setting is an instruction to Composer: do not look at the machine, believe me. It resolves dependencies as if the version you wrote is the installed version, regardless of what really runs.

JSON
{
    "config": {
        "platform": {
            "php": "8.2.20"
        }
    }
}

This sounds like an invitation to trouble, but the setting exists for a good reason. In a team where the developers work on a new PHP and the server is still on an old version, composer update from each computer would choose packages that suit that computer. When you write the server's version in the setting, everyone gets a lock file that suits the server, no matter where the update was run from.

Hence the rule: the value in the setting is the lowest version the code will run on. The setting helps when it describes the server, and does harm when it describes the developer's computer.

And it goes stale quietly. No one opens the configuration file to check whether this line is still correct. It was written on the day it was correct, and since then it has simply been there.

3. How it breaks

For me it looked like this (I replaced the package name and the versions; the mechanism is the same). The server ran PHP 8.2.20, the computer I develop on ran 8.3.6, and the configuration file held this line, written at some point according to the computer and not according to the server:

JSON
"platform": { "php": "8.3.6" }

I wanted to update one package; let us call it acme/zip-builder. Its new version requires PHP 8.3 or higher, and I did not know that. I ran this on the server:

ShellCode was run
composer update acme/zip-builder --with-all-dependencies

Composer checked the requirement against the 8.3.6 in the file, found a match, and installed. Without a warning and without an error. And it was right: it was asked whether the package suits 8.3.6, and it does.

Then came the first request to the site. The autoloader was loaded, and with it the third guard:

TextOutput actually received
Composer detected issues in your platform:
Your Composer dependencies require a PHP version ">= 8.3.0". You are running 8.2.20.

The message itself is polite and accurate, and I have no complaint about it. My complaint is about the timing. Instead of my reading it in the terminal, a minute earlier, everyone who came to the site got it.

And it really was everyone who came. The check runs before the application's code, so even requests that would never touch the updated package fail. One package that requires a version that is too high is enough to bring down an entire site.

This is also where it became clear to me why the very same update works locally without a problem, which only strengthens the feeling that everything is fine. On the local computer 8.3 really runs, so the value in the file was correct there. The gap between the environments existed even without the setting. The setting only hid it until the last moment.

4. Opening the third guard

I was curious to see who exactly had stopped my site. The third guard is a single file, platform_check.php, and it sits inside your project:

Text
vendor/
├── autoload.php
└── composer/
    ├── autoload_real.php
    └── platform_check.php

No one writes it by hand. Composer generates it anew on every install, update or dump-autoload, according to the requirements of the installed packages, so there is no point in editing it: the next update will overwrite any change. It is not in git either, because the vendor folder is usually outside the repository.

Its job is to stop the application before it starts, if the PHP that runs it does not meet the packages' requirements. The application does not call it itself. It loads vendor/autoload.php, like every PHP application that uses Composer, and that file loads autoload_real.php, whose first action is to run the check. That is why the check runs on every request, and before every line of your code.

I opened it. I expected a mechanism. I found one condition:

PHP
if (!(PHP_VERSION_ID >= 80300)) {
    $issues[] = 'Your Composer dependencies require a PHP version ">= 8.3.0". You are running ' . PHP_VERSION . '.';
}

That is the whole guard. At install time Composer calculates the strictest requirement among all the packages and writes it into the file as a single number. 80300 is 8.3.0, and my server was 80220. The whole site fell over a difference of 80 between two numbers. Further on in the file, if a problem was found, it sends a 500 header and throws an exception, and not a single line of the application runs after it.

Reading the file showed me three things I did not know.

The guard knows how to keep a secret. Before it prints the message to the browser, it passes it through this line:

PHP
str_replace('You are running '.PHP_VERSION.'.', '', implode(PHP_EOL, $issues))

It deletes the sentence "You are running 8.2.20" from the message. The visitor sees which version is required, but not which version really runs on the server. The full message, the one I showed above, reaches only the log and the command line. An exact PHP version is information an attacker is happy to receive, and someone at Composer thought about that inside a file of thirty lines. The secret is kept only when display_errors is off, as it should be on a real server. When it is on, PHP shows the full exception, version included.

This guard has a blind spot. By default ("platform-check": "php-only") it checks only the PHP version, and not the extensions. A package that requires an extension that is not installed on the server will pass it quietly, and the error will appear only when the code reaches the missing function. With "platform-check": true it checks the extensions too.

And it can be turned off, and that is the most tempting fix. You write "platform-check": false, and the site comes back up. Sort of. Now there is no guard at the door, and code written for 8.3 runs on 8.2 until it meets syntax the old version does not know. It falls over there, in the middle of an operation, with an error that does not mention versions at all. The original message appeared immediately and said exactly what was wrong. After turning the check off, the failure stays, and only its explanation disappears.

5. The check that sees reality

After the site was working again, I wanted to know whether you can ask the third guard for its opinion in advance, without waiting for the first visitor. You can, and it takes one command. composer check-platform-reqs ignores the setting and checks the installed packages against the PHP and the extensions that really exist on the machine.

ShellCode was run
composer check-platform-reqs

It is the same check the site would have run on the first request, except that it runs on the command line, before any user is hurt. When there is a mismatch it ends with a non-zero exit code, so you can put it in a deployment script as a gate.

The caveat is the detail from the first section: the command checks the PHP that runs it. If the command-line PHP is not the one that serves the site, you need to run it with the right version.

When the check fails, the next question is which package requires the higher version:

ShellCode was run
composer why-not php 8.2.20

The output lists which packages block this version. Sometimes it is not the package you asked to update, but one of its dependencies that was updated along with it.

The fix is to make the setting describe the server again, and to update again:

ShellCode was run
composer config platform.php 8.2.20
composer update acme/zip-builder --with-all-dependencies

This time Composer itself chooses a version of the package that suits 8.2, or keeps the existing version.

6. What was checked before publication — and what remains open

Before publication the mechanism was reproduced in a small test project: a local dummy package in two versions, a real PHP 8.2, and the value 8.3.6 in the setting. All the commands in the post were run there, and the claims were compared with Composer's documentation, with its source code and with PHP's documentation.

What is written here What was found
An update against a value in the setting that is too high passes quietly Reproduced: the package was upgraded without a warning and without an error
The condition and the message in the check file Identical to the file generated in the reproduction, word for word
The visitor does not see which version runs on the server Reproduced: when display_errors is off, the response is 500 and the message arrives without "You are running". In the log it is complete
check-platform-reqs ignores the setting Written in the documentation, and reproduced: the check failed, with exit code 1
composer install stops when the version does not match Reproduced, with a caveat detailed below
The file ends by throwing an exception So in Composer's current source code. In version 2.5, in which the reproduction was done, it ends with a fatal error, and the result for the visitor is the same
After the check is turned off, the code falls over somewhere else Not checked: depends on what the package really uses
Restarting the web server alone does not clear OPcache Not reproduced: depends on the server's configuration

What was verified? The whole mechanism, from the silent update to the error on the first request, and every one of the commands.

What was not verified? What depends on a particular server or a particular package: the behavior of OPcache, and what happens to the code after the check is turned off.

The check also revealed three details that did not come up in the story.

The setting is recorded in the lock file too. Composer writes the value of config.platform into composer.lock, in the platform-overrides field, and composer install checks against the value recorded there. In the reproduction, install passed even after the setting was deleted from composer.json, because the old value stayed in the lock file until the next update. Only when there is no value anywhere does install stop against the real PHP.

The documentation and the code disagree on one detail. Composer's documentation says that the check prints a warning and exits with exit code 104. In the current source code there is no such exit: the file throws an exception.

And on a server with the default settings, the new code loads by itself. According to PHP's documentation, OPcache by default checks every two seconds whether a file has changed. On such a server the update takes effect without any restart, and the check-platform-reqs command runs after the files have already been replaced. If you want to check before anything changes in the vendor folder, you can split the update in two:

ShellCode was run
composer update acme/zip-builder --with-all-dependencies --no-install
composer check-platform-reqs --lock

The first command writes only composer.lock, without installing anything. The second checks what is recorded in it against the real PHP. If it fails, you restore the backup of composer.lock, and no file the site loads has changed. If it passes, you run composer install.

Part two — the professional summary

From the update to the visitor: where is the version checked?

  1. composer update or require — leads to: Do the requirements match the value in the setting?
  2. Do the requirements match the value in the setting? — leads to: Composer chooses another version or stops with an error; The file composer.lock is written and the packages are installed
  3. Composer chooses another version or stops with an error
  4. The file composer.lock is written and the packages are installed — leads to: The first request to the site loads the autoloader
  5. The first request to the site loads the autoloader — leads to: Does the PHP that runs the request meet the requirement?
  6. Does the PHP that runs the request meet the requirement? — leads to: The application runs; Error 500 for every request
  7. The application runs
  8. Error 500 for every request
Source code of the diagram (Mermaid)
Mermaid
flowchart TD
 A[composer update or require] --> B{Do the requirements match the value in the setting?}
 B --> C[Composer chooses another version or stops with an error]
 B --> D[The file composer.lock is written and the packages are installed]
 D --> E[The first request to the site loads the autoloader]
 E --> F{Does the PHP that runs the request meet the requirement?}
 F --> G[The application runs]
 F --> H[Error 500 for every request]

The first question is asked against what is written in the configuration file. The second question is asked against the PHP that really runs. When the value in the file is higher than the version that really runs, the answers differ, and the first to discover it is the visitor.

The settings: what each one determines

The setting in composer.json What it determines
"platform": { "php": "8.2.20" } The version Composer assumes during update, require and install, instead of the version that runs it
Without platform Composer checks against the PHP that runs it
"platform-check": "php-only" The default: at the start of every request only the PHP version is checked
"platform-check": true The extensions the packages require are checked too
"platform-check": false The check file is not generated, and there is no check at run time

The commands: which one checks against what

The command What is checked Against which version
composer update Which versions of the packages can be installed The value in the setting, and if there is none, the PHP that runs Composer
composer install That the packages recorded in composer.lock can be installed The value recorded in composer.lock, and if there is none, the PHP that runs Composer
composer check-platform-reqs The packages installed in the vendor folder The PHP and the extensions that really exist; the setting has no effect
composer check-platform-reqs --lock The packages recorded in composer.lock The PHP and the extensions that really exist
composer why-not php 8.2.20 Which packages block this version The version written in the command

The number in the check file

The version As a single number Against the condition >= 80300
8.2.20 80220 Does not meet it
8.3.0 80300 Meets it
8.3.6 80306 Meets it

A safe working order for updating dependencies on a server

Since then I update dependencies in a fixed order. There is nothing sophisticated about it, and all of its value is in step 5, which sits between the update and the restart.

  1. A backup of composer.lock. This file is the way back: you restore it and run composer install, not update, and you get exactly the versions that worked.

  2. Comparing the real version with the setting. php -v against the value in config.platform. If they differ, fix that before anything else.

  3. Running as the user that runs the application. Files created under another user's ownership may be unreadable for the application server.

  4. Updating specific packages only, and reading the output: which packages changed, and to which version.

  5. composer check-platform-reqs. If it fails, stop here and go back to the backup. When OPcache is set not to check files for changes, the site is still running the old code in memory.

  6. Restarting PHP-FPM, and not only the web server. When PHP-FPM runs as a separate service, the compiled code is kept in OPcache inside its processes, and restarting the web server alone does not clear it.

  7. One real action that touches the updated packages, while watching the error log.

The rule in one sentence: The setting tells Composer what to assume, and the check tells you what is true. Before a restart, run the check.

What not to do: do not turn off the check to get the site back up; do not write the version of the computer you develop on in the setting; and do not assume that the command-line PHP is the PHP that serves the site.

Review questions — with answers

9 questions. Select a question to open its answer.

Question 1: Against which PHP version does Composer check the requirements during composer update?

Answer: Against the value in config.platform. Only when there is no such value does it check against the PHP that runs Composer itself.

Question 2: Why did the update pass without a warning, even though the server was running PHP 8.2?

Answer: Because the setting said 8.3.6, and Composer checked the package's requirement against that value. Against 8.3.6 the package really does match.

Question 3: Who stopped the site, and when?

Answer: The file platform_check.php. It is loaded together with the autoloader at the start of every request, before the application's code, and it checks the PHP that runs the request.

Question 4: Why did pages that do not use the updated package fail too?

Answer: Because the check runs before every line of the application, and it checks the strictest requirement among all the installed packages.

Question 5: What does the number 80300 in the check file mean?

Answer: It is version 8.3.0 as a single number. The constant PHP_VERSION_ID holds the running version in the same form, so 8.2.20 is 80220.

Question 6: What does the visitor see when the check fails, and what is written to the log?

Answer: When display_errors is off, the visitor gets a 500 error with the required version, without the version that runs on the server. The full message is written to the log.

Question 7: What is the difference between "platform-check": "php-only" and "platform-check": true?

Answer: The first, the default, checks only the PHP version. The second also checks that the extensions the packages require are installed.

Question 8: Which command checks the packages against the PHP that really runs, and what is its caveat?

Answer: composer check-platform-reqs. It ignores the setting, but it checks the PHP that runs it, so you run it with the same version that serves the site.

Question 9: Why is turning off the check not a fix?

Answer: Because the mismatch remains. The code runs until it meets something the old version does not know, and falls over there with an error that does not mention versions.

Appendix for anyone meeting Composer for the first time

There is no shame at all in arriving here without knowing the difference between composer.json and composer.lock. Here is the explanation that lets you continue the story without opening ten other tabs.

What does Composer do?

Composer is PHP's dependency manager. A project is almost never written entirely from scratch: it relies on libraries that others wrote, and these are called Dependencies. Composer downloads them into the vendor folder, and makes sure that their versions suit each other and the PHP version.

What is the difference between composer.json and composer.lock?

In the file composer.json you write what the project needs, usually as a range: version 2 or higher of a certain package. In the file composer.lock Composer records what was actually installed: the exact version of each package.

  1. composer.json — leads to: composer update
  2. composer update — leads to: composer.lock
  3. composer.lock — leads to: composer install
  4. composer install — leads to: vendor
  5. vendor
The diagram as text
Text
composer.json
     |
composer update
     |
composer.lock
     |
composer install
     |
vendor

The composer update command recalculates which versions match, and writes them to the file composer.lock. The composer install command calculates nothing: it installs exactly what is recorded there. That is why you update in one place, and install the same file everywhere else.

What is an autoloader?

The file vendor/autoload.php is the Autoloader. The application loads it once, and from that moment every class is loaded from its own file the moment it is used, without a require line for every file. Because every request passes through it, this is where Composer puts the version check.

How does a package say which PHP it needs?

Every package also writes requirements on the environment in its own composer.json file: a PHP version, and sometimes extensions. Composer treats PHP and the extensions as packages in every respect. It takes their version from the machine it runs on, unless the config.platform setting tells it otherwise.

Why is there more than one PHP on the same server?

PHP runs in several forms. On the command line it is the CLI that runs; the site's requests are usually run by a separate service, PHP-FPM. Each of them can have its own version and its own configuration files, so php -v in the terminal is not necessarily the version that serves the site.

What is PHP_VERSION_ID?

A PHP constant that holds the version as a single number: the major version times 10,000, plus the minor version times 100, plus the patch number. 8.3.0 is 80300, and 8.2.20 is 80220. One number is easier to compare than three.

What is OPcache?

PHP compiles every file before it runs it. OPcache keeps the compiled result in memory, so that it does not have to compile again on every request. In the default setting it checks every two seconds whether the file has changed; when this check is off, a changed file is loaded only after a restart.

Glossary of the post

Term Short explanation Why remember it
Composer PHP's dependency manager The tool that checks that the packages match the PHP version
Dependency A library the project relies on Each one has requirements of its own, the PHP version among them
Lock File The file composer.lock: the exact versions that were installed You install from it, and its backup is the way back
Dependency Resolver The part of Composer that chooses versions that suit each other and the environment The first guard: it believes the setting
Platform Package PHP and its extensions, which Composer treats as packages The requirement "PHP 8.3 or higher" is a requirement on such a package
config.platform A setting that tells Composer which version to assume It helps when it describes the server, and does harm when it does not
Autoloader The file through which the packages' classes are loaded Every request passes through it, so the check sits in it
Platform Check The check that runs at the start of every request, from the file platform_check.php The third guard: it checks the PHP that really runs
PHP_VERSION_ID The PHP version as a single number, for example 80300 The condition in the check file is checked against it
display_errors A PHP setting that determines whether errors are shown in the browser When it is on, the version running on the server is exposed to the visitor
check-platform-reqs A command that checks the packages against the PHP and the extensions that really exist The check you run before a restart
Exit Code The number a program returns when it ends: 0 for success It lets a deployment script stop when the check fails
CLI PHP that runs on the command line Not necessarily the version that serves the site
PHP-FPM The service that runs PHP for the site's requests Its version is the one you check, and it is the one you restart
OPcache A cache of compiled PHP code Because of it, old code can keep running after an update

What stayed with me from that evening

And what stayed with me from that evening is wider than Composer. A tool that says "everything is fine" answers the question it was asked. It is worth knowing which question that was, and who worded it. In my case it was me, in one line I had forgotten I wrote.

Sources for further reading and checking

In this post

8.3 in the file, 8.2 on the server: what Composer really checks before it installs

Glossary term

Why remember it

Where it appears in the post

    Open in the glossary

    Saved in this browser only

    My pinned terms

    No pinned terms yet. You can pin a term from its explanation, or from the glossary page.

      Pinned terms on the glossary page

      Search and commands