10aday › Version Constraint Checker › Composer version constraints

Composer version constraints: what ~1.2, ^1.2, 1.2.* and "1.2" really allow in composer.json

Short answer: in composer.json, ^1.2.3 allows ≥1.2.3 <2.0.0 (like npm). The tilde lets the last number you wrote move: ~1.2 = ≥1.2.0 <2.0.0, ~1.2.3 = ≥1.2.3 <1.3.0. A bare version is exact, even a partial one: "1.2" means 1.2.0 only. Those last two rules are where people coming from npm get surprised.

Have a constraint and a list of versions? Paste them into the Version Constraint Checker: it shows, side by side, what npm, Cargo, Composer, pip and RubyGems accept.

Every Composer constraint form, next to npm

Highlighted: npm reads the same string differently.

ConstraintComposernpm
"1.2.3" (bare)exactly 1.2.3exactly 1.2.3try it
"1.2" (bare)
Composer: exactly 1.2.0
exactly 1.2.0≥1.2.0 <1.3.0try it
~1.2.3≥1.2.3 <1.3.0≥1.2.3 <1.3.0try it
~1.2≥1.2.0 <2.0.0≥1.2.0 <1.3.0try it
~1≥1.0.0 <2.0.0≥1.0.0 <2.0.0try it
~0.3≥0.3.0 <1.0.0≥0.3.0 <0.4.0try it
^1.2.3≥1.2.3 <2.0.0≥1.2.3 <2.0.0try it
^1.2≥1.2.0 <2.0.0≥1.2.0 <2.0.0try it
^0.3
below 1.0: minor = breaking
≥0.3.0 <0.4.0≥0.3.0 <0.4.0try it
^0.0.3≥0.0.3 <0.0.4≥0.0.3 <0.0.4try it
1.2.*≥1.2.0 <1.3.0≥1.2.0 <1.3.0try it
1.*≥1.0.0 <2.0.0≥1.0.0 <2.0.0try it
*any versionany versiontry it
>=1.2 <2.0≥1.2.0 <2.0.0≥1.2.0 <2.0.0try it
>=1.2,<2.0
npm has no comma
≥1.2.0 <2.0.0not validtry it
>1.2>1.2.0≥1.3.0try it
!=1.2.3not 1.2.3not validtry it
1.2 - 2.3≥1.2.0 <2.4.0≥1.2.0 <2.4.0try it
^1.2 || ^3.0≥1.2.0 <2.0.0 or ≥3.0.0 <4.0.0≥1.2.0 <2.0.0 or ≥3.0.0 <4.0.0try it

Ranges shown for stable releases. Composer also filters by stability (minimum-stability, @dev, @beta flags), which this table does not cover; the checker shows how pre-releases such as 1.3.0-RC1 are handled.

Rules of thumb for composer.json

Common questions

What does ~1.2 mean in composer.json?

≥1.2.0 and <2.0.0: the last number you wrote (2) may increase. npm and Cargo read the same ~1.2 as ≥1.2.0 <1.3.0. Write ~1.2.0 in Composer if you want patch updates only.

What is the difference between ^ and ~ in Composer?

With three numbers: ^1.2.3 = ≥1.2.3 <2.0.0 and ~1.2.3 = ≥1.2.3 <1.3.0. With two numbers they are the same for 1.x and above: ^1.2 = ~1.2 = ≥1.2.0 <2.0.0. Below 1.0 they differ: ^0.3 = <0.4.0 but ~0.3 = <1.0.0.

Does "1.2" in composer.json mean 1.2.x?

No. A bare version without an operator or wildcard is an exact constraint, padded with zeros: "1.2" matches 1.2.0 only. Write "1.2.*" for the whole 1.2.x line. (In npm, a bare 1.2 means 1.2.x.)

How do I write "and" and "or" in Composer?

A space or a comma means “and” (>=1.2 <2.0 or >=1.2,<2.0); || means “or” (^1.2 || ^2.0). Hyphen ranges like 1.2 - 2.3 include the whole 2.3.x line.

Related

Sources