BACK

Laravel 6 Homestead

Install Laravel Homestead Virtual Machine on Windows 10

Hello. You are reading an extensive guide on how to install Laravel 6.x Homestead virtual machine ( vagrant box ) on Windows 10.

Introduction

I was teaching at the private coding academy when I have realized that many people that are starting web development with Laravel’s framework are not sure what are the apparent differences between these 3 things: VirtualBoxVagrant, and Homestead. Let’s make it clear before proceeding further with the installation process. If you know these differences, then skip to the “Let’s Start!” section.

What is VirtualBox?

VirtualBox for Homestead
VirtualBox Dashboard

VirtualBox is free and open-source software that provides a way to run virtual machines on your desktop or laptop computer. For example, you can run Linux or Mac operating systems and vice-versa on your Windows machines without a hassle using VirtualBox software.

There are other options like VMWareParallels, or Hyper-Vbut they might not be free or easy to setup.

What is Vagrant?

Vagrant Homestead

Vagrant is an open-source software that provides an easy way to manage virtual machines ( virtual software development environments ). It simplifies the configuration process and automates repetitive tasks.

By the way, there are plenty of vagrant boxes right hereNote that the second most popular vagrant box is Homestead, with over 13 million downloads.

What is Laravel Homestead?

Homestead is a virtual machine ( vagrant box ) that provides you a marvelous development environment tailored explicitly for the Laravel framework. It perfectly runs on Windows, Linux, and Mac.

Homestead is mainly used for Laravel web application development, however, it can be used from WordPress development, too.

Homestead vagrant box includes a list of software:

  • PHP
  • Nginx
  • MySQL
  • PostgreSQL
  • Git
  • Redis
  • Memcached
  • Node

Let’s Start!

How to install Laravel Homestead on Windows?

Let’s start by configuring the BIOS.

Configuring BIOS

You need to enable hardware virtualization ( VT-x ) on your computer, and you can do that on your computer’s BIOS.

By the way, Hyper-V should be disabled for VT-x to work. To disable Hyper-V, go to control panel and type “Turn Windows features on or off”, then find Hyper-V in the list and disable it.

Hyper-V on Windows 10
Hyper-V should be turned off

Installing GIT Bash

If you already have a GIT available in your terminal, then you can skip this step. Otherwise, proceed to https://git-scm.com/, download, and install it. There is a lot of options available throughout the setup, but leave everything as it is unless you know what you do.

Installing Vagrant

I am using Vagrant 2.2.6_x86 for this Homestead tutorial. Download it from here and install it into your Windows machine, the setup is straightforward.

Installing VirtualBox

For my virtual machines provision, I am using Oracle’s VM VirtualBox 6.0.14 for this tutorial as it is entirely free. Download it from here and install it.

Adding Homestead Box

Before proceeding to this step, make sure you have Visual C++ Package 2015 installed.

After this, add the Homestead box to your Vagrant’s installation. Then open the terminal and write down this command. Be patient, because you will be downloading this vagrant box over the internet.

vagrant box add laravel/homestead

Downloading Homestead

Now it is time to download the Homestead machine from Github. I will store my Homestead machine right here – C:\Users\$USER\Server\Homestead. You can use the same path, too.

Open your terminal. Once you open the Git Bash, you should be at the root’s directory, which is usually – C:\Users\$USER for Windows. This symbol ~ on Windows 10 machine by default represents this path C:\Users\$USER.

Create a Server folder there using mkdir, jump inside it with the cd command, and download Homestead from the GitHub using git clone $url. After that, go inside the Homestead folder and choose the release branch with git checkout releaseunless you want an unstable version of Homestead.

mkdir Server
cd Server
git clone https://github.com/laravel/homestead.git Homestead
cd Homestead
git checkout release

After that use bash bash init.sh(for Git Bash or Linux / Mac) and init.bat (for CMD or PowerShell) to initialize the Homestead configuration file.

Creating an SSH Key

If you already have an SSH key generated, then you can totally skip this step. Otherwise, let’s make a one.

This is straightforward if you are using the Git Bash terminal. Open it and write ssh-keygen -o. Then follow along with the setup. I usually use the default directory, and I prefer to leave the passphrase empty.

Configuring Homestead

Before proceeding further, make sure you have already created a Code directory on C:\Users\$USER\Code. After that, open Homestead.yaml file, it is located inside your Homestead server’s directory. You can use any file editor you prefer, but I will use nano on my Git Bash terminal. You can use any of your favorite code editors like Atom or VSC to edit this file.

If you have less then 8 GB of RAM in your system, then I am highly suggesting to reduce the allocated Homestead server’s memory to 1024 MB.

I won’t change the authorize and key lines, because my SSH keys are already stored at the ~/.ssh directory. Yours probably are too, otherwise, adapt this to your needs.

