Search code examples
windowscmdcomposer-php

Using composer require without quotes


Why does

composer require symfony/symfony ~4.4

install Symfony 4.4.43 (as of 6 July 2022), and

composer require symfony/symfony ^4.4

installs Symfony 4.4.0 (not what I asked for), and

composer require symfony/symfony 4.4

installs Symfony 4.4.0 (sensible default), but

composer require symfony/symfony "^4.4"

installs the latest version of Symfony 4.4.43 (as it should)?

My platform is Windows 10, using the command prompt.

Why are the double quotes needed for Composer to recognise the ^ character?


Solution

  • Why are the double quotes needed for Composer to recognise the ^ character?

    Because the ^ has special meaning on the command prompt, it (the caret "^") is the escape character WP on the Windows command prompt, mind:

    My platform is Windows 10, using the command prompt.

    Unfortunately the caret ^ has as well special meaning for the version constraint (compare Caret Version Range Composer as well as for NPM/YARN etc.) so the character needs to be escape as well.

    One way to do this is to quote the value to remove the special meaning on the commandline and then it works as you have tried yourself already:

    composer require symfony/symfony "^4.4"
    

    So the shorter answer on windows is: Quote the version constrain in double quotes with composer require so that a potential caret ^ gets passed to the Composer command verbatim: "^4.4".

    Btw. something similar is the case on a Linux command prompt, the tilde ~ character can have special meaning (path to the USERs HOME directory) when at the beginning of a command line argument. Here again quoting in double quotes prevents having the special meaning: "~4.4.0" (compare Tilde Version Range Composer).