Laravel Homestead on HyperV

Update: 2015-11-11 – I’ve finished porting over the PHP 7 branches of settler and homestead. If you want to use PHP 7, follow the instructions on the standard Laravel docs site and here, except use johnpbloch/homestead-7 as your box instead of laravel/homestead-7.

Somebody sent me an email this morning asking me a quick question regarding my recent work getting Homestead running on HyperV 1.

Hello my friend,

I see repository on github about hyperv configuration to vagrant up. What are the commands to start the new machine on HyperV?

Thanks!!

I really can’t see any good reason not to share the info with the rest of the world too, so I figured I’d put it in a blog post for the edification of everybody.

Environment

Before getting into the specifics of running the VM, I’ll talk about my setup a little bit first.

I’m using Vagrant 1.7.4. You will need at least 1.7.3 to use my Homestead setup (there were some major bugs on Windows that were fixed in that version). The HyperV provider comes bundled with Vagrant, so there’s no need to worry about installing it.

One of the things about HyperV is that you can’t set a static IP address for your machines. This makes it difficult to have instructions for adding hosts to /etc/hosts. Fortunately, there’s a vagrant plugin that can detect the IP address assigned to Vagrant. It’s called vagrant-ghost:

vagrant plugin install vagrant-ghost

The other important thing about my setup is that I use the Unix tools that come with Git for Windows. On installation, you have to choose to add the git bin directory to your global path. The most important thing this gives you is bash. I use bash as my main shell (not PowerShell or CMD), so I can’t say whether any of this works from within PowerShell or CMD.

All of the following commands assume you are using bash as your shell. If that is not the case, you will need to translate the commands yourself.

Installing and Running Homestead for HyperV

First you need to initialize the homestead environment. This will clone the repository and run the init script:

git clone https://github.com/johnpbloch/homestead.git Homestead
cd Homestead
bash init.sh

Then add my vagrant box:

vagrant box add --provider hyperv johnpbloch/homestead

At this point you’ll want to configure ~/.homestead/Homestead.yaml. Open it in your text editor and modify the configuration as necessary. The only setting that you must set a certain way is to change provider to hyperv. I also bump CPUs to 2 and memory to 2048, but that’s up to you. I also add type: "smb" to each of the entries in folders, but that’s not strictly necessary, since SMB is the default option on HyperV.

EDIT (2015-08-31): I no longer override the box used by Homestead, so you’ll also need to add a box setting to the yaml file and set it to johnpbloch/homestead.

Now it’s time to run the box. HyperV boxes can only be started with administrative privileges, so you need to open a new shell using Run as Administrator. Navigate to the Homestead directory and run:

vagrant up

Vagrant is going to ask you to enter your user credentials during boot so that it can mount the SMB share points, so be aware of that. The first time it runs, it’ll provision the VM which might take some time.

Hope that helps!

Notes:

  1. HyperV is a native virtualization hypervisor for Windows Operating systems. It uses SMB shares by default for mounted directories, which are way faster than VBox shared directories. In general, it blows virtualbox out of the water in terms of performance.

    At some point, I’ll need to write about even using it with Vagrant at all.

