{{announcement.body}}
{{announcement.title}}

Laravel Homestead With Windows 10 Step-by-Step

DZone 's Guide to

Laravel Homestead With Windows 10 Step-by-Step

In this article we discus how to set up Homestead for Laravel 5.2 in Windows 10 with VirtualBox and avoid common mistakes.

· Web Dev Zone ·
Free Resource

I am going to write down a step-by-step procedure to set up the Homestead for Laravel-5.2 in Windows 10 with VirtualBox. I spent a lot of time to set up the 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. Let's dig in.

The official documentation for Laravel Homestead setup is: Official Documentation.

N.B: Please try to type all the commands instead of copy-paste from this tutorial. It may cause unexpected errors. See the response section below for more information.

Step 1

As the official documentation says, you need to enable hardware virtualization (VT-x). To do this, 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 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.

You may also like: The Eight Biggest Laravel Development Mistakes You Can Easily Avoid.

Step 2

Now, you need to download the latest version of VirtualBox and vagrant. 

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:


Shell




xxxxxxxxxx
1


 
1
vagrant box add laravel/homestead



If you are now getting an error like this:

Plain Text




x


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



Then, download this MS Visual C++ 2010 x86 Redistributables and install it. Now, run the following command again:

Plain Text




xxxxxxxxxx
1


1
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

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

Java




xxxxxxxxxx
1


 
1
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:

Java




xxxxxxxxxx
1


 
1
cd Homestead
2
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 in C:\Users\USER_NAME\Homestead folder)

Step 6

Now, we need an ssh key. To check it is already exists 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:

Java




xxxxxxxxxx
1


 
1
ssh-keygen -t rsa -C “”



Then, the command prompt will ask you two things. You don’t need to type anything, just press enter to whatever the command prompt asks 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, inside it.


Step 7 

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

Plain Text




xxxxxxxxxx
1
34


 
1
  -
2
ip: “192.168.10.10
3
memory: 2048
4
cpus: 1
5
provider: virtualbox
6
 
           
7
authorize: ~/.ssh/id_rsa.pub
8
 
           
9
keys:
10
  ~/.ssh/id_rsa
11
 
           
12
folders:
13
  map: ~/Code
14
 to: /home/vagrant/Code
15
 
           
16
sites:
17
  map: homestead.app
18
 to: /home/vagrant/Code/Laravel/public
19
 
           
20
databases:
21
  homestead
22
 
           
23
# blackfire:
24
#  id: foo
25
# token: bar
26
# client-id: foo
27
# client-token: bar
28
 
           
29
# ports:
30
#  send: 50000
31
# to: 5000
32
#  send: 7777
33
# to: 777
34
# protocol: udp



I will explain the file step-by-step and also modify it to configure our Homestead. Let's start.

Java




xxxxxxxxxx
1


 
1
ip: “192.168.10.10
2
memory: 2048
3
cpus: 1
4
provider: virtualbox



These lines specify on which IP address our Homestead will listen (in this case 192.168.10.10), 
the maximum amount of memory it can consume (2048), how many CPUs it will use (1), and the provider (VirtualBox). 


Plain Text




xxxxxxxxxx
1


 
1
authorize: ~/.ssh/id_rsa.pub
2
 
           
3
keys:
4
  ~/.ssh/id_rsa



In these lines, we are going to set up our ssh keys for Homestead. Remember we have created our ssh keys in step 6. We are going to point to those two files in our Homestead.yaml file. After editing these two lines, it will look like this:

Plain Text




xxxxxxxxxx
1


 
1
authorize: c:/Users/USER_NAME/.ssh/id_rsa.pub
2
 
           
3
keys:
4
  c:/Users/USER_NAME/.ssh/id_rsa



