133

I have this line in my composer.json file:

"require": {
    ...
    "friendsofsymfony/user-bundle": "~2.0@dev",
    ...
},

What does the tilde ~ in ~2.0@dev exactly mean? Is that a placeholder and shall always fetch the subversions like 1.2.0, 2.2.0, 3.2.0 and so on? Doesn't make sense (and would be done by the * wildcard).

The composer.json documentation doesn't tell anything about the tilde.

I am asking because I just read about a security issue in the Symfony blog and they recommend to upgrade to version 1.3.3. But figuring out the FOSUserBundle's version isn't that easy (I couldn't find a file that contains the version).

Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116

4 Answers4

160

Tilde means next significant release. In your case, it is equivalent to >= 2.0, < 3.0.

The full explanation is at Tilde Version Range docs page:

The ~ operator is best explained by example: ~1.2 is equivalent to >=1.2 <2.0.0, while ~1.2.3 is equivalent to >=1.2.3 <1.3.0.

Another way of looking at it is that using ~ specifies a minimum version, but allows the last digit specified to go up.

Seldeak's below comment is a simple sum up explanation of the Composer documentation.

kenorb
  • 155,785
  • 88
  • 678
  • 743
AlterPHP
  • 12,667
  • 5
  • 49
  • 54
  • 79
    A simple rule-of-thumb way I like to put it is that the ~ allows the last digit to go up. e.g. `~2.2` means 2.2 and any 2.x where x is 2 or above. `~2.1.3` on the is also any 2.1.x where x is 3 or above. – Seldaek Sep 24 '13 at 11:36
  • 4
    How is `~2.0` different to `2.*`? Is it only useful if the last digit isn't 0? – Jesse Jun 11 '15 at 12:51
  • 31
    ~2.0 and 2.* are the same BUT ~2.3 and 2.* are different because ~2.3 does not allow versions below 2.3 whereas 2.* allows 2.0, 2.1, 2.2, etc – AlterPHP Jun 11 '15 at 14:22
4

Tilde operator is useful for the projects that version their libraries using semantic versioning scheme.

Semantic versioning is more of a guideline that evaluates to the next significant release.

For Composer, this operator means to allow minor releases (that can include patches) without allowing a major version (that may not be backward compatible) while installing and updating.

For example: ~4.1 will allow project versions >=4.1 but <5.0.

Credits: http://dwellupper.io/post/37/using-tilde-range-operator-to-resolve-dependency-version-in-composer-php

Pranav Rana
  • 371
  • 2
  • 6
2

The tilde ~ is one of many constraints that can be used to handle versions.

Next Significant Release Operators (~, ^):

The ~ operator is best explained by example: ~1.2 is equivalent to >=1.2 <2.0.0, while ~1.2.3 is equivalent to >=1.2.3 <1.3.0

The ^ operator behaves very similarly, but it sticks closer to semantic versioning, and will always allow non-breaking updates. For example ^1.2.3 is equivalent to >=1.2.3 <2.0.0 as none of the releases until 2.0 should break backwards compatibility. For pre-1.0 versions it also acts with safety in mind and treats ^0.3 as >=0.3.0 <0.4.0

Hyphenated Version Range (-)

Inclusive set of versions. Partial versions on the right include are completed with a wildcard. For example 1.0 - 2.0 is equivalent to >=1.0.0 <2.1 as the 2.0 becomes 2.0.*. On the other hand 1.0.0 - 2.1.0 is equivalent to >=1.0.0 <=2.1.0

Wildcard Version Range (.*)

You can specify a pattern with a * wildcard. 1.0.* is the equivalent of >=1.0 <1.1

Simple Version Range (>, >=, <, <=, !=)

By using comparison operators you can specify ranges of valid versions. Valid operators are >, >=, <, <=, !=.

You can define multiple ranges. Ranges separated by a space ( ) or comma (,) will be treated as a logical AND. A double pipe (||) will be treated as a logical OR. AND has higher precedence than OR.

And finally Exact Version Constraint

You can specify the exact version of a package Example: 1.0.2

Dylan KAS
  • 4,840
  • 2
  • 15
  • 33
0

The tilde ~ in ~2.0@dev exactly mean to go up for immediate version :

For Example :

If we have ~2.0@dev it goes to up immediate next version => ~2.x@dev

Selim Reza
  • 683
  • 1
  • 11
  • 31