composer.json Basics

Every Composer-managed project has a composer.json file sitting in its root folder. It's the project's manifest — a plain JSON file that declares what your project needs (its dependencies), plus metadata about the project itself (its name, description, and more). Composer reads this file to know what to download, and writes back to it whenever you add or remove a package.

Common Top-Level Fields

FieldWhat it does
nameThe package's identity, in vendor/project form, e.g. "acme/my-app".
descriptionA short, human-readable summary of what the project is.
requirePackages your project needs to run — installed in production.
require-devPackages only needed for development, like testing tools (e.g. PHPUnit) — not installed in production by default.
autoloadTells Composer how to find and load your own project's classes (commonly a PSR-4 namespace map).
typeWhat kind of package this is — "library" (the default) or "project" for a full application, among others.

Creating One Interactively with composer init

Rather than hand-writing composer.json from scratch, you can let Composer build it for you by answering a short set of prompts:

$ composer init

It will ask for a package name, description, author, the type of project, license, and whether you'd like to interactively add dependencies right away. Once you confirm the generated JSON, Composer writes it out to composer.json in the current folder.

A Full Annotated Minimal Example

{
    "name": "acme/my-app",
    // short, human-readable summary of the project
    "description": "A small demo application",
    // packages required at runtime
    "require": {
        "php": ">=8.1",
        "monolog/monolog": "^3.0"
    },
    // packages only needed for development/testing
    "require-dev": {
        "phpunit/phpunit": "^10.0"
    },
    // maps our own namespace to a folder for autoloading
    "autoload": {
        "psr-4": {
            "Acme\\MyApp\\": "src/"
        }
    }
}

Note that JSON doesn't actually support comments — the // lines above are only there to explain each part; a real composer.json would omit them (Composer will error on genuine comments in the file).

Version Constraint Syntax Basics

The values inside require aren't just exact version numbers — they're constraints that tell Composer which versions are acceptable. The three you'll see most often:

ConstraintMeaning
^1.2Caret: allows compatible releases — anything from 1.2.0 up to, but not including, 2.0.0.
~1.2Tilde: a narrower range — allows 1.2.x updates but not 1.3.0 or higher.
1.2.3Exact pin: only that precise version is acceptable, nothing else.
Note: The examples above are correct, static illustrations of the syntax — we won't run composer init or install anything live on this page, since that would modify this site's own project. Try them in your own project's terminal.
Next step: Now that you can read and write a composer.json, learn how to actually pull packages in → Installing Packages