Laravel Homestead with Windows 10 Step by Step setup procedure with explanation.

Laravel Homestead with Windows 10

I am going to write down step by step procedure to setup homestead for Laravel-5.2 in windows 10 with virtualbox. I spent a lots of time to setup homestead for Laravel-5.2 in my windows 10 PC. I am writing this so that anybody could get benefit from this post. Well, enough talking. Lets dig in.

The official documentation for Laravel Homestead setup is: Official Documentation

N.B: Please try to type all the command instead of copy paste from this tutorial. It may cause unexpected error. See the response section bellow for more information.

Step 1:
As the official documentation says, you need to enable hardware virtualization (VT-x). to do it follow this site: http://www.howtogeek.com/213795/how-to-enable-intel-vt-x-in-your-computers-bios-or-uefi-firmware/

If this doesn’t help, then google it with your laptop model number or with your PC configuration. You must have to enable hardware virtualization (VT-x). And If you are using Hyper-V on a UEFI system you additionally need to disable Hyper-V in order to access VT-x.

Step 2:
After passing Step 1, now you need to download the latest version of virtualbox and vagrant. 
Virtualbox download link: https://www.virtualbox.org/wiki/Downloads
vagrant download link: https://www.vagrantup.com/downloads.html
After downloading these, first install virtualbox. And then install vagrant. You may need to restart your PC after the installation complete.

Step 3:
Now we need to install git bash (if git bash is already installed in your PC, then skip this step).
Download link: https://git-scm.com/download/win
after downloading, install it.

Step 4:
Now open git bash in administrator mode and run the following command:

vagrant box add laravel/homestead

if you are now getting an error like this:

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:

then download this MS Visual C++ 2010 x86 Redistributables and install it. now run the following command again:

vagrant box add laravel/homestead

it should add the laravel/homestead box to your Vagrant installation. It will take a few minutes to download the box, depending on your Internet connection speed.

Step 5:
Now, after completing Step 4, type cd ~ on you git bash and hit enter. Now run the following command:

git clone https://github.com/laravel/homestead.git Homestead

it will clone the Homestead repository into a Homestead folder within your home (C:\Users\USER_NAME) directory.

now run the following two commands one by one:

cd Homestead
bash init.sh

this will create the Homestead.yaml configuration file. The Homestead.yaml file will be placed in the C:\Users\USER_NAME\.homestead directory.

