Change PHP version used by Composer on Windows
I have already use WAMP 2.5 with PHP 5.5.12, and with Composer. The php is on:
For new project, I need to use nginx and installed PHP 7. The php is on:
Now, using GitBash MINGW32, I tried to install laravel 5.3 using Composer create-project but it said
I already put both C:\wamp\bin\php\php5.5.12 and C:\nginx\php on Windows System PATH variable.
How do I change the PHP version used by Composer?
2 Answers 2
Three ways to do this, really.
Something like alias ncomposer=`/path/to/php /path/to/composer.phar `
Specify the path to PHP version inside composer.phar itself
NB! The line will disappear upon self-update, so it’s not a reliable solution.
Move up the path with the newest PHP version
If you place C:\nginx\php first, it should be used by default when using composer.
In my case I have a folder named php733 inside xampp folder which corresponds to PHP 7.3.3. This is this other answer that helped me in creating the alias :
Finally, type this command in Git Bash :
Example : in the project that requires at least PHP 7.1.3
Using composer :
Using composer733 (the alias I created) :
It works, without having to change the environment variables
How to change PHP version used by composer
How can I use PHP7 without reinstalling the composer again?
9 Answers 9
You can change php version of composer without uninstalling it, follow these steps :
I’m assuming Windows if you’re using WAMP. Composer likely is just using the PHP set in your path: How to access PHP with the Command Line on Windows?
You should be able to change the path to PHP using the same instructions.
Otherwise, composer is just a PHAR file, you can download the PHAR and execute it using any PHP:
Use which composer command to help locate the composer executable.
Another possibility to make composer think you’re using the correct version of PHP is to add to the config section of a composer.json file a platform option, like this:
Where is the PHP version of your choice.
Snippet from the docs:
Lets you fake platform packages (PHP and extensions) so that you can emulate a production env or define your target platform in the config. Example: <"php": "7.0.3", "ext-something": "4.0.3">.
Old question I know, but just to add some additional information:
Note: It is important to understand that the «PHP CLI Version» is used by WAMP’s own internal PHP scripts. This «PHP CLI Version» has nothing to do with the version you wish to use for your scripts, Composer or anything else.
For your scripts to work with the version you require, you need to add it’s path to the Users Environmental Path. You could add it to the Systems environmental Path but the Users Path is the recommended option.
From WAMP v3.1.2, it would display an error when it detect reference to a PHP path in the System or User Environmental Path. This was to stop confusion such as you were experiencing. Since v3.1.7 the display of this error can now be optionally displayed through a selection in the WampSettings menu.
As indicated in previous answers, adding an installed PHP path (such as «C:\wamp64\bin\php\php7.2.30») to the Users Environmental Path is the correct approach. PS: As the value of the Users Environmental Path is a string, all paths added must be separated with a semi-colon (;)
After experiencing the exact same problem (IE: Choosing which version of PHP I wanted Composer to use), I created a script which could easily and rapidly switch between PHP CLI Versions depending on what project I was working on.
Change php version used by composer on windows #6277
Comments
crashbdx commented Mar 19, 2017
The text was updated successfully, but these errors were encountered:
We are unable to convert the task to an issue at this time. Please try again.
The issue was successfully created but we are unable to update the comment at this time.
alcohol commented Mar 20, 2017
I don’t understand why my composer is using a 5.4.45 php version
Please explain what this means, where this information comes from, or what you are basing this on. Because it makes zero sense without any actual context.
Seldaek commented Mar 20, 2017
@crashbdx the PATH value should contain the directory of the php.exe, not the full path including php.exe, just in case there is some confusion there.
Composer is not at fault in any case as this is OS/shell-level resolution.
denisdulici commented Jul 5, 2017
@crashbdx the only way is to uninstall and install it again 😉
@alcohol @Seldaek it would be nice if there were an option to change the following option without having to uninstall => install:
alcohol commented Jul 5, 2017
@denisdulici such requests should really be posted to composer/windows-setup. Then @johnstevenson can have a look at it and see how feasible it is 🙂
treetechsuman commented Dec 25, 2017
the only way is to uninstall and install it again 😉 work for me
geeky3 commented Mar 9, 2018
You can change php version of composer without uninstalling it, follow these steps :
linslin commented Aug 3, 2018 •
This should do it if you have composer installed globally:
AndrasZiegenham commented Aug 17, 2018
DependencyResolver easily eats up over 1GB and 32bit php is limited to 2GB memory use and oddly enough I get fatal errors near 1.3GB, so I have to use a 64bit php just for composer.
So according to this (and this: composer/windows-setup#72) topic the devs apparently don’t care about this sort of problem and want you to change your whole system’s environment to a different php. noice.
The only workaround seems to be the one mentioned by @linslin even though one can end up with monsters like this (using GitBash on windows):
Really, am I supposed to write this instead of «composer update»? Legit.
RahulDey12 commented Feb 24, 2020

check out this i have composer installed and php command working on my pc but there is no php in path variable
stof commented Feb 24, 2020
@RahulDey12 check both the user list and the system list. Both provide values for the PATH variable.
jonathanlaf commented Aug 19, 2020
Tell Composer to use Different PHP Version
I’ve been stuck at this for a few days. I’m using 1and1 hosting, and they have their PHP set up a bit weird.
Is there any way to tell Composer to use the php5.5 command? Has anyone successfully gotten Composer configured on 1and1 hosting?
I’m trying to get Laravel up and running (which uses Composer). I’ve been able to do it on my GoDaddy domain, but not 1and1.
21 Answers 21
Just add below code to your composer.json file to set different php version:
On xubuntu I had php 7.2 as default. But needed to run composer with php 5.6.
So this worked for me:
If you just need to get composer to ignore the version requirements, you can bypass using:
composer is a PHP binary script and it’s using Unix Shebang to define which interpreter for executing.
So if you’re running composer without any specific option, it will use your PHP version at /usr/bin/env php (which is your default PHP)
We can have many versions of PHP inside your Linux and you can flexible use which PHP version for executing as you want like this way:
To check & find all installed PHP packages, goto /usr/bin/
You always can use that way.
In project folder where you has composer.json file. Run the command
where php7.4 your specific version can be(php7.0, php5.5, php7.3 etc. ) where /usr/local/bin/composer path for system folder with composer
. you should have php7.4-cli
That way for linux/ubuntu user
I too have a shared hosting account on 1&1 (ionos) and here’s what I have had to do:
if you login as the root ssh account, you can create a
/.bash_profile and add
to it so that you can now use the commands you would normally use and it just works. (put composer.phar in your project folder root)
Then, make sure your laravel composer.lock file from your dev machine gets up to your project folder on 1and1 and run
Using this tip from @tobymackenzie: on shared hosts you really should just run composer install (not update!) from a composer.lock file you created on your own machine. That way the memory usage remains very low.
Hope this helps somebody.
Step 01:- Add below code to your composer.json file
Step 02:- Then, run the below command.
You could change your PATH to override the php version.
In /home/user/bin (or any other directory really) make a symlink named php to the php5 binary.
Another option, use a shell alias:
There are two ways to do it.
The existing answers partly do not answer the question, give suggestions that do not work or give bad advice.
The question stated:
However, I can run php5.5 composer.phar install, get a little bit further, but it still fails because somewhere along the line, PHP is called again, but it fails, as it’s using 4.4.6.
Instead, I am here showing a way to test and verify the suggestions.
I wish I had a better answer myself but I am unsure what is the best solution. Personally I would go with a solution which make sure PHP is called with the correct version on the host (e.g. via an alias or setting the PHP environment correctly, running the entire operation inside a container or some other solution), regardless of Composer and add a platform php constraint.
If you don’t run any scripts and just need the requirements to resolve the dependencies correctly config:platform:php would probably be your friend.
Let’s say our default php version is 7.4 but our project requires 7.2.
Check your default PHP version
Create a test script in your composer.json
Now you can test what version of PHP is used for the commands Composer calls, e.g.
In my case, both of these use my default php script (which is version 7.4). So, while the 7.2 version is used to run Composer, the scripts called by Composer use the default command.
Now let’s add the platform setting as suggested in some of the answers
Does not change anything for the script execution:
How to install a specific version of package using Composer?
I am trying to install a specific version of a package using Composer. I tried composer install and composer require but they are installing the latest version of the package. What if I want an older version?
9 Answers 9
composer require vendor/package:version
composer require refinery29/test-util:0.10.2
Add double quotes to use caret operator in version number.
As @alucic mentioned, use:
Related to question about version numbers, you can review Composer documentation on versions, but here in short:
1.2.3 is equivalent to >=1.2.3 =1.2.3
just use php composer.phar require
Also available with install.
Suppose you want to install Laravel Collective. It’s currently at version 6.x but you want version 5.8. You can run the following command:
A good example is shown here in the documentation: https://laravelcollective.com/docs/5.5/html
Of course, as the other answers point out you can run the following from the terminal:
I tried to require a development branch from a different repository and not the latest version and I had the same issue and non of the above worked for me 🙁
after a while I saw in the documentation that in cases of dev branch you need to require with a ‘dev-‘ prefix to the version and the following worked perfectly.
Curl can be used to download a specific version:
composer require package-name version-number
Example: composer require «laravel/tinker»: «^2.5»





















