Quick Summary:

Laravel Homestead has taken up the web application development market by the storm. Whether it is its efficiency, efficacy or implementation ability in every aspect it takes on the full sweep for developers. Therefore, in this blog post we have covered the all-round aspects of Laravel Homestead including the overview of the topic and Vagrant Box along with a step-by-step guide to use it for your next project.

Table of Contents

What is Laravel Homestead?

In simple words, Laravel Homestead is a pre-packaged Vagrant box that enables a development environment for Laravel projects eliminating the needs for installing PHP, a web server, and any other server software to your local machine. It is precisely built to offer consistent and portable development environment that are easy to set up and can be used across different operating systems.

Homestead comes with inbuilt pre-installed tools and packages including PHP, Nginx, MySQL, Redis, and more that are commonly beneficial for Laravel development. Also, it includes config files making it easy to set up the virtual hosts and databases and develop amazing laravel applications.

Using Homestead requires Vagrant and VirtualBox in your system. After the installation process is complete you can download and configure the Homestead box based on the instructions provided in the official documentation.

What is Vagrant Box?

Vagrant Laravel or Vagrant Box can be referred to a pre-built, pre-configured, lightweight and portable virtual machine that allows creating and managing consistent development environments across multiple machines and OS. Vagrant boxes include pre-installed software, configurations, and dependencies that are commonly used in development projects, allowing developers to work in a standardized and reproducible environment. It is managed via a tool (Vagrant) that automates the setup and configuration of the virtual machines.

The Step-by-Step Tutorial to Implement Laravel Homestead for Local Development

Now, that we are aware of the details of both Laravel Homestead and Vagrant, let us jump to our detailed guide on implementing Homestead Laravel for Local Development.

Laravel Homestead vagrant

Step: 1 Download and Install Vagrant

The very first step to install Vagrant is installing the Vagrant command line utility to manage the virtual machine lifecycle. Then, isolate the dependencies and their configuration within a single disposable and consistent environment. When using Vagrant you can run various commands and access the code directory along with the MySQL database. Then, for installing and downloading Vagrant to your systems refer Vagrant Download Page.

Step: 2 Download And Installation of VirtualBox

In the next step you are required to install the VirtualBox as it is a mandate for running Vagrant into your systems. Talking about VirtualBox it is cross-platform virtualization software that allows users to extend their existing computers to run multiple operating systems. For installing and downloading VirtualBox refer to the VirtualBox Download Page.

Are you tired of manual development environment setup and inconsistencies across machines?
Look no further. Our team of experienced Laravel developers are ready to turn your ideas into reality. Hire Laravel developer today and take the first step towards creating your dream web application.

Step: 3 Add Homestead to VagrantBox

After that we need to add Homestead to VagrantBox so we can run the Homestead in the Virtualbox. Open terminal and run the following command:

Copy Text
# vagrant box add laravel/homestead

The output looks like:
This box can work with multiple providers! …

Copy Text
hyperv
parallels
Virtualbox
vmware_desktop

We are using VirtualBox, so enter Option-3. This will take some time to add. Sometimes it may also take a long time.
After that run the below command to check if it has been successfully added to our box.

Copy Text
# vagrant box list

You should see something like this:

Copy Text
laravel/homestead (virtualbox, 9.4.0)
Installation of VirtualBox

Step: 4 Installation of Homestead To Specific Location

Start installing Homestead. Now we need to download the laravel homestead files into our system. Choose a directory where you would like to keep it and modify the path in the code accordingly. For me, I have chosen a folder named laravel inside my Documents folder. So run the below command

Copy Text
# git clone https://github.com/laravel/homestead.git 
~/Documents/laravel/Homestead

After successfully cloning the laravel files, go inside this folder.

Copy Text
# cd Documents/laravel/Homestead

Run below command to initialize Homestead that will create some files that are necessary for the Homestead. For Mac/Linux run the below command
For Mac, Ubuntu

Copy Text
# bash init.sh

For Windows…

Copy Text
# init.bat

After successful, you should see a response like this: Homestead initialized!

Step: 5 Configure Homestead

Now configure Homestead, navigate back one step in the terminal so that you are inside the Documents/laravel folder.

Copy Text
# cd ..

After that run the below command to create a folder named code. This folder has all your projects/coding files and folders.

Copy Text
# mkdir code

Now after completing the all steps you should have this folder structure
> Documents
> laravel
> code
> Homestead
Go back to your text editor where you opened the Homestead.yaml file. Homestead.yml file holds the directory path and hosts names for your projects. Locate the folders path and modify it to map to the folder that you just created.

Folders:

Copy Text
map: ~/Documents/laravel/code
to: /home/vagrant/code

And then save the file.

Step: 6 Start Homestead Machine To Run The Site

To start the Homestead machine goto terminal and run the below command so this will up the vagrant machine and running. If you are getting any errors then restart the system and try again.