NB: (According to this #06b52c7 change, from Feb 17, 2017, the Homestead.yaml file will be now located on C:\Users\USER_NAME\Homestead folder)

Step 6:
Now we need ssh key. To check it is already exist in your computer or not go to C:\Users\USER_NAME\ directory and try to find out a folder named .ssh. If it exists, go into the folder and try to find out two files named id_rsa and id_rsa.pub. If the folder .ssh doesn’t exist or the folder exists but the two files named id_rsa and id_rsa.pub doesn’t exist then run the following command:

ssh-keygen -t rsa -C “your_email@example.com

then the command prompt will ask you two things. you don’t need to type anything, just press enter what ever the command prompt ask you.
after finishing this command a new .ssh folder (if already not exist) will be created with the two files named id_rsa and id_rsa.pub into it.

Step 7:
Now we are going to edit the Homestead.yaml file which is generated in Step 5. This step is very very important. Go to the C:\Users\USER_NAME\.homestead directory. And now open the Homestead.yaml file with any text editor. The file will look like this:

 — -
ip: “192.168.10.10”
memory: 2048
cpus: 1
provider: virtualbox
authorize: ~/.ssh/id_rsa.pub
keys:
— ~/.ssh/id_rsa
folders:
— map: ~/Code
to: /home/vagrant/Code
sites:
— map: homestead.app
to: /home/vagrant/Code/Laravel/public
databases:
— homestead
# blackfire:
# — id: foo
# token: bar
# client-id: foo
# client-token: bar
# ports:
# — send: 50000
# to: 5000
# — send: 7777
# to: 777
# protocol: udp

I will explain the file step by step and also modify it to configure our homestead.
Lets start.

ip: “192.168.10.10”
memory: 2048
cpus: 1
provider: virtualbox

These lines say that on which ip address our homestead will listen and it is 192.168.10.10 (you can edit it)
how much memory will it consume (max) and it is 2048 (you can edit it)
it will use one CPU
and the provider is virtualbox.


authorize: ~/.ssh/id_rsa.pub
keys:
— ~/.ssh/id_rsa

In these lines we are going to setup our ssh keys for homestead. Remember we have created our ssh keys on step 6, right? we are going to just point those two files in our Homestead.yaml file. after editing these two lines it will look like this:

authorize: c:/Users/USER_NAME/.ssh/id_rsa.pub
keys:
— c:/Users/USER_NAME/.ssh/id_rsa

Don’t forget to use the lowercase of you drive name(c instead of C) and forward slash(/) instead of back slash(\). See what I have wrote. In natural way we should write C:\Users\USER_NAME\.ssh , right? but no, see carefully. I have wrote c:/Users/USER_NAME/.ssh instead of C:\Users\USER_NAME\.ssh this is the tricky part, don’t miss it. We will always use lowercase of our drive name(like c instead of C) and the forward slash(/) instead of back slash (\) in our Homestead.yaml file.


folders:
— map: ~/Code
to: /home/vagrant/Code

Here we are going to map a folder which will be used by both our PC and vagrant. just imagine a common folder where if we change anything from our Windows 10 PC, the change will be visible from vagrant also. And vice versa. 
- map: ~/Code means the folder which is located in our PC and to: /home/vagrant/Code means where we will access the same folder in vagrant. not clear yet? Well just see the lines after I change it. It will be clear. after change:

folders:
— map: e:/Homestead_Projects
to: /home/vagrant/Code

See now? my PC’s e:/Homestead_Projects folder and vagrant’s /home/vagrant/Code folder are pointing to the same folder. if you change anything in /home/vagrant/Code folder it will be reflected to e:/Homestead_Projects folder also and vice versa. 
in my case e:/Homestead_Projects is my project folder. In your case use your own project folder. You can use any folder name here like /home/vagrant/ANY_FOLDER_NAME instead of /home/vagrant/Code


sites:
— map: homestead.app
to: /home/vagrant/Code/Laravel/public

Don’t get confused this one with the last discussion. this lines has nothing to do with the last discussion. I am going to explain it. this configuration says that if we hit homestead.app from our browser then the vagrant will serve the site from /home/vagrant/Code/Laravel/public folder. Yea I know we have not created any folder named Laravel in our /home/vagrant/Code folder from vagrant yet or in our e:/Homestead_Projects folder from our PC yet. we will create it later. you will find your answer in step 10. In future if you develop lot more sites, then this configuration will look like this:

sites:
— map: homestead.app
to: /home/vagrant/Code/Laravel/public
— map: site2.bla
to: /home/vagrant/Code/site2/public
— map: site3.yeap
to: /home/vagrant/Code/site3/public
— — -bla bla bla bla bla — — — -

One more thing the prefix of /Laravel/public which is /home/vagrant/Code, have to be exact match of to: /home/vagrant/Code from the last discussion. in the last discussion if you have used /home/vagrant/ANY_FOLDER_NAME to map your PC’s project folder then here you have to use /home/vagrant/ANY_FOLDER_NAME as the prefix of /Laravel/public which will look like /home/vagrant/ANY_FOLDER_NAME/Laravel/public. THIS IS IMPORTANT.

Please read “N.B.” part of step 8 before proceed to next para.

databases:
— homestead

this line will create a database in vagrant named homestead.


after editing my Homestead.yaml file looks like bellow:

 — -
ip: “192.168.10.10”
memory: 1024
cpus: 1
provider: virtualbox
authorize: c:/Users/Eaiman/.ssh/id_rsa.pub
keys:
— c:/Users/Eaiman/.ssh/id_rsa
folders:
— map: e:/Homestead_Projects
to: /home/vagrant/Code
sites:
— map: homestead.app
to: /home/vagrant/Code/Laravel/public
databases:
— homestead
# blackfire:
# — id: foo
# token: bar
# client-id: foo
# client-token: bar
# ports:
# — send: 50000
# to: 5000
# — send: 7777
# to: 777
# protocol: udp

Step 8:
Now windows will not allow the homestead.app link to be hit from browser. we have to add this to the windows hosts file. so that if we hit homestead.app from our browser it will go to the IP address we defined in our Homestead.yaml file. For now our defined IP address is 192.168.10.10
go to C:\Windows\System32\drivers\etc\ folder, edit the hosts file in any text editor (text editor must have to openned in administrator mode). Add the following line at the very bottom of hosts file:

192.168.10.10 homestead.app

If you want to add another site just append here like this:

192.168.10.10 homestead.app
192.168.10.10 site2.bla
192.168.10.10 site3.yeap
— -bla bla bla bla — —

Now homestead.app is accessible from our browser. but don’t hit it yet.

N.B: https://laravel-news.com/chrome-63-now-forces-dev-domains-https
this link says “Based on this article by Danny Wahl, he recommends you to use one of the following: “.localhost”, “.invalid”, “.test”, or “.example”.
So, you should use “homestead.test” or something else instead of “homestead.app”
If all this sounds like too much trouble another viable option is to switch to Firefox as your development browser.”

Step 9:
Now we can start our homestead using vagrant box by running the command vagrant up. But to do so we have to always run this command from C:\User\USER_NAME\Homestead directory. But we can do something so that we can run vagrant box from anywhere using git bash. To do so, download this file https://www.dropbox.com/s/haekwwhab4jn56r/.bash_profile?dl=0 and paste it in C:\User\USER_NAME\ directory or in C:\User\USER_NAME\ directory create a file named .bash_profile. And write down the following lines in the .bash_profile file:

# Some shortcuts for easier navigation & access
alias ..="cd .."
alias vm="ssh vagrant@127.0.0.1 -p 2222"
# Homestead shortcut
function homestead() {
( cd ~/Homestead && vagrant $* )
}

Now using git bash from anywhere running homestead up command you can run the vagrant box. To terminate vagrant box run homestead halt command. You might have to restart Git bash since the .bash_profile is loaded upon start. (Thanks @Odin Herjan for pointing out this)
For the first time homestead up will take some time.

I am writing down the two commands again:

To up vagrant box use:
homestead up
To stop vagrant box use:
homestead halt

Step 10:
Now we are going to create our first project named Laravel. Your questions from seeing /home/vagrant/Code/Laravel/public this line in Step 7 will be clear now. Till now we have only /home/vagrant/Code folder. There is no folder named Laravel in /home/vagrant/Code folder yet. You can check your project folder on your PC that I am telling right or wrong. In my case the project folder on my PC is e:/Homestead_Projects. You will see that there is no folder named Laravel in your PC’s project folder. Well, we are now going to create it.

run homestead by using homestead up command. Now run the following command:

homestead ssh

This will login you into vagrant.
Type ls and press enter. You will see there is only one folder named Code. Type cd Code and press enter. Now you are in Code folder. Type ls and press enter again and you will see that there is nothing in this folder yet.

Now its time to create our first laravel project here. run the following command

composer create-project --prefer-dist laravel/laravel Laravel

This command will take some time and create a laravel project in Laravel folder. Type ls and press enter and now you will see there is a folder named Laravel. Go to your project folder in your PC (in my case e:/Homestead_Projects) and you will see that there is a folder named Laravel. Now you can see that the /home/vagrant/Code folder and your project folder are actually the same folder.

Step 11:
Well, everything is set now. Make sure the homestead is running. Now type homestead.app in your browser and press enter. You should see the Laravel 5 welcome page now :)

Default Laravel Welcome Page

Congratulation.

Please share and leave a comment for any question or feedback.

Like what you read? Give Eaiman Shoshi a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.