“It works on my machine.” Almost always, that sentence hides a dependency nobody declared: a library installed by hand, a PHP extension that only exists on the laptop of whoever wrote the code, a package version different from the one on the server.
This is the second article in the Twelve factors in practice series. The first one covered factor I, codebase. Now it’s factor II, dependencies, with examples in Composer, PHP’s package manager, and a comparison with what other languages use.
Before we start: three words
- Dependency: any code or tool your app needs to work that you didn’t write. A library to generate UUIDs, an HTTP client, a PHP extension.
- Package: a dependency bundled so a tool can download and install it, with a name and a version.
- Package manager: the tool that reads the project’s list of dependencies, works out which versions to use and downloads everything. In PHP it’s Composer, in JavaScript it’s npm, in Python it’s pip.
What factor II says
The original text fits in two rules:
- Declare everything. The app lists, in a file inside its own repository, every dependency it needs, with versions. That file is called the manifest.
- Isolate everything. The app doesn’t use anything that “happens to be” installed on the machine. Dependencies live in a project folder, separate from other projects on the same server.
The practical consequence is the most important part: a new person on the team clones the repository, runs one command and has the app working. No list of steps in a forgotten document, no “oh, first you need to install this other thing”.
An analogy helps. Think of a cake recipe. A bad recipe says “use flour”. A good recipe says “500 g of all-purpose wheat flour”. The manifest is the good recipe: it leaves nothing to the luck of whoever is cooking.
The manifest in PHP: composer.json
In Composer, the manifest is composer.json. This article’s lab uses a minimal app that generates a UUID with the ramsey/uuid library:
{
"name": "lab/twelve-factor-dependencies",
"require": {
"php": ">=8.2",
"ext-redis": "*",
"ramsey/uuid": "^4.0"
},
"require-dev": {
"symfony/var-dumper": "^7.0"
}
}Each line has a job:
"php": ">=8.2": the minimum version of PHP itself."ext-redis": "*": a PHP extension, a module written in C that must be installed on the server. It’s a dependency too, and it needs to be declared too."ramsey/uuid": "^4.0": a library, accepting any 4.x version.require-dev: things only needed during development, like debugging and testing tools. They shouldn’t go to production.
^4.0 is a version range: “any 4.something, as long as it’s at least 4.0”. It’s useful for getting fixes, but it creates a problem: two people installing on different days can get different versions.
The lock: composer.lock
That’s where composer.lock comes in. composer.json says what the app accepts. composer.lock says what was chosen, with the exact version of every package, including the dependencies of the dependencies.
In the lab, the first composer update resolved the range and wrote the lock:
$ composer update
Updating dependencies
Lock file operations: 6 installs, 0 updates, 0 removals
- Locking brick/math (1.0.0)
- Locking ramsey/collection (2.1.1)
- Locking ramsey/uuid (4.10.0)
...
Writing lock filecomposer.json still says ^4.0, but the lock now says 4.10.0. Notice also that one package was requested and six came in: ramsey/uuid depends on brick/math and ramsey/collection, and the lock keeps all of them.
Then the test deleted the vendor/ folder and ran composer install:
$ rm -rf vendor && composer install
Installing dependencies from lock file (including require-dev)
Verifying lock file contents can be installed on current platform.
Package operations: 6 installs, 0 updates, 0 removals
- Installing ramsey/collection (2.1.1): Extracting archive
- Installing brick/math (1.0.0): Extracting archive
- Installing ramsey/uuid (4.10.0): Extracting archiveThe first line is the one that matters: “from lock file”. install doesn’t choose versions, it reproduces what’s in the lock. So the rule is simple:
composer update: picks new versions and rewrites the lock. Run it when you want to update.composer install: installs exactly what’s in the lock. Run it everywhere else: on a newcomer’s machine, in CI and in the deploy.
Both files, composer.json and composer.lock, go into the repository. The vendor/ folder goes into .gitignore.
When the lock and the manifest disagree
The lab also tested the most common mistake: someone edits composer.json by hand, adding "monolog/monolog": "^3.0", and forgets to update the lock. composer install warned and stopped:
Warning: The lock file is not up to date with the latest changes in composer.json.
- Required package "monolog/monolog" is not present in the lock file.
This usually happens when composer files are incorrectly merged or the
composer.json file is manually edited.The fix is to use composer require monolog/monolog, which updates both files together, instead of editing the JSON by hand.
PHP extensions are dependencies too
This is the point PHP projects miss most often. An app that uses Redis through the redis extension works on the laptop that has the extension and breaks on the server that doesn’t. If the extension isn’t in composer.json, nobody finds out until the error shows up in production.
In the lab, composer.json declared ext-redis, which wasn’t installed on the machine. composer install refused right away:
Problem 1
- Root composer.json requires PHP extension ext-redis * but it is missing
from your system. Install or enable PHP's redis extension.And the composer check-platform-reqs command shows the state of everything at once:
$ composer check-platform-reqs
ext-iconv 8.4.27 success
ext-redis n/a lab/twelve-factor-dependencies requires ext-redis (*) missing
php 8.4.27 successext-iconv shows up without being requested because one of the locked packages needs it. This command is a good step for CI or for the start of a deploy: if something is missing, the process stops before the version goes live.
One caveat: Composer checks extensions, but doesn’t install them. The image or the server does that. In Docker, a tool that packages the app with everything it needs into an image, this is written in the Dockerfile:
FROM php:8.4-cli
# the extension declared in composer.json, installed in the image
RUN apt-get update \
&& apt-get install -y --no-install-recommends $PHPIZE_DEPS \
&& pecl install redis \
&& docker-php-ext-enable redis \
&& apt-get purge -y --auto-remove $PHPIZE_DEPS \
&& rm -rf /var/lib/apt/lists/*
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
WORKDIR /app
# manifest and lock first: install exactly what's in the lock
COPY composer.json composer.lock ./
RUN composer install --no-dev --optimize-autoloader --no-interaction
COPY public ./public
CMD ["php", "-S", "0.0.0.0:8080", "-t", "public"]This way, the full list of what the app needs lives in the repository: the libraries in composer.json, the exact versions in composer.lock, and what goes on the system in the Dockerfile.
The same goes for tools the app calls from the outside, like curl or ImageMagick to resize images. The original text mentions exactly this case: if the app depends on a system program, that program has to come along, declared.
Production without the development tools
require-dev exists so development tools don’t go to production. In the lab, with symfony/var-dumper installed, the test ran:
$ composer install --no-dev --optimize-autoloader
Installing dependencies from lock file
Package operations: 0 installs, 0 updates, 3 removals
- Removing symfony/var-dumper (v7.4.22)
- Removing symfony/polyfill-mbstring (v1.43.3)
- Removing symfony/deprecation-contracts (v3.8.1)
Generating optimized autoload files--no-dev removed the development package and the dependencies only it used. --optimize-autoloader builds a map of the classes ahead of time, so PHP doesn’t have to search file by file on every request.
What about other languages?
The problem is the same in any language, and so is the solution: a manifest, a lock and an isolated folder. Only the names change.
| Language | Manager | Manifest | Lock | Install exactly what’s locked |
|---|---|---|---|---|
| PHP | Composer | composer.json | composer.lock | composer install |
| JavaScript | npm | package.json | package-lock.json | npm ci |
| Python | pip | requirements.txt | the same file, with pinned versions (==) | pip install -r requirements.txt |
| Python | uv | pyproject.toml | uv.lock | uv sync --locked |
| Ruby | Bundler | Gemfile | Gemfile.lock | bundle install |
| Rust | Cargo | Cargo.toml | Cargo.lock | cargo build --locked |
| Go | Go modules | go.mod | go.sum | go mod download |
A few differences worth knowing:
- npm:
npm installcan updatepackage-lock.json. For CI and deploys, the right command isnpm ci, which follows the lock strictly and fails if it doesn’t matchpackage.json. It’s the direct equivalent ofcomposer install. - pip: pip doesn’t have a separate lock. The classic approach is to pin versions in
requirements.txt(requests==2.32.3) and isolate dependencies in a virtual environment (python -m venv), which plays the role of thevendor/folder. Newer tools like uv and Poetry bring a real lock. - Go:
go.sumstores verification codes (checksums) for each version, to guarantee the downloaded code is the same as before.
If you already use one of these, Composer will feel familiar, and the other way around too.
How to tell if your project follows factor II
A few quick questions:
- Is
composer.lockin the repository? - Does the deploy run
composer install, notcomposer update? - Are the PHP extensions the app uses in the
requiresection ofcomposer.json? - Can a newcomer run the project with one command, without a separate step-by-step guide?
- Does production install with
--no-dev?
If any answer is “no”, start there. It’s also a matter of ownership: when the dependency list is written down, anyone on the team can understand, install and update the app, and it stops depending on someone’s memory.
Next article
In the next article in the series, the topic is factor III, config: why passwords, addresses and keys stay out of the code, in environment variables, and how to read that config in PHP.
Glossary
- Manifest: the file that lists the project’s dependencies and the accepted versions, like
composer.json. - Lock: the file that stores the exact version chosen for each dependency, like
composer.lock. - Version range: a rule like
^4.0that accepts several versions instead of just one. - PHP extension: a module, usually written in C, that adds functions to PHP and must be installed on the server, like
redisorintl. - Autoloader: the code that loads each PHP class automatically when it’s used, without a manual
require. - Virtual environment: in Python, an isolated folder with a project’s dependencies, separate from the system’s.
Sources
- The Twelve-Factor App: II. Dependencies
- Composer: basic usage, composer.lock and install
- Composer: platform packages (php and ext-*)
- Composer: check-platform-reqs
- Official PHP image on Docker Hub: installing extensions
- npm: npm ci
- pip: requirements file format
- uv: projects and lockfile
- Bundler: Gemfile.lock
- Cargo: Cargo.toml vs Cargo.lock
- Go modules: go.sum
Comments