Multiple Homestead versions for simple Laravel development

Sooner or later you are going to encounter the need to work multiple Laravel-applications, perhaps maintaining a legacy app running Laravel 4.2 and something new built on Laravel 5.4. All this while still using Homestead for the ease of development.

Well, how to we do this if we are looking for having access to both a PHP 5.6 VM and a 7.0 (or even 7.1) VM at the same time to take advantage of the bells and whistles in PHP 7 for our newer apps? Well, as it turns out — It is pretty easy!

Update: Since the release of Laravel Homestead 6 you can now run PHP 5.6 and 7.* simultaneously on the same Vagrant Box. It’s quite convenient, although you will manually have to switch PHP versions when you need it. Check out the docs (“Multiple PHP Versions”).

If you want to use a dedicated PHP5.6 and a PHP7.* setup, please read on.

Step 1 — Virtual Machine Management

If you’re reading this text you probably have VMWare, VirtualBox or Parallels installed. If not, just head over to virtualbox.org and download the installer. This guide uses VirtualBox as a VM Manager, but other types should work just as well.

Secondly you will need VagrantBox, so head over there and download their package too over at vagrantup.com.

Once all this is installed, add Homestead as a vagrant box:

$ vagrant box add laravel/homestead

Step 2 — Get a hold of the correct Homestead versions

So, the use case that most people will run into requiring multiple Homestead versions is the need for a pre-PHP 7.0 app and one for a post-PHP 7.0 app.

The Homestead version that we will be looking at to accomplish this is version 2.x for PHP 5.6, and 5.3 for the newest version available (at the time of this writing).

So simply prepare a neat location where you can install your Homestead-files, for example I usually go with something like this:

~/Dev/vagrant/Homestead7

And:

~/Dev/vagrant/Homestead56

There are multiple ways to actually get the base Homestead code onto your computer, but I usually go with composer. You can always clone and checkout the correct version from Github of course — this is simply a matter of taste.

Anyhow, in the Homestead7-folder, install Homestead 5.3:

$ composer install laravel/homestead ^5.3

And for your legacy version with PHP 5.6 as default, your Homestead56-directory:

$ composer install laravel/homestead 2.*

In both cases you need to initiate the installation, this is as simple as:

$ php ./vendor/bin/homestead make

Step 3 — Configuration

We’re not quite out of the woods just yet, time for the all important configuration.

As we will be using two similar, but still different setups there are a things to take note of.

Here are two rudimentary Homestead.yaml-files you can use, please note the filename to see what version correspond to which installation.

Take note:

  • For Homestead56 you need to set the version to “0.3.3”.
  • The names need to be unique.
  • As there will be clashes for the default IP-adresses, you will manually have to set two different ones (in this case 192.168.10.10 and 192.168.10.20).
  • Same goes for the MySQL-ports. If you plan on using a MySQL-tool such as SequelPro, you will need to remap the port on one of the boxes. In this case Homestead56 uses the default 33060 and Homestead7 the custom 33070.

Also, in the example above both VM-machines will use ~/Dev as an entry point for all your projects, this can be customised to your liking of course.

Now the setup is done!

Step 4 — Workflow and quick tips

Now when you run:

$ vagrant up

In your directory the Virtual Machine will be brought up and be ready to be used.

Remember to edit your hosts-file when developing using for example .app or .dev-URLs, for example:

$ nano /etc/hosts
# Homestead (PHP 5.6)
192.168.10.20 myfirstlegacysite.app
192.168.10.20 mysecondlegacysite.app
# Homestead (PHP7.1)
192.168.10.10 myfirstsite.app
192.168.10.10 mysecondsite.app

When working with multiple VM-instances I like to use some simple shortcuts to make things a bit easier to work with.

$ nano ~/.bash_profile
# Homestead
function homestead7() {
( cd ~/Dev/vagrant/Homestead && vagrant $* )
}
function homestead56() {
( cd ~/Dev/vagrant/Homestead56 && vagrant $* )
}
function homestead() {
( cd ~/Dev/vagrant/Homestead56 && vagrant up && cd ~/Dev/vagrant/Homestead $
}

This allows you to spin up both VMs using:

$ homestead

And then use homestead7 and homestead56 for shortcuts to access the VM via ssh, and more:

$ homestead7 ssh

Happy coding!

That’s it for now. This guide is mostly for myself if I ever have the need to create a similar setup again in the future, if this helps anyone out, feel free to reach out on Twitter or via my website.