Running Homestead from a separate directory

When creating a PHP website, there is a good chance that you wish to use a virtual machine. This has many benefits, like managing multiple versions of php, or extensions etc.

A lot of projects use vagrant in combination with virtual box. But nobody likes to set up a Vagrantfile every single time. Laravel created homestead, which deals with all the vagrant configuration for you. You can install it in your project with composer install laravel/homestead, and you got it.

But here comes the problem. If you have a linux VM, and have a windows ‘host’ system, then you will have to do the entire composer install twice. Once on the ‘host’ system, and once again inside the box. Plus if you use older versions of symfony, it could conflict with homestead.

Instead, we can install it in another directory.

So inside the project, create a new folder (We’ll use homestead), and cd into it cd homestead. There run a composer init, and specify that you want to use laravel/homestead as a dependency when asked. The rest of the file doesn’t really matter. Now run a composer install, inside this directory.

After that you can go back to the root folder cd .., and then run the following composer command: composer install --working-dir homestead. After that you can run ./homestead/vendor/bin/homestead make. This will add a few files to the root of your project.

One thing we will need to update now is the Vagrantfile.

# -*- mode: ruby -*-
# vi: set ft=ruby :

require 'json'
require 'yaml'

VAGRANTFILE_API_VERSION ||= "2"
confDir = $confDir ||= File.expand_path("vendor/laravel/homestead", File.dirname(__FILE__))

homesteadYamlPath = File.expand_path("Homestead.yaml", File.dirname(__FILE__))
homesteadJsonPath = File.expand_path("Homestead.json", File.dirname(__FILE__))
afterScriptPath = "after.sh"
customizationScriptPath = "user-customizations.sh"
aliasesPath = "aliases"

require File.expand_path(confDir + '/scripts/homestead.rb')

# rest of the file ...

Lets update one key line:

- confDir = $confDir ||= File.expand_path("vendor/laravel/homestead", File.dirname(__FILE__))
+ confDir = $confDir ||= File.expand_path("homestead/vendor/laravel/homestead", File.dirname(__FILE__))

If you did everything correctly, your project structure should look something like this:

\homestead
  composer.json
  composer.lock
  vendor/
after.sh
aliases
composer.json
composer.lock
Homestead.yaml
Vagrantfile
vendor/

If you updated this from an old repository, be sure to remove the old laravel/homestead dependency from the composer.json. And update the documentation to run composer install --working-dir homestead to get the homestead dependency, instead of the normal composer install.

Take a look at This example repository to see it in action, and for more in depth homestead configuration, see the documenatation

Versions used:

  • Composer: 1.8.3
  • Homestead: 8.0.2
Avatar
Gert de Pagter
Software Engineer

My interests include software development, math and magic.