Profile Picture
Written ByMd Tanveer

Homestead for Laravel

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.

Coding
283
Jul 31, 2019 @ 6:11 PM

What is Vagrant

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.

How to install Homestead on Mac

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

Once you are done with installing composer run the above code on your terminal. This will put the composer PHAR in the directory that is part of your PATH. Then you will be able to run composer command from any directory you are be in.

Creating the Homestead Vagrant Box

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.

cd ~

this will take you to the home directory. Then type the below command.
vagrant box add laravel/homestead

This will take few minutes to install depending on your internet speed. If it fails make sure your Vagrant installation is up to date.

Installing Homestead

Run the below command in your home directory to install Homestead.

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

This will create Homestead folder in your home directory and clone everything from github.

Installing Homestead.yaml

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.

cd ~/homestead

This will bring you to homestead folder in your home directory. Then run the below command.
bash init.sh

This will create homestead.yaml file in your homestead folder.

Exploring Homestead.yaml

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.

mkdir code

This will create a Code directory in your home folder. Make sure you are inside your home directory before you make one. Or you can just go to your home directory and create Code folder using GUI.
nano ~/Homestead/Homestead.yaml

This will open your Homestead.yaml file in edit mode on Mac. You should be able to see something exactly above image.

Creating more than one project and configuring Homestead.yaml

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.


Follow the below instruction to quit Homestead.yaml file and save it.
ctrl + x

Generating SSH key if it does not exist

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.

ls -la

This will show you all the system file that has a . In front of them. Here is how .ssh file looks like.

If you don’t see it after running above command then run below command from home directory.
ssh-keygen -t rsa

This will generate both public and private keys for you. It will ask you where to save it. Hit enter as you are already in the home directory. Keep hitting enter until it’s done.

Run Vagrant or Refresh Vagrant

To run Vagrant, run below command from Homestead folder

vagrang up

If you want to refresh your vagrant box run below command from Homestead folder. Make sure you have Vagrant box up and running by typing above command.
vagrang provision

Start Homestead

Now that everything is set up it’s time to start Homestead by running below command.

vagrant ssh

If everything went well you should be able to see below image.

To see the Code folder type ls. If you do not see the Code folder then either you haven’t created the folder in right directory (Should be in home folder), or your Homestead.yaml is not correctly set up (check the name of the folder and the name you put in Homestead.yaml). Refresh your Homestead by following above command. If everything seems good but still Code folder is not appearing then follow the below process.
exit

To exit from Homestead. Then go to homestead directory and destroy your homestead by following below command.
vagrant destroy

It will destroy everything you have set up so far. Then run vagrant up.

Installing Laravel

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

cd Code

This will bring you to the Code folder. Remember this is the folder we will have all our code in. Then run below command.
laravel new folder-name-of-your-project

This will download all the Laravel files inside the folder ‘folder-name-of-your-project’. Once it’s finished you will be able to see all the folders Laravel offers inside ‘folder-name-of-your-project’. Now follow the below commands to start your Laravel project.
cd folder-name-of-your-project

This will bring you into the folder you downloaded all the files in.
php artisan serve

This will start the server and you will see something like this.

Here you see , this is the local server created for me. If you paste it onto your browser you will see the landing page of Laravel.

Shut down the server

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.

ctrl + c

Change the name/link of local server to a convenient one

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.


Here you can see 2 windows are open. One is Homestead.yaml file (top one) where you need to copy the IP from. Beneath that you can see the hosts folder open in edit mode. Now paste the IP address and name it to whatever you want. I named it to laravel.app. Here is the step by step command:
nano ~/Homestead/Homestead.yaml

Open Homestead.yaml file in edit mode so you can copy the IP. Now copy the IP and paste it into your hosts file using below command.
sudo nano /etc/hosts

It will ask for your administration password as you and asking for super user’s access. Put your password. Then paste the IP address and give it a name. Now close it and save it by below command.
ctrl + x

Hit ‘y’ to save it and press enter. Now if you go to your browser and type the name you gave to your local server for Laravel you will see the Laravel page.