Don’t forget to use the lowercase of your drive name ("c" instead of "C") and forward-slash("/") instead of backslash("\"). See what I have written. In a natural way, we should write C:\Users\USER_NAME\ .ssh, right? but no, see carefully. I have written 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 the lowercase of our drive name (like "c" instead of "C") and the forward-slash("/") instead of backslash ("\") in our Homestead.yaml file.


Plain Text




xxxxxxxxxx
1


 
1
folders:
2
  map: ~/Code
3
 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 (and vice versa). 

- map: ~/Code means the folder that 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 them. It will be clear after change:

Plain Text




xxxxxxxxxx
1


 
1
folders:
2
  map: e:/Homestead_Projects
3
 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 in the 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


Plain Text




xxxxxxxxxx
1


 
1
sites:
2
  map: homestead.app
3
 to: /home/vagrant/Code/Laravel/public



Don’t get confused about this one with the last discussion. these lines have 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, the vagrant will serve the site from /home/vagrant/Code/Laravel/public folder.

Yes, I know we have not created any folder named Laravel in our /home/vagrant/Code folder from Vagrant 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 the future, if you develop more sites, then this configuration will look like this:

Plain Text




xxxxxxxxxx
1


 
1
sites:
2
  map: homestead.app
3
 to: /home/vagrant/Code/Laravel/public
4
  map: site2.bla
5
 to: /home/vagrant/Code/site2/public
6
  map: site3.yeap
7
 to: /home/vagrant/Code/site3/public
8
   -bla bla bla bla bla    -



One more thing — the prefix of /Laravel/public, which is /home/vagrant/Code has to be the exact match of to: /home/vagrant/Code from the last section. 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/publicTHIS IS IMPORTANT.

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

Plain Text




xxxxxxxxxx
1


 
1
databases:
2
  homestead



This line will create a database in Vagrant named homestead.


After editing my Homestead.yaml file, it looks like the following:

Plain Text




xxxxxxxxxx
1
34


 
1
  -
2
ip: “192.168.10.10
3
memory: 1024
4
cpus: 1
5
provider: virtualbox
6
 
          
7
authorize: c:/Users/Eaiman/.ssh/id_rsa.pub
8
 
          
9
keys:
10
  c:/Users/Eaiman/.ssh/id_rsa
11
 
          
12
folders:
13
  map: e:/Homestead_Projects
14
 to: /home/vagrant/Code
15
 
          
16
sites:
17
  map: homestead.app
18
 to: /home/vagrant/Code/Laravel/public
19
 
          
20
databases:
21
  homestead
22
 
          
23
# blackfire:
24
#  id: foo
25
# token: bar
26
# client-id: foo
27
# client-token: bar
28
 
          
29
# ports:
30
#  send: 50000
31
# to: 5000
32
#  send: 7777
33
# to: 777
34
# protocol: udp



Step 8

Now, Windows will not allow the homestead.app link to be hit from the 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 and edit the hosts file in any text editor (text editor must have to open in administrator mode). Add the following line at the very bottom of the hosts file:

Plain Text




xxxxxxxxxx
1


1
192.168.10.10 homestead.app



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

Plain Text




xxxxxxxxxx
1


 
1
192.168.10.10 homestead.app
2
192.168.10.10 site2.bla
3
192.168.10.10 site3.yeap
4
  -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 use one of the following: “.localhost”, “.invalid”, “.test”, or “.example”.  So, you should use “homestead.test” or something else instead of “homestead.app” 

Nowadays browser forces all .dev domains to use HTTPS. You can try this. Or you can use one of the following: “.localhost”, “.invalid”, “.test”, or “.example”. 

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 by running the command vagrant up. But, to do so, we have to always run this command from C:\User\USER_NAME\Homestead directory. We can do something so that we can run vagrant boxes 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 and create a file named .bash_profile. Then, write down the following lines in the .bash_profile file:

Shell




xxxxxxxxxx
1


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



Now, using git bash from anywhere by running the homestead up command, you can run the vagrant box. To terminate, vagrant box run the homestead halt command. You might have to restart Git bash since the .bash_profile is loaded upon start. For the first time, homestead up will take some time.

I am writing down the two commands again:

Shell




xxxxxxxxxx
1


 
1
To up vagrant box use:
2
 homestead up 
3
To stop vagrant box use:
4
 homestead halt 



  • NB:
    1. If you are getting bash: cd: /c/Users/User Name/Homestead: No such file or directory” this kind of error then please replace the following line of .bash_profile cd ~/Homestead && vagrant $*   with 
      cd “YOUR_ACTUAL_HOMESTEAD_DERECTORY_PATH” && vagrant $* and of course restart git bash.
    2. If these command doesn’t work on git bash then please try to run these commands from CMD from now on.

Step 10

Now, we are going to create our first project named, Laravel. Your questions from seeing /home/vagrant/Code/Laravel/public in Step 7 will be clear now. Until now, we only have the /home/vagrant/Code folder. There is no folder named Laravel in the /home/vagrant/Code folder yet.

You can check your project folder on your PC that I am telling you 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 the homestead up  command. Then, run the following command:


This will log in 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 the Code folder. Type ls and press enter again, and you will see that there is nothing in this folder yet.

Now, it's time to create our first laravel project here. Run the following command:

Shell




xxxxxxxxxx
1


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



This command will take some time and create a laravel project in the Laravel folder. Type ls and press enter. 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 :)

Congratulations! Please share this article and leave a comment for any questions or feedback. 


Further Reading

Topics:
laravel ,tutoiral ,web dev ,php ,windows ,installation

Published at DZone with permission of Eaiman Shoshi . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}