Paulund
2017-09-09 #laravel

Using Homestead Per Project

When using Homestead with your Laravel projects there are two approaches you can take with it. You can either install Homestead globally and use all your sites on a single Homestead virtual machine or you can use a single Homestead on a per project basis. You may think that just having one virtual machine to manage is the best approach to take, but you may find some difficulties when working on different projects. The advantage of having a single Homestead instance per project basis is that you can customise it just for that single project, so if you need to install other software on the box you can do so and it doesn't affect other projects. Having a single Homestead instance per project also allows you to add the server setup to version control so if you're working on the project with other developers you can make sure they have the same setup as you. If you haven't used Homestead before you can see information on how to install it globally here

Install Homestead Per Project

The best way to install Homestead per project is to require it using composer.


composer require laravel/homestead --dev

Once it has been installed there will be a homestead file in vendor/bin, you can now make the Homestead.yaml file by running the command.


php vendor/bin/homestead make

In this file you can customise the virtual box by changing the IP, Folders, Databases, Name, Hostname. If you want to bring in other Laravel projects into this virtual machine all you have to do is add the mapping to this yaml file. When you ran the make command homestead will also create an after.sh file to your project this is where you can put standard commands you want to run after the box has been configured. This is the perfect place to put commands you will need other developers to run on the box such as a composer install.


#!/bin/sh

# If you would like to do some extra provisioning you may
# add any commands you wish to this file and they will
# be run after the Homestead machine is provisioned.

cd Code/
composer install --no-progress
php artisan migrate --seed

Now with the after command setup we can provision our new vagrant box by running vagrant up, once the box has been provisioned we can login to the box by using vagrant ssh. You can then add the Vagrantfile, after.sh and aliases to your version control, when other developers are getting started on your project all they will need to do is clone the repository, create their Homestead.yaml file by using the make command and then they can spin up the same box as you by running vagrant up.