Copy Text
# vagrant up

If you make any changes to the Homestead.yaml file, you must restart the server by calling the below commands otherwise your changes will not reflect and will not work.

Copy Text
# vagrant halt

and then to re-running the Homestead

Copy Text
# vagrant up

Same way, below command also able to reprovision your server but I will suggest below one:

Copy Text
# vagrant provision or # vagrant up --provision

To suspend vagrant run below command

Copy Text
# vagrant suspend

Connect With SSH
Now run this command to ssh into the machine. By running the below command you can login into the machine. After login to the machine you can go inside your projects.

Copy Text
# vagrant ssh

Above command will bring up the command line which looks like below. In the ssh you can access your project folders and files.

Copy Text
# vagrant@homestead:~$

Enter the command ls to check if our configuration was correct and you should see our newly created code directory listed there.

Copy Text
# vagrant@homestead:~$ ls

– code
To logout from the ssh write below command.

Copy Text
# exit

Step: 7 Installation of Laravel Installer

Install Laravel installer in Homestead so you can create multiple Laravel projects. Make sure you are still logged in to the vagrant machine. Navigate into the code directory by running below command

Copy Text
# cd code

To download the Laravel installer using Composer run the below command:

Copy Text
# composer global require laravel/installer

To create fresh laravel project run the below command

Copy Text
# laravel new myproject1

Step: 8 Configure Newly Created Laravel Project

Now we have to configure the newly created laravel project in our Homestead.yaml file. First of all, add the name of the new project-website or you can say specific url (domain), url can be anything whatever you want and also set the database to the respective places.

Copy Text
sites:
- map: myproject1.test
to: /home/vagrant/code/myproject1/public
- map: myproject2.test

// For another project2
to: /home/vagrant/code/myproject2/public 

Databases:

Copy Text
myproject1
myproject2

Second thing, go to myproject1/.env file and set the following values to set the database name, default username and password used by homestead to make the connection with the database.

Copy Text
DB_DATABASE=myproject1
DB_USERNAME=homestead
DB_PASSWORD=secret

Save the file. Your project is configured now.

Step: 9 Map the Hosts File

Map the hosts file, The domain/url that we have set which is ‘myproject.test’. In order to be able to open this in our browser we need to add it to the system hosts file. Open a new window in your terminal, and type the following command.
For Mac and Ubuntu

Copy Text
# sudo nano /etc/hosts

For Window

Copy Text
# C:\Windows\System32\drivers\etc\hosts

This will open up the hosts file in your terminal in edit mode. It should look like the code below. At the end of the file, add a line with the local address and our project site domain/url that we have set.

Copy Text
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1   	localhost
255.255.255.255 broadcasthost
::1         	localhost
127.0.0.1   	myproject1.test //(This line is the one that we added.)

Save and close the file. Ctrl+O -> Enter to save, and Ctrl+X to close.

Step: 10 Run Your Laravel Project

Open your browser and enter the website address/url, postfixed with :8000. Our project point out the port 8000 so it should look like this: http://myproject1.test:8000/. You should see a fresh Laravel website up.
Congratulations! Your fresh Laravel website is up and running.

Why use Laravel Homestead?

Laravel Homestead is a pre-packaged virtual machine that provides a development environment for Laravel web applications. There are a number of reasons why developers prefer Laravel Homestead, a few of them are given below:

  • Standardized Environment that ensures that all developers on a project are using the same configuration.
  • Isolated Development Environment meaning developers can work on multiple projects without worrying about conflicts between dependencies.
  • Easy to Set Up as it comes with everything needed to run a Laravel application out-of-the-box. Therefore, you don’t have to worry about configuring their environment, installing packages, or setting up virtual hosts.
  • Consistent Development Workflow states that developers can build, test, and deploy their apps in a consistent way, which helps to streamline the development process.
  • Compatibility is compatible with Windows, macOS, and Linux operating systems, making it accessible to a wide range of developers.
  • Conclusion

    So, this is the step-by-step guide on Laravel Homestead for Local Development and the other aspects revolving around it. With this guide we can infer that it can help developers save time and effort offering a pre-configured environment that is ready to use. It is also beneficial to improve the code quality ensuring developers are using the same configuration and development workflow. Therefore, If you are also a business owner and confused if Laravel Homestead is your ideal choice for your upcoming project and how it can benefit your business then Hire Laravel Development Company like Bacancy to help you at every step of your web application development project.

Ready to streamline your Laravel development process and boost your productivity?

Don’t let development setup slow you down. Take control of your workflow with Laravel Homestead and start building amazing web applications faster than ever. Try it out now and supercharge your development.

Connect now

Build Your Agile Team

Hire Skilled Developer From Us

[email protected]

Your Success Is Guaranteed !

We accelerate the release of digital product and guaranteed their success

We Use Slack, Jira & GitHub for Accurate Deployment and Effective Communication.

How Can We Help You?