Working With Different Versions of PHP in Laravel Applications

With Laravel Homestead

Bob Krijnen
Appstract
3 min readDec 12, 2016

--

The problem

As I’m working on different versions of Laravel projects. I need to have both PHP version 5.6 and 7. It would be nice if I could switch between these versions without a whole lot of hassle. I have been messing around with this for a bit, turns out there are a few ways to do this:

  1. You can go for the per project installation approach.
  2. Change the PHP version inside your VM
  3. Install multiple versions of Homestead (what I did)

I didn’t feel like having a virtual machine for each project. I need to be able to work on multiple projects simultaneously. I tried to change the php version inside the VM, didn’t work, fucked it up somehow..

Multiple homestead boxes

Since I already had Homestead installed I just added the latest version following the Laravel docs. Assuming you are already working with Laravel Homestead and have both Vagrant and VirtualBox installed. Here’s what you can do.

Install the latest homestead box within your “home” directory:

cd ~

git clone https://github.com/laravel/homestead.git Homestead-7

If you already have a Homestead.yaml configuration file you don’t need to run the bash init.sh command. Otherwise you will overwrite the existing one. The Homestead.yaml file will be placed in the ~/.homestead hidden directory.

cd into the Homestead-7 directory and run vagrant up. Once the box is downloaded and up, ssh into the vm with vagrant ssh, check the PHP version with php -v. You should see that php version 7 is running.

Basically this was all I had to do, I already have an instance of Homestead with php 5.6. But if you don’t, here’s what you can do.

Simply repeat the steps above only this time clone the older branch of Homestead (the one with php 5.6) into another folder in your ‘home’ directory.

cd ~

git clone -b 2.0 https://github.com/laravel/homestead.git Homestead

Again, cd into the Homestead directory and run vagrant up. Once the box is downloaded and up, ssh into the vm with vagrant ssh, check the PHP version with php -v. You should see that php version 5.6 is running.

If you didn’t change or overwrite anything, both versions point to the same configuration file, the one in your~/.homestead folder.

Usage

Now choose the box you need, just cd into the right directory and run vagrant up. To simplify this process you can add a function for the boxes to your aliases file, might be handy for daily usage.

alias homestead='function __homestead() { ( cd ~/Homestead && vagrant $* ); unset -f __homestead; }; __homestead’

Make sure to tweak the ~/Homestead path in the function to the location of your actual Homestead installation. Now you can use homestead up from anywhere on your system and will automatically point that command to your Homestead installation. Of course the same is true for the homestead7 alias.

alias homestead7='function __homestead7() { ( cd ~/Homestead-7 && vagrant $* ); unset -f __homestead7; }; __homestead7'

Handy

A nice tool to manage your machines is with vagrantmanager.

--

--

Bob Krijnen
Appstract

Programmer, passionate about Laravel, Vue and all things related.