Homestead is an official pre-packaged Vagrant box. Homestead accommodates a Vagrant box that creates an elegant development environment with all the necessary server softwares. For instance when you install windows/linux on your Mac using virtual box you have a totally different OS inside your Mac. That OS will never interrupt your Mac OS. Same thing Homestead does by creating a Vagrant box which will never mess up with your computer’s OS.
Vagrant is a box which houses all the necessary softwares to run PHP. If we continue the previous example, if Homestead is a virtual machine then Vagrant is the OS for that VM. You can check more about Vagrant on their official site.
There are some dependencies software you have to have on your machine before you attempt to install Homestead. 1. VirtualBox/VMWare/Parallels/Hyper-V 2. Vagrant 3. Composer (Optional as it comes with Vagrant) 4. Git (optional, you can do everything with terminal on Mac). We will use VirtualBox in this tutorial. Below are the links to download and install these software.
mv composer.phar /usr/local/bin/composer
You need to be in the folder first where you want to install Homestead Vagrant Box. I will install it in my home directory so Homestead can serve all my Laravel projects. Type the below commands on your terminal.
vagrant box add laravel/homestead
Run the below command in your home directory to install Homestead.
git clone https://github.com/laravel/homestead.git ~/Homestead
Homestead.yaml is the configuration file. We will go over this file later below. Run the below command on your terminal to install Homestead.yaml in your homestead folder.
This is the file where you have all the configuration for your virtual environment created by Vagrant. In the below picture you can see there is a public and private key. This should be in your home folder if not we will see how to generate one. The folder name is code were you will put all your code. You need to create this folder using the below command in your home directory. Make sure the name matches exactly with the folder name in your path of your Homestead.yaml. In our case it will be Code.
Below you can see there is folders , sites and databases. Notice all of them are plural, because you can create more than one for each of those if you need, Means you can simply copy ‘-map’ and ‘to’ these two items under folders and paste it under ‘to’ and change the name if you have more than one project. Make sure they alien exactly same as the above -map and to otherwise you will have an error. Once you are done then you need to do the same for sites and database for your 2nd project.
As showed in the first picture, you need to have public and private keys in order to run Vagrant successfully. First check in your home directory whether you have .ssh file or not. Run the below command to see the existence of .ssh file from home directory.
ssh-keygen -t rsa
To run Vagrant, run below command from Homestead folder
Now that everything is set up it’s time to start Homestead by running below command.
Finally we are at the point where we can now install Laravel. The tough part is over now sit back and relax and let your Homestead do the job for you. Follow below command to have Laravel installed in your Homestead. Make sure you started your Homestead and you can see the Code folder as showed above
laravel new folder-name-of-your-project
php artisan serve
There is no command to shut down the local server you just started above. You need to type below keys when you are on terminal to shut down your local server for Laravel.
Imagine you have to copy and paste this link every time you start your server. It’s not convenient at all. Let’s change it to something more easy to use. First go to your Homestead.yaml file. You will see an IP address at the very top. Copy this and paste it into your /etc/hosts file and put what so ever name you like to replace with the ugly link to navigate your Laravel project. Follow the below instruction.
sudo nano /etc/hosts