I started learning Laravel and I thought of writing a small tutorial on how to setup your own local development environment for Laravel projects with Laravel Homestead and Vagrant.

What is Laravel Homestead?

It's an official package from Laravel that provides a neat development environment that comes along with a Vagrant box that has pre-installed software. Read their documentation to know more about it.

Note: The examples of this tutorial are applicable to any developer that uses any UNIX-based operating systems such as macOS or any Linux distributions like Ubuntu. If you're using Windows, I recommend that you install Git for Windows on your local machine.

Prerequisites

The only requirement for this tutorial is that you should be familiar with using the command-line interface.

Install VirtualBox and Vagrant

VirtualBox is the software used to run a virtual machine with a sandbox operating system within your own computer.

Vagrant is a software that's used to manage a development environment. Through the command-line, you can perform any operation such as installing an OS, configuration, deployment and so on.

You can install VirtualBox and Vagrant via the command-line:

sudo apt install virtualbox vagrant

Once you're done installing them, you should add the laravel/homestead box to your Vagrant installation using the following command:

vagrant box add laravel/homestead

To check if it has been installed, use the following command:

vagrant box list | grep "homestead"

Using VirtualBox and Vagrant allows you to simulate your Laravel development environment without messing up any configurations on your hosting system. Even if you did, no worries, Vagrant boxes are disposable, so you can destroy and create a new box again in minutes.

Download Laravel Homestead

You can install Homestead by cloning it's repository onto your local machine by typing the following command:

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

Generate configuration file

Once you're done cloning the repository, go inside the projects/Homestead directory and run the init.sh or init.bat (for Windows) to create the Homestead.yaml configuration file:

cd projects/Homestead
bash init.sh

Open the default Homestead.yaml file and make the following changes:

---
ip: "192.168.10.10"
memory: 2048
cpus: 2
provider: virtualbox

authorize: ~/.ssh/id_rsa.pub

keys:
    - ~/.ssh/id_rsa

folders:
    - map: ~/projects/code
      to: /home/vagrant/projects/code

sites:
    - map: homestead.test
      to: /home/vagrant/projects/code/public

databases:
    - homestead

Generate SSH key

The documentation for Laravel Homestead doesn't really talk about generating SSH keys used to access the Vagrant box. Use the following command to generate it using ssh-keygen on the command-line:

ssh-keygen -t rsa -C "root@homestead"

Map the project's shared folder

Make sure that you have created a folder named code in your projects directory. This folder will keep all of your Laravel project files in sync between your local machine and Homestead environment.

Installing Laravel

Time to install Laravel into your virtual machine. So, get switch on your virtual machine by doing the following in the command-line:

vagrant up

As you're switching it on for the first time, depending on your internet connection, it might take somewhere around 10-20 minutes to getting it running but it'll be fine afterwards.

Alright, login to your Vagrant machine by doing the following the command-line:

vagrant ssh

Once you're in, go to your code directory and type the following in the command-line:

composer global require laravel/installer

Create a new project

Now, it's time to create a new Laravel project by simply typing the following command:

laravel new projectname

It'll take some time to generate the necessary files and boilerplate code and after that, you're good to go!

Oh, you should be able to see the Laravel project files in your local machine as it's synced with the virtual machine and local machine. That means, any changes made on either machines will be synced and reflected on both ends.

Map your hosts file

One last step, make sure you map your project's test domain onto your local machine's hosts file.

Open a text-editor of your choice and do the following in the etc/hosts file:

127.0.0.1   localhost
::1         localhost

# Add this line (Note: It can be any URL)
127.0.0.1   homestead.test

Now, go to your browser and type homestead.test and you should be able to see your Laravel project's sample layout.

Conclusion

As mentioned, in the beginning of this article, I have started to learn Laravel and I'll be building some projects and writing out my experiences about it soon.

I hope you liked reading this article and please share it with others if you do find this tutorial useful.

Stay tuned for more!