19 thoughts on “Laravel Homestead on HyperV

  1. Martin Bakke

    This is great, thank you!

    I got this mostly working on my first try which is kind of amazing since VMs seems to hate me.
    The only thing that didn’t quite work was the vagrant-ghost plugin (I think).
    On vagrant up my hosts file is filled with several lines of what seems to be code instead the normal ip hostname lines.

    Does vagrant-ghost require any additional configuration that you didn’t cover here?

    Reply
    1. Martin Bakke

      More information about my problem:
      vagrant-ghost seems to be inserting the content of ~/homestead/aliases into the hosts file.
      Creating an aliases file in the same dir as the Vagrantfile will insert result in the contents of both files being inserted into the hosts file.

      Reply
      1. John P. Bloch Post author

        Yes, that’s a known issue with vagrant-ghost. The fix is likely to be a per-VM override to change the filename it looks for. For now, it doesn’t inhibit the functionality of the VM, though.

        Reply
  2. Michael Brooks

    Any help on this issue? Currently using Windows 10.

    An error occurred while executing a PowerShell script. This error
    is shown below. Please read the error message and see if this is
    a configuration error with your system. If it is not, then please
    report a bug.

    Script: get_vm_status.ps1
    Error:

    Get-VM : Hyper-V was unable to find a virtual machine with id “c4650435-6f62-42b1-80dc-413a93d83784”.
    At C:\HashiCorp\Vagrant\embedded\gems\gems\vagrant-1.7.4\plugins\providers\hyperv\scripts\get_vm_status.ps1:12 char:11
    + $VM = Get-VM -Id $VmId -ErrorAction “Stop”
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (:) [Get-VM], VirtualizationException
    + FullyQualifiedErrorId : ObjectNotFound,Microsoft.HyperV.PowerShell.Commands.GetVM

    Reply
  3. Miguel Rodriguez

    Hello, been trying to work this out. I’ve seen you’ve made an edit: ” (2015-08-31): I no longer override the box used by Homestead, so you’ll also need to add a box setting to the yaml file and set it to johnpbloch/homestead.”
    Could you please paste your configuration for this?

    After following your steps and running “vagrant up” this is what i get:

    The box you’re attempting to add doesn’t support the provider
    you requested. Please find an alternate box or use an alternate
    provider. Double-check your requested provider to verify you didn’t
    simply misspell it.

    If you’re adding a box from HashiCorp’s Atlas, make sure the box is
    released.

    Name: laravel/homestead
    Address: https://atlas.hashicorp.com/laravel/homestead
    Requested provider: [:hyperv]

    Where should I check those values?

    Thanks!

    Reply
    1. John P. Bloch Post author

      Hi Miguel,

      You’ll need to edit ~/.homestead/Homestead.yaml and add

      box: johnpbloch/homestead

      to your configuration.

      Reply
      1. Miguel Rodriguez

        Thank you very much!! That’s what I did, got it working already.

        On the other hand, is it necessary to install the vagrant-ghost plugin? How is it different to run “vagrant ssh” inside your Homestead folder and then run “ifconfig” to check the IP assigned? Or editing your /etc/network/interfaces file to set a static IP?
        Haven’t tried it yet but that’s something I’m planning to do.

        Thanks again, great job!

        Reply
        1. John P. Bloch Post author

          The vagrant ghost plugin isn’t strictly necessary, I just find it convenient because it can be configured to automatically add my site URLs to my hosts file so that networking Just Works™.

          Reply
  4. tom

    I get this error

    “C:/HashiCorp/Vagrant/embedded/lib/ruby/2.0.0/psych.rb:205:in parse': (<unknown>): did not find expected '-' indicator while parsing a block collection at line
    14 column 5 (Psych::SyntaxError)
    from C:/HashiCorp/Vagrant/embedded/lib/ruby/2.0.0/psych.rb:205:in
    parse_stream’
    from C:/HashiCorp/Vagrant/embedded/lib/ruby/2.0.0/psych.rb:153:in parse'
    from C:/HashiCorp/Vagrant/embedded/lib/ruby/2.0.0/psych.rb:129:in
    load’
    from C:/msys64/home/tomaz/Homestead/Vagrantfile:20:in block in <top (required)>'
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.7.4/lib/vagrant/config/v2/loader.rb:37:in
    call’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.7.4/lib/vagrant/config/v2/loader.rb:37:in load'
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.7.4/lib/vagrant/config/loader.rb:103:in
    block (2 levels) in load’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.7.4/lib/vagrant/config/loader.rb:97:in each'
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.7.4/lib/vagrant/config/loader.rb:97:in
    block in load’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.7.4/lib/vagrant/config/loader.rb:94:in each'
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.7.4/lib/vagrant/config/loader.rb:94:in
    load’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.7.4/lib/vagrant/vagrantfile.rb:28:in initialize'
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.7.4/lib/vagrant/environment.rb:689:in
    new’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.7.4/lib/vagrant/environment.rb:689:in vagrantfile'
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.7.4/lib/vagrant/environment.rb:441:in
    host’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.7.4/lib/vagrant/environment.rb:207:in block in action_runner'
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.7.4/lib/vagrant/action/runner.rb:33:in
    call’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.7.4/lib/vagrant/action/runner.rb:33:in run'
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.7.4/lib/vagrant/environment.rb:428:in
    hook’
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.7.4/lib/vagrant/environment.rb:671:in unload'
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.7.4/bin/vagrant:177:in
    ensure in ‘
    from C:/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.7.4/bin/vagrant:177:in `’

    Reply
      1. tom

        sir i reinstalled everything and now its working. At the end i thing the problem is if i add smb to config.

        At the end i think hyper-v should be avoided for homestead because i cannot change folder permissions in shared vagrant folder

        Reply
  5. Manuel

    Am I the only one who struggles all day long with issues getting Homestead running on Hyper-V?
    I managed to get so far that it actually boots up and signs in correctly. Now it fails during mounting of the shared folders.

    Here is what I get: http://pastebin.com/PvHzbY1p
    (At the bottom you find a readable version of the outputs, don’t know why it’s messing it up like that)

    Running on Win10 x64. Need it for local Webservice simulation during Windows Phone development (thus I need Hyper-V in order to run the WP Emulator).

    Thanks for your help!

    Reply
  6. Brian

    Just got done installing this on a fresh Windows 10 Pro x64 Install.

    Ran into two annoying bugs.
    1) If you receive an error about the box not being found. It could be because the version of Curl that ships with Vagrant requires a dll that is missing by default. Solution is to install Microsoft Visual C++ 2010 x86. Alternatively you could replace the version of curl with one not requiring the dll.

    2) Vagrant 1.8.4 has a bug currently on Windows 10 in that it is reading the status of Hyper-V VMs incorrectly.
    There’s a fix you can manually apply by using this file: https://github.com/mitchellh/vagrant/blob/master/plugins/providers/hyperv/scripts/get_vm_status.ps1
    Save that file and copy over the file at the following location:
    C:\HashiCorp\Vagrant\embedded\gems\gems\vagrant-1.8.4\plugins\providers\hyperv\scripts\get_vm_status.ps1

    Figured I’d post solutions incase anyone else runs into the same errors.

    Reply
  7. Fábio

    When provisioning the machine the Postgres scripts gives an error.
    On the machine itself the postgres service isn’t running and that’s probably the problem.
    I can’t put the service running (i dont know the password) so I couldn’t test it

    I’ve commented out the scripts for the postgres provisioning and it works fine (I’ve no need for the postgres since I’m using MySQL)

    This is the error
    default: Running: script: Creating Postgres Database
    ==> default: createdb: could not connect to database template1: could not connect to server: No such file or directory
    ==> default: Is the server running locally and accepting
    ==> default: connections on Unix domain socket “/var/run/postgresql/.s.PGSQL.5432”?
    The SSH command responded with a non-zero exit status. Vagrant
    assumes that this means the command failed. The output for this command
    should be in the log above. Please read the output to determine what
    went wrong.

    Reply
    1. John P. Bloch Post author

      Yeah, that’s something I need to fix in the base box. The username/password is vagrant/vagrant, in case you need to log into the machine (this is a vagrant standard, btw, so should work on most vagrant boxes).

      Reply
      1. Fábio

        Ah, didn’t really know about that.
        It will be useful for fixing network related issues (the most common I’ve encountered)

        PS – Nice jog with this one. I did want to use Homestead but with no hyper-v provided boxes for Vagrant, I was ready to install WAMP xD
        Great job, thanks!

        Reply

Leave a Reply

Your email address will not be published. Required fields are marked *