Skip to content
← Back to articles

Article

12 factors: dependencies declared, nothing assumed

“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

What factor II says

The original text fits in two rules:

  1. Declare everything. The app lists, in a file inside its own repository, every dependency it needs, with versions. That file is called the manifest.
  2. 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:

^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.

Diagram: composer.json, with the range ^4.0, becomes composer.lock through composer update, which stores the exact version 4.10.0. composer.lock becomes the vendor/ folder through composer install. composer.json and composer.lock go into Git; the vendor/ folder does not, it is rebuilt on every machine from the lock

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 file

composer.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 archive

The 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:

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   success

ext-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.

LanguageManagerManifestLockInstall exactly what’s locked
PHPComposercomposer.jsoncomposer.lockcomposer install
JavaScriptnpmpackage.jsonpackage-lock.jsonnpm ci
Pythonpiprequirements.txtthe same file, with pinned versions (==)pip install -r requirements.txt
Pythonuvpyproject.tomluv.lockuv sync --locked
RubyBundlerGemfileGemfile.lockbundle install
RustCargoCargo.tomlCargo.lockcargo build --locked
GoGo modulesgo.modgo.sumgo mod download

A few differences worth knowing:

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:

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

Sources

Share LinkedIn WhatsApp X

Get new articles by email

One email when a new article comes out, in English and Portuguese. No spam, unsubscribe anytime.

Comments