How to change the version of PHP in which Composer runs on Linux?

7

I installed the composer on my Ubuntu through the following command:

apt-get install composer

Ubuntu currently supports multiple versions of PHP installed. I have PHP5.6, PHP7.0 and PHP7.1 installed on my machine.

When I only had PHP5.6, the command php was running this version, so I had no problems with Composer.

However, when installing the 7.* versions, Composer started running with 7.1 version of PHP installed.

How do I make the globally installed Composer run with another version of PHP?

    
asked by anonymous 16.03.2017 / 16:22

1 answer

10

In my case, I resolved as follows.

By giving which php , it points to the path /usr/bin/php . I discovered that this is actually just a link to the latest php installation.

So I deleted link and created another one pointing to version 5.6 of PHP.

So:

sudo rm /usr/bin/php
sudo ln -s /usr/bin/php5.6 /usr/bin/php

If you run the command php -v , you will notice that the result will look something like this:

php -v

> PHP 5.6.30-7+deb.sury.org~xenial+1 (cli) 

So when running composer install , executed PHP will be the version placed in the symbolic link of /usr/bin/php .

Update

The way used above was a manual way I used to solve the problem, but the most recommended way is to use the Linux command itself, called update-alternatives . With it we can determine symbolic links for certain commands.

Just do this:

sudo update-alternatives --set php /usr/bin/php5.6

If you want to use the 7.1 version, for example, just do:

 sudo update-alternatives --set php /usr/bin/php7.1

Remembering that PHP versions should be installed in your OS: p

    
16.03.2017 / 16:22