I want to use another php version on my machine than the one already installed using WAMP (2 PHP version installed)
.
PHP 5.6
PHP7.0
. Whenever I choose PHP 7 from the control panel of WAMP and then run php -v
it still printing PHP5.6 (CLI)...
instead of PHP7
.
How can I use PHP7 without reinstalling the composer again?
You can change php version of composer without uninstalling it, follow these steps :
php -v
, press enter and you should see php7.1.9.php -v
again in cmd , it will work.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:
C:\full\path\to\php.exe C:\full\path\to\composer.phar install
If anyone is still having trouble, remember you can run composer with any php version that you have installed eg $ php7.3 -f /usr/local/bin/composer update
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:
"config": {
"platform": {
"php": "<ver>"
}
},
Where <ver>
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"}.
I found a very easy way to switch php versions:
I found out that composer runs with the php-version /usr/bin/env finds first in $PATH, which is 7.1.33 in my case on MacOs. So shifting mamp's php to the beginning helped me here.
PHPVER=$(/usr/libexec/PlistBuddy -c "print phpVersion" ~/Library/Preferences/de.appsolute.mamppro.plist)
export PATH=/Applications/MAMP/bin/php/php${PHPVER}/bin:$PATH
This is what happens in my case. I hope this maybe help to someone have the same situation. I'm using macOS Monterey with MAMP.
I linked the php 7.4 using ~/.profile file. So the terminal it says I'm using php 7.4. However, still my composer giving an error saying i'm using php 7.3.
So I check the php path using
which php
This gives me the /usr/local/bin/php as my php cli location. So I remove the file and made a symlink to my php7.4 and working perfectly fine.
sudo rm -rf /usr/local/bin/php
sudo ln -s /Applications/MAMP/bin/php/php7.4.21/bin/php /usr/local/bin/php
After a long search on the internet and finding many unrelated answers / ones that did not work for me, Here is what worked for me. Those who are in shared hosting know that bin
directory is write-protected and running sudo
commands or any system-wide command is not allowed.
There's two ways of solving this:
ea-php80 /opt/cpanel/composer/bin/composer update
To get available PHP on your server type ea-php
and hit TAB
to see a list. nano ~/.bashrc
alias composer="ea-php80 /opt/cpanel/composer/bin/composer"
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.
The Windows batch script "WampServer-PHP-CLI-Version-Changer" can be found at https://github.com/custom-dev-tools/WampServer-PHP-CLI-Version-Changer
I hope this helps others.
Good luck.
Answers above are absolutely solving the problem. But if you don't wanna change that Path
ENV Variable yourself every time, I created a tool with C++ that does the job for you with a pretty easy cli interface.
PHP Version Switcher Github Repo
It only modifies the User Path Variable instead of System Path Variable (Both Works The Same BTW except their accessibility)
Example Usage (switching to another php version):
switcher set --version=80
If you are using Windows, all you have to do is change the path to php.exe in the composer.bat file located in: "C:\ProgramData\ComposerSetup\bin".
In my case I include paths to all php versions, whenever I need to run a project on a specific php version, I just move the required path to the top (using these buttons in the right) and then close all the terminals and restart my wampp server. The path with listing in the will be selected as your php version by windows This is the simplest solution I think.
If you still facing the problem after changing Environment variables in windows, try to delete directory or just rename directory of your old php. I've done it and it's work.
Came here by the title, but the question specifies WAMP; which this may not easily apply to. So, in my case - using a Mac.. so more like a MAMP - if you have brew and the below versions installed, this could help - and composer picks it up.
brew link --overwrite --force php@8.1
php -v
#PHP 8.1...
brew link --overwrite --force php@7.4
php -v
#PHP 7.4...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.