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

Asked

Viewed 9,516 times

7

I installed Composer on my Ubuntu via the following command:

apt-get install composer

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

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

But when installing versions 7.*, Composer is now running with the version 7.1 of PHP installed.

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

1 answer

12


In my case, I solved it this way.

When giving a which php, the same points to the way /usr/bin/php. I found that this is actually just a link to the latest php installation.

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

Thus:

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

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

php -v

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

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

Updating

The way used above was a manual way that 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 to certain commands.

Just do it like this:

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

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

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

Remember that PHP versions must be installed in your :p OS

  • 1

    very good, I maintain 2 versions of PHP and HHVM in this way that you cited +1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.