How and why to use Laravel Homestead for local development — real life TIPS

Valerio Barbera

Hi, I’m Valerio, software engineer from Italy and CTO at Inspector.dev.

To be honest, I wasn’t an early adopter of local VMs as development environment (Docker, Vagrant, etc), mainly because I thought it took time to learn and I always have very limited free time.

How did I get started with Laravel Homestead?

Than I discovered Homestead, the official Laravel local development environment. It has solved my doubts and simplified my life.

This in no way is a replacement for referencing their documentation, and I encourage anyone who reads this to immediately read the docs after. So, with that being said, this article is for you if:

  • You want to learn what Homestead and Vagrant are and do.
  • You want to know why and how Homestead (or local VMs in general) will supercharge your development experience.
  • You are expanding your business and using Xampp, MAMP, you are no longer able to recreate the production environment.

I will tell you the thinking process that led me to understand what kind of benefits I had using a VM to work on my applications locally.

Vagrant

Vagrant is a tool to automate virtual machine “setup and configuration” process.

Basically to run a VM on your computer VirtualBox could be enough. But VirtualBox prepare and run just the hardware side of a VM. It’s up to you to install everything you need starting from the OS…

The team behind Vagrant has built a simple tool that get a script in input (called Vagrantfile) to install and configure automatically all softwares and features you want your server to have, on top of the hardware provided by VirtualBox.

That’s why we need both installed on our PC.

You can download and Install VirtualBox from here: https://www.virtualbox.org/wiki/Downloads

You can download and install Vagrant from here: https://www.vagrantup.com/downloads.html


VirtualBox + Vagrant already provide you everything you need to run the VM for your application. But at this stage you should learn more about Vagrant to manipulate the Vagrantfile and customize your server configuraiton to fit your requirements.

What is Laravel Homestead

Laravel Homestead is a pre-packaged Vagrant Box and Vagrant setup. Thanks to Homestead I didn’t need to learn anything about Vagrant.

This is simple for small teams, fit for small to medium size projects, it boots fast, and it’s enough to get anyone started with Laravel in seconds.

1. Install on your project (Windows 10, Mac, Ubuntu)

Looking for lavarel homestead windows 10, lavarel homestead for ubuntu…? I love this, mainly because you can install it “per project” using Composer like any other dependencies:

composer require laravel/homestead --dev

2. Generate the Vagrantfile and Homstead.yaml

Run the make command based on your OS:

Mac / Linux:

php vendor/bin/homestead make

Windows:

vendor\\bin\\homestead make

3. Map an URL for your project

The first field in the configuration file is the IP address of the VM.

It could be better to have an url that map this raw IP address to reach our application with the browser.

For the sites section, the “map” Key references the hostname that you are calling your project.

Using this hostname and the IP address we’ll need to update your computer’s host file.

  • Mac: /etc/hosts
  • Windows: C:\Windows\System32\drivers\etc\hosts

Add the entry below:

192.168.10.10 demoapp.locl

This is telling your computer that demoapp.locl is located at the server IP address 192.168.10.10 (which is your VM).


Tips! To run multiple VM for multiple project at the same time you need to have a unique IP for each of them. You can simply increase the last IP number in the Homestead.yaml file and configure properly your computer’s hosts file:

192.168.10.10 demoapp.locl
192.168.10.11 another-app.locl
...

In the Homestead.yaml file you can see:

schedule: true

This option provided by homestead allow us to run the server with a cron job configured to run the Laravel Task Tcheduling. This is what I mean with “I didn’t need to learn Vagrant”.

4. Run the VM with your application inside

In your project’s directory execute:

vagrant up

You will see the booting logs appear in your teminal, it takes just a few seconds.

5. Connect with SSH

You can SSH into your virtual machine by issuing the command below inside your project’s directory:

vagrant ssh

Tips! As you can see in this image I usually keep two terminal windows opened in my IDE (this is a screenshot of my PHPStorm):

  • Vagrant” always in SSH with the project’s VM.
  • Local” that simply point my local terminal to the project’s directory (as by default in PHPStorm).

This is important because now your database isn’t reachable outside of your VM. So for example you need to use the “Local” terminal to create a migration script, but you need to go in the “Vagrant” terminal to run the migration.


Lavarel/Homestead Github

Take a time to review the homestead documentation especially “Installing Optional Features” to check what other softwares you can install in your server to reproduce your production environment faithfully.

If you are intereted in code details about Homestead and Laravel integration check the official repository on Github: https://github.com/laravel/homestead

Conclusion

Homestead provides the perfect tool to start developing with Laravel. It lets you focus on the code and not the server, but at the same time, it encouraged me into getting more familiar with the server environment.

Thank you so much for reading it, I hope it helps people get started.

If you want know more about my product come on our website https://www.inspector.dev . Don’t hesitate to share your thoughts on the comment below or drop in live chat on our website!

Related Posts

NodeJs Real-Time Execution Monitoring with Inspector

Have you ever desired to watch your code running, instead of just imagining it? Hi, I’m Valerio software engineer from Italy and CTO at Inspector. As product owner I learned on my skin how an application issue can be so hard to fix, creating a negative impact on the users experience, or block new potential

How to accelerate application performance with smart SQL queries.

Why so many performance issues are caused by the database? We often forget that each request is not independent of other requests. If one request is slow, it’s unlikely to affect the others… right? Database is a shared resource used by all processes that runs in your application. Even just one poorly designed access can

Application monitoring tools – Why, when, what!

In this article I reported my experience working on large applications, and how monitoring cames in my journey when I started producing software designed to solve business critical problems. Hi, I’m Valerio, software engineer from Italy and CTO at Inspector. Solve customers critical problems can generate great business opportunities, but in these situations you need

Stop losing customers and money
due to technical problems in your applications

Monitor performance of your code execution in real-time and identify bugs and bottlenecks before your users do.
Real-Time Application Monitoring Platform.
2020 © Inspector S.R.L - VAT: 09552901218