G-Design Blog : Web Design & Development — Multiple Laravel Homestead Installations

1.5M ratings
277k ratings

See, that’s what the app is perfect for.

Sounds perfect Wahhhh, I don’t wanna

Multiple Laravel Homestead Installations

image

UPDATE : Laravel Homestead now provides the possibility to use older versions of the vagrant box by changing the configuration file : see the documentation

Since I maintain some older Laravel applications that are not compatible with the latest PHP version, I need a way to use a different Laravel Homestead installations for different projects.

Here is a solution to install different Homestead Composer packages and Homestead Vagrant boxes on Mac Os X. I will be using an older Homestead installation with PHP 5.6 for an old project and the latest Homestead installation that comes with PHP 7 for a new project.

This article uses information from the laravel Homestead documentation and from a great article by Julián Gutiérrez.

Install the vagrant boxes

I assume you have VirtualBox and Vagrant installed on a Mac Os X system.

We have to install multiple Homestead vagrant boxes to be able to use a different installation for different laravel projects.

As an example, we will be using an older Homestead box (version 0.2.7) that uses PHP 5.6 and the most recent Homestead box. (version 0.5.0 at the time of this writing)

Run the following commands to install the two vagrant boxes :

    vagrant box add laravel/homestead
    vagrant box add laravel/homestead --box-version 0.2.7

We can check the different vagrant boxes that are installed by running the following command :

    vagrant box list 

If, at a later date, we wish to remove the older Homestead box we can do so by running this command :

    vagrant box remove laravel/homestead --box-version=0.2.7

Install the Homestead package per project

Follow the steps on the laravel documentation page to install the Homestead package locally for every project.

For the new project this means executing these commands in the project directory :

    composer require laravel/homestead --dev
    php vendor/bin/homestead make

For the older project, where we want to use an older Homestead Vagrant box, we have to execute the following commands in the project directory to install an older version of the Homestead composer package :

    composer require laravel/homestead:^2.0 --dev --no-scripts
    php vendor/bin/homestead make 

Configure the Homestead installation

The Homestead make command will automatically configure the sites and folders directives in the Homestead.yaml file. Check this configuration to make sure it meets your needs and remember to update the /etc/hosts file entry for Homestead.app or the domain of your choice. See the laravel Homestead documentation for more information.

To make sure the older project uses the older Homestead vagrant box, we will use the technique described by Julián Gutiérrez.

We have to change the VagrantFile generated by the Homestead make command in the directory of the older project.

Replace this code block :

    if File.exists? homesteadYamlPath then
        Homestead.configure(config, YAML::load(File.read(homesteadYamlPath)))
    elsif File.exists? homesteadJsonPath then
        Homestead.configure(config, JSON.parse(File.read(homesteadJsonPath)))
    end

with the following code :

    settings = nil
    if File.exists? homesteadYamlPath then
        Homestead.configure(config, YAML::load(File.read(homesteadYamlPath)))
    elsif File.exists? homesteadJsonPath then
        Homestead.configure(config, JSON.parse(File.read(homesteadJsonPath)))
    end

    if settings != nil then
        config.vm.box_version = settings["version"] ||= ">= 0"
        Homestead.configure(config, settings)
    end

This will allow us to define the Homestead Vagrant box version in our Homestead.yaml file for the older project so that this project uses the 0.2.7 version.

Add the following line to the Homestead.yaml file of the older project to instruct Vagrant to use the older Homestead Vagrant box :

    version: "~> 0.2.0"

Use Homestead

You can now use the Homestead installation in every project by running the following command from the project directory :

    vagrant up

If you wish to connect to the virtual machine, you can run this command :

    vagrant ssh

And when you wish to power down the virtual machine, execute this command :

    vagrant halt

The older project should now use the older Homestead Vagrant box and the recent project should use the latest Homestead Vagrant box.

laravel vagrant homestead

See more posts like this on Tumblr

#laravel #vagrant #homestead