Learning Laravel (Part1: Setting Up Laravel Homestead)

Image for post
Image for post

Hello, it’s been quite a long time since the last time I post in Medium. A couple of weeks back I moved to a new company and here we use Laravel for developing web applications. The consequence, of course, I need to familiarize myself with the environment. And this (hopefully going to be) series aim to be just that, the journey of me familiarizing myself with the environments, learning one thing or two along the way, and hopefully being quite a bit better as a developer.

Okay, let’s start.

Introduction

Since I’m learning Laravel in my Windows machine, I have several options for me get my machine ready and set up Laravel. I could just install PHP with Composer in my machine and just install Laravel CLI and create a new project (to be honest I don’t know what’s wrong with that approach). But looking around there are two main ways that people do to prepare Their machine:

  1. Using Laravel Homestead (using Vagrant and Virtual Box)
  2. Using *AMPP stack (like XAMPP, WAMPP, LAMPP, etc.)

And after reading Bruno Skvorc’s post on Site Point about the right way to start a PHP project, I am sold to using Laravel Homestead Improved (like Laravel Homestead, but with added features, thus the name Improved) for my path to learning. Btw I’m following Site Points tutorial which can be found here.

Words of warning from Laravel Homestead Page: If you are using Windows, you may need to enable hardware virtualization (VT-x). It can usually be enabled via your BIOS. If you are using Hyper-V on a UEFI system you may additionally need to disable Hyper-V in order to access VT-x.

Preparing My Computer

Actually, there are not much that’s needed to be done. You need to install several things first.

  • Virtualbox, because we need the tools to host our VM
  • Vagrant, because we’ll be using Laravel Homestead, which is a Vagrant Box.
  • Git, I personally use Cygwin which also includes many more Linux commands beside git inside it.

There are many articles out there that covers installing those requirements, so I won’t elaborate it here.

I personally use Hyper as my terminal, You could use git bash which comes with Git for Windows, and it should be just fine.

Cloning the Repo

For every project that we’ll make, we need to clone the Laravel Homestead Improved repo and rename the folder to our project name. In short, you simply use these following command to clone the repo.

git clone https://github.com/swader/homestead_improved learn_laravel

Of course, change to your project name. After you finished cloning, go to your project folder, and create a public folder.

cd learn_laravel
mkdir -p Project/public

in case you’re wondering just as I am the first time seeing the directive, it stands for parents. That is, it creates both the parent folder and its child if the parent folder didn’t exist.

Why do we need to create the public folder? It is to ease the workflow of working with a VM. With the public folder, You can edit the public folder in your host OS and the changes will be reflected in your VM. Neat right?

But before we can use the public folder, we need to link the folder in our host OS to the folder inside our VM. Fear not, because Laravel Homestead Improved has a script to help us do the work. Just execute the following line in your bash (or bash-like) terminal:

bin/folderfix.sh

And, Poof! It will the Public folder so You can use it as I said before. If You want to link another folder, you can do so manually later from . And also Laravel documentation got You covered for configuring shared folder. Next we need to setup our project Sites

Configuring Sites

So what we’ll do exactly in this part?

We want to develop a website or web apps right? And how do we usually access those? by accessing its address. For example, if we want to use Gmail we will go to . So in this step, we will make it so that to access our sites or apps we could access it like we’re accessing Gmail.

To do so, there are two things that we need to configure:

  • Host file: so our browser knows how to redirect the address
  • homestead.yaml: to tell our application exactly to which folder will your website be

Before proceeding please make sure that you don’t have VPN or DNSCrypt running in your machine because it will affect how your browser interprets the sites. Because when writing this blog I encounter a headache inducing problem just because I use DNSCrypt.

Configuring Host file

I just found out about how cool this file is when I learned Laravel. Just imagine, you can redirect any site to any IP address. I bet this file played a big role in blocking my school computer from accessing Friendster in the past. In Windows you can find this file in this folder:

C:\Windows\System32\drivers\etc\hosts.txt

To open and edit the file you need to open notepad as administrator then open the file from inside notepad. I haven’t found any other way to do it besides that. So when you open the file you will be greeted with a bunch of comments and a pair of IP Address and hostname. In short, more or less this will be what you see:

Image for post
Image for post

Now what we need to do is add our desired hostname into our host file, into the host file. In this case, I will add to the bottom of the file. By adding this file we’re telling computer to go to 192.168.10.10 if we access learnlaravel.app.

Configuring homestead.yaml File

file contains Laravel Homestead configurations. For starters what we need to configure only the part where it said . Now what we need to do is telling this file that we want to go to when we open our site ( in my case ). To do so we need to add the following code inside our :

sites:
- map: learnlaravel.app
to: /home/vagrant/Code/Project/public

After all that now we’re ready to fire up our Vagrant box.

Fire up The Box!

We will at last use vagrant commands, to start Laravel Homestead all we need to do is call

vagrant up

in the root folder of our project. It will take quite a bit time the first time you run it because it will download the box needed to run Homestead. But when you do it the second time it should take a shorter time, since you don’t need to download the box. After it finishes, you can access the box using the command

vagrant ssh

after you’re connected you can use it as you use normal ubuntu terminal. But with some functionalities already prepared for you.

Laravel Homestead use to provision (more or less another way of saying setting up the system for the first time) the Vagrant box. So if you need to change the after creating the vagrant box using you need to reprovision the box by using . Or else the changes won’t be reflected on your VM.

The Hello World Part?

After all those works we haven’t been able to see the result right? now let’s add something that we can see for sure. In your host machine (not the vagrant box) go to folder and create a file named . And write this inside:

<html>
<head>
<title>learnPHP</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?>
</body>
</html>

now when you go to you will see the following

Image for post
Image for post

LOL, unfortunately, I don’t know what went wrong and still using localhost:8000 to access my site which works like charm.

Image for post
Image for post

So this problem conclude the first part of this series. Hopefully I could find the fixes fast :)

Conclusion

Congratulation on setting up your first Laravel Homestead environment! :)

I know it’s not as easy as the *AMPP solution which only requires you to click through the installation steps. Yet it feels more hacker-y doing it this way right? Besides by trying to set up Laravel Homestead personally, I learned a lot of things about VM and networking, and more when I wrote this article. Hope this will help others and hit me up in the comment if You get anything to ask, I’ll be happy to help.

Written by

Misadventure of programming, doodle, and friends

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store