Laravel Homestead With Windows 10 Step-by-Step
Join the DZone community and get the full member experience.
Join For FreeI 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.
Step 2
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:
xxxxxxxxxx
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:
xxxxxxxxxx
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:
xxxxxxxxxx
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:
xxxxxxxxxx
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 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:
xxxxxxxxxx
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:
xxxxxxxxxx
— -
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. Let's start.
xxxxxxxxxx
ip: “192.168.10.10”
memory: 2048
cpus: 1
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).
xxxxxxxxxx
authorize: ~/.ssh/id_rsa.pub
keys:
— ~/.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:
xxxxxxxxxx
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 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.
xxxxxxxxxx
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 (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:
xxxxxxxxxx
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 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
xxxxxxxxxx
sites:
— map: homestead.app
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:
xxxxxxxxxx
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 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/public. THIS IS IMPORTANT.
Please read “ N.B.” part of step 8 before proceed to next para.
xxxxxxxxxx
databases:
— homestead
This line will create a database in Vagrant named homestead.
After editing my Homestead.yaml file, it looks like the following:
xxxxxxxxxx
— -
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 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:
xxxxxxxxxx
192.168.10.10 homestead.app
If you want to add another site, it just append here like this:
xxxxxxxxxx
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 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:
xxxxxxxxxx
# 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 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:
xxxxxxxxxx
To up vagrant box use:
homestead up
To stop vagrant box use:
homestead halt
- NB:
- 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 $*
withcd “YOUR_ACTUAL_HOMESTEAD_DERECTORY_PATH” && vagrant $*
and of course restart git bash. - If these command doesn’t work on git bash then please try to run these commands from CMD from now on.
- 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
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:
xxxxxxxxxx
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
Published at DZone with permission of Eaiman Shoshi. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments