29

What are the steps to downgrade the php version to 7.1 and make it default on latest Homestead 7, Homestead 7 comes with php version 7.2 as default.

There are a number of dev sites on my homestead I want everything to run on 7.1 by default, if I need 7.2 then I will use the following in my homestead yaml

sites:
    - map: homestead.test
      to: /home/vagrant/Code/homestead/public
      php: "7.2"
karmendra
  • 2,206
  • 8
  • 31
  • 49

4 Answers4

42

You can simply run sudo update-alternatives --config php and choose from the list like this:

enter image description here

You should then change the defaults for phar, phar.phar, phpize, php-config the same way you did for php

Denes Papp
  • 3,835
  • 3
  • 32
  • 33
  • 1
    Nice, This is more interactive approach in switching php version. I guess we can similarly change `phar`, `phpize` and `php-config` interactively. – karmendra Oct 31 '19 at 06:20
36

I think I have a better solution, to switch the php version in Homestead (I am using version 8 currently) running following lines of code switches the default php version.

sudo update-alternatives --set php /usr/bin/php7.1
sudo update-alternatives --set phar /usr/bin/phar7.1
sudo update-alternatives --set phar.phar /usr/bin/phar.phar7.1
sudo update-alternatives --set phpize /usr/bin/phpize7.1
sudo update-alternatives --set php-config /usr/bin/php-config7.1

Above line will switch the php version from any version to version 7.1 (Homestead 8 comes with php 7.3 by default).

Do remember to reload web-server (apache or nginx) after making this change.


July 2020 UPDATE: In newer versions of homestead to change php version there are aliases like php71 php72 php73 php74 etc. to easily swith between versions

karmendra
  • 2,206
  • 8
  • 31
  • 49
  • 1
    We are talking about changing the default php version in homestead, In my Original post I already mentioned about changing the php version for each site. Thus to change the `default version` in Homestead, above is the solution. – karmendra Jul 05 '19 at 09:24
  • Above solution can also be used to switch between any version of available php higher or lower versions on homestead. – karmendra Jul 05 '19 at 15:02
14

You shouldn't use older versions of Homestead if you just want to use a different version of PHP.

You can set each site to use PHP 7.1, that won't hurt anything. You can change the CLI version by running php71 from the command line.

joepferguson
  • 1,088
  • 7
  • 18
  • 1
    In an absolute last resort you can use https://laravel-news.com/using-older-versions-of-homestead but I would no longer recommend this because it was written before Homestead supported multiple PHP versions. – joepferguson Dec 16 '17 at 22:54
  • 3
    So looks like easiest way now is to use `php: "7.1"` tag in yaml file. – karmendra Dec 20 '17 at 08:21
  • Yes, and don't forget to run your jobs using `php7.1` like this: `$ php7.1 artisan queue:listen ...` – aaafly Sep 20 '18 at 04:58
  • 1
    As time progress, the new thing is Homestead supports multiple php version. And with experience in using the accepted solution, I see no issue switching php versions to older as per need. – karmendra Dec 03 '19 at 10:18
1

As a slightly simpler alternative to update-alternatives --config php you can enter the PHP version you want on the command line with a syntax of php71 where 7 is the major version and 1 the minor version.

For example to switch to PHP 7.1 from another version simply type php71.

vagrant@local:~/code$ php71
update-alternatives: using /usr/bin/php7.1 to provide /usr/bin/php (php) in manual mode
update-alternatives: using /usr/bin/php-config7.1 to provide /usr/bin/php-config (php-config) in manual mode
update-alternatives: using /usr/bin/phpize7.1 to provide /usr/bin/phpize (phpize) in manual mode
mattwith
  • 53
  • 4