Blues Clues
Blues Clues

Reputation: 1838

How to change PHP version used by composer

I want to use another php version on my machine than the one already installed using WAMP (2 PHP version installed).

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?

Upvotes: 110

Views: 380493

Answers (17)

Lijesh Shakya
Lijesh Shakya

Reputation: 2542

Too late for the answer, but for the Windows OS, I had to do it like this:

Steps:

  1. I have alias different version of PHP using the environment variable as php8

  2. Opened the command prompt using cmd

  3. Went to the designated folder using cd path/to/folder

  4. Used where composer to know the location of the composer

  5. I used the following command to the designated project location

php8 C:\ProgramData\ComposerSetup\bin\composer.phar install

The trick was to include .phar extension in the composer.phar command in order to run in the Windows OS.

I hope it helps for the Windows OS users.

Upvotes: 0

Martin Muñoz
Martin Muñoz

Reputation: 516

In Windows if you change the Environment Variables. You should close the current windows on which you will use composer, because will remains the old environment variables values

Upvotes: 0

Mahdi Rashidi
Mahdi Rashidi

Reputation: 437

How to change PHP version dynamically in windows (Powershell)

Consider we have "C:\php" directory which includes 3 sub-directories for 3 version of php: "C:\php\56" , "C:\php\74" and "C:\php\81".

  1. Open PowerShell as an administrator.
  2. Run notepad $PROFILE command to open your PowerShell profile in Notepad.

If you don't have a PowerShell profile yet, this command will create a new one for you.

  1. Add the following lines to your PowerShell profile:
$phpVersionsPath = "C:\php"

function Set-PHPVersion {
    param([string]$version)
    $phpPath = Join-Path $phpVersionsPath $version
    $env:PATH = "$phpPath;{0}" -f $env:PATH
    $output = Invoke-Expression 'php -v'
    Write-Output $output
}

You can use shorter function name for easier execution. Something like spv

  1. Save and close your PowerShell profile.
  2. Restart PowerShell to load the updated profile.

After following these steps, you should be able to use the Set-PHPVersion function from any PowerShell session, regardless of the current working directory.

Run Set-PHPVersion 56 , Set-PHPVersion 74 or Set-PHPVersion 81 to use the version you want.

Upvotes: 1

Qrzysio
Qrzysio

Reputation: 1172

I use two xampp installations: with PHP 7.4 and 8.1. The system "Path" environment variable is set to C:\xampp\xampp8.1\php\, so the PHP 8.1 is the default PHP version in the Windows console.

RUN:

php -v
PHP 8.1.6 (cli) (built: May 11 2022 08:55:59) (ZTS Visual C++ 2019 x64)

PHP 7.4 is installed in C:\xampp\xampp7.4\php\php.exe.

RUN:

C:\xampp\xampp7.4\php\php.exe -v
PHP 7.4.2 (cli) (built: Jan 21 2020 17:52:43) ( ZTS Visual C++ 2017 x64 )

Composer.phar file is located in c:\ProgramData\ComposerSetup\bin\composer.phar.

So we can run the following:

C:\xampp\xampp7.4\php\php.exe c:\ProgramData\ComposerSetup\bin\composer.phar 

To simplify create aliases:

composer74=d:\xampp\xampp7.4\php\php.exe c:\ProgramData\ComposerSetup\bin\composer.phar

php74=d:\xampp\xampp7.4\php\php.exe

I user Cmder console, where aliases are located in c:\Program Files\Cmder\config\user_aliases.cmd.

Upvotes: 3

I will assume that you need this because a requirement to have multiple php versions installed to handle multiple projects.

If this is the case a prefer to run directly the php desired bin pointing to the executable script of composer, for example, in my case I have php 8.1 and 7.4, my main php version configured for CLI is 8.1, but I want to run composer with 7.4 in some projects, so I run this command:

php7.4 -f /usr/local/bin/composer install

Where php7.4 is the bin installed and my global composer script is in /usr/local/bin/composer

From there, you can make an alias like this to facilitate things: alias composer7.4='php7.4 -f /usr/local/bin/composer ' so next time you need to run composer with [email protected] you only need to run: composer7.4 install

Upvotes: -1

Dananjaya
Dananjaya

Reputation: 669

enter image description here

This is what happens in my case. I hope this may help to someone have 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 now working perfectly.

sudo rm -rf /usr/local/bin/php
sudo ln -s /Applications/MAMP/bin/php/php7.4.21/bin/php /usr/local/bin/php

Upvotes: 5

barkii
barkii

Reputation: 31

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.

Upvotes: 0

Rafael Bitencourt
Rafael Bitencourt

Reputation: 606

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".

Upvotes: 1

Dennis Maina
Dennis Maina

Reputation: 210

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:

  1. Run the command directly on your project folder selecting the appropriate PHP version you need. 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.
  2. make an alias to composer
  • Run this command to edit/make this file nano ~/.bashrc
  • Inside that file, put alias composer="ea-php80 /opt/cpanel/composer/bin/composer"
  • This gives you the flexibility to run composer commands as usual without those long trailing strings

Upvotes: 1

alsocodes
alsocodes

Reputation: 1

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.

Upvotes: -1

Vincent V
Vincent V

Reputation: 326

I found a very easy way to switch php versions:

  • Search for system environment variable
  • Click on "Environment variables"
  • Under "System variables" select path and click on edit
  • Move the PHP version folder you want to use before the other one. So for example: php 7.0 will now be used:

enter image description here

  • Close all windows by clicking "OK"
  • Close all the cmd windows, than open cmd and type php -v
  • You will see the correct php version loaded now: enter image description here

Upvotes: 26

Furai
Furai

Reputation: 331

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"}.

Upvotes: 23

midnight-coding
midnight-coding

Reputation: 3217

Old question I know, but just to add some additional information:

  • WAMP is used only on Microsoft Windows Operating Systems.
  • Changing the version of PHP used through the left-click -> PHP -> Version menu changes the version used by Apache to server your site.
  • Changing the version of PHP used through the right-click -> Tools -> Change PHP CLI Version menu changes the version used by WAMP's PHP CLI.

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.

Upvotes: 1

themroc
themroc

Reputation: 21

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

Upvotes: 2

0CDc0d3r
0CDc0d3r

Reputation: 639

If anyone is still having trouble, remember you can run composer with any php version that you have installed e.g. $ php7.3 -f /usr/local/bin/composer update

Use which composer command to help locate the composer executable.

Upvotes: 41

geeky
geeky

Reputation: 1652

You can change php version of composer without uninstalling it, follow these steps :

  1. Search for system environment variables in cortana.
  2. Click on the button "Environment variables".
  3. Under "System variables" select path and click on edit, you will see one entry like this "C:\wamp\bin\php\php5.6.13".
  4. Just change this to the folder name of the php located at your wamp/bin/php7.1.9, here php7.1.9 is folder name.
  5. Replace php5.6.13 with bin7.1.9, it will look like these "C:\wamp\bin\php\php7.1.9", just click ok on all the boxes.
  6. You are done.
  7. To verify, first close all the cmd windows, than open cmd and type php -v, press enter and you should see php7.1.9.
  8. If you don't see change in php version than just restart your pc and run php -v again in cmd , it will work.

Upvotes: 143

Devon Bessemer
Devon Bessemer

Reputation: 35337

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

Upvotes: 35

Related Questions