Then I will map the C:\Users\$USER\Code directory on Windows, to /home/vagrant/code on my Homestead’s virtual machine. This means that this folder will be shared on these 2 machines. Everything that I put on my Windows machine inside the Code folder will be instantly available for my Homestead’s virtual machine.

Then I will add 2 sites: victorlava.test and dashboard.test. They will be stored inside the /home/vagrant/code/$site directories on my Homestead machine. Therefore on Windows, I will be able to access and edit the website files right here – C:\Users\$USER\Code\$site.

In the next step, I will add 2 databases, which will be named victorlava and dashboard.

This is how my final Homestead.yaml looks like:

---
ip: "192.168.10.10"
memory: 2048
cpus: 2
provider: virtualbox

authorize: ~/.ssh/id_rsa.pub

keys:
    - ~/.ssh/id_rsa

folders:
    - map: ~/Code
      to: /home/vagrant/code

sites:
    - map: victorlava.test
      to: /home/vagrant/code/victorlava/public

    - map: dashboard.test
      to: /home/vagrant/code/dashboard/public

databases:
    - victorlava
    - dashboard

features:
    - mariadb: false
    - ohmyzsh: false
    - webdriver: false

# ports:
#     - send: 50000
#       to: 5000
#     - send: 7777
#       to: 777
#       protocol: udp

Note that every space and symbol matters here. If you make a mistake, then the Vagrant won’t be able to process the Homestead.yaml configuration file.

Bash Profile ( Optional )

This step is optional but very convenient – it lets you start and halt Homestead globally from your Git Bash terminal.

Go to C:\Users\$USER and create a .bash_profile file with the code below. Make sure you have the right directory that leads to the Homestead server right here cd ~/Server/Homestead.

alias ..="cd .."
alias vm="ssh vagrant@127.0.0.1 -p 2222"
function homestead() {
 ( cd ~/Server/Homestead && vagrant $* )
}

Note I found this on @eaimanshoshi medium’s post, so all credits go to him. Thanks, Eaiman!

Installing Laravel

If everything went fine and you haven’t got any errors in the terminal, then go to the Homestead’s directory and login to your virtual machine.

vagrant ssh

Then head to the code directory using cd (if a directory isn’t present there, then create it with mkdir command). After this, create a Laravel application with laravel new $siteName. This will create a directory named $siteName and will install your Laravel web application inside. This can take some time.

laravel new victorlava

laravel new dashboard

After that, both of the web applications should be accessible on this link http://$site.test. For example, on my virtual machine, they are available here: http://victorlava.test and http://dashboard.test. If something doesn’t work, then try a troubleshooting section below.

Laravel 6 Dashboard
Laravel 6.0 Dashboard

If you are getting something like this, then you are ready to build your first Laravel web application. Good luck!

Troubleshooting

Missing SSH key

Check your Homestead.yaml (or Homestead.json) file, the path to your private key does not exist.

Create an SSH key or comment out authorize and key lines.

Misspell or unwanted symbols in Homestead.yaml

Homestead did not find expected '-'
C:/HashiCorp/Vagrant/embedded/mingw64/lib/ruby/2.4.0/psych.rb:377:in `parse': (): did not find expected '-' indicator while parsing a block collection at line 17 column 5 (Psych::SyntaxError)

Head to Homestead.yaml file and try to find the error. You probably made a simple typo.

I do not see any changes

If you have edited the Homestead.yaml file and you don’t see any changes when firing up the Homestead virtual machine, then add a provisioning tag next to your vagrant commands. For example:

vagrant reload --provision

Homestead is running but the website is not accessible

Laravel Site Can't Be Reached
The site can’t be reached

There can be many reasons why this is happening, but the main one is that the provision scripts weren’t able to write hostnames inside your host file. Otherwise, head to C:\Windows\System32\drivers\etc open the host file with administration rights and add your hostnames there. For example, this is how my host file looks like:

#
127.0.0.1 localhost
::1 localhost

192.168.10.10 victorlava.test
192.168.10.10 dashboard.test

No input file specified

Laravel No Input File Specified
No input file specified.

This means that your Homestead machine is working fine, and the hostnames are set up correctly. However, the Homestead virtual machine isn’t able to find any files to the server. This means that you have probably made a mistake setting up your directories. It probably directs to the wrong place or to a place where there aren’t any files for a web server to serve.

Missing Visual C++ Package

The box ‘laravel/homestead’ could not be found or
could not be accessed in the remote catalog. If this is a private
box on HashiCorp’s Atlas, please verify you’re logged in via
`vagrant login`. Also, please double-check the name. The expanded
URL and error message are shown below:URL: [“https://atlas.hashicorp.com/laravel/homestead"]
Error:

If you are getting this then you probably don’t have the Visual C++ Package 2015 installed.

References

Newsletter

Get my content to your inbox every Monday. I promise, no spam included!

.

Leave a Reply

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