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 know the details of both Laravel Homestead and Vagrant, let’s jump to our detailed Laravel Homestead Tutorial to implement 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 Install 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. We have chosen a folder named Laravel inside the 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 Laravel Homestead 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.
  • Benefits of Using Laravel Homestead for Local Development

    Laravel Homestead comes with its set of benefits for local development. This significantly helps improve your Laravel development workflow; here are a few key factors you can consider.

    Consistency

    Homestead Laravel provides a standard and consistent development environment for all team members, reducing compatibility issues and ensuring everyone works with identical software versions and configurations.

    Isolation

    By running within a virtual machine (VM), Homestead Laravel isolates your development environment from your host operating system. This prevents conflicts with other software or dependencies on your local machine, allowing you to work on your Laravel project without interference.

    Pre-Configured Stack

    Homestead includes a complete and optimized LAMP (Linux, Apache, MySQL, PHP) stack, Composer, Node.js, and other development tools. This pre-configured environment is ready to use, saving time and effort when setting up your development stack.

    Version Control

    You can track changes to your Homestead configuration using a version control system like Git. This practice helps manage and maintain your configuration files alongside your Laravel project’s source code.

    Customization

    While Homestead offers a default configuration, it is highly customizable. You can modify the “homestead.yaml” file to match your project’s requirements, including web servers, PHP versions, and database servers.

    Multi-Site Support

    Laravel Homestead tutorial guides and enables you to configure and manage multiple sites within a single instance. Each site can have its domain name, database, and configuration settings, which is helpful for developers working on multiple projects or agencies managing multiple client websites. This feature enables separate development environments for each project without needing additional virtual machines.

    Points To Consider When Implementing The Laravel Homestead For Local Development

    When setting up Laravel Homestead for local development, consider these best practices to ensure a smooth and efficient workflow:

    Customize Homestead.yaml File

    Customize the Homestead.yaml file to your project needs by defining sites, databases, folders, and additional services required for your Laravel application.

    Configure Version Control

    Include your Homestead.yaml file in your version control system, such as Git. This practice ensures consistency across team members’ development environments and simplifies sharing configurations.

    SSH Key Management

    Consider generating an SSH or Secure Shell key paired specifically for your Laravel Homestead virtual machine. The key can secure authentication when connecting to the virtual machine via SSH. Be sure to properly configure the SSH keys within your host and ensure the virtual machines have secure access.

    Generate a dedicated SSH or Secure Shell key paired precisely with your Laravel Homestead virtual machine to secure authentication. Use the key to secure authentication when connecting to the virtual machine via SSH. Ensure you correctly configure the SSH keys within your host and that the virtual machines have secure access.

    Provisioning Scripts

    Create custom provisioning scripts if your Laravel app requires additional setup beyond Homestead’s default provisions. These scripts can automate tasks like installing software, configuring server settings, or seeding databases.

    Monitoring and Maintenance

    Update Laravel Homestead, VirtualBox, Vagrant, and other dependencies to the latest versions regularly to benefit from security patches and improvements. Monitor VM performance and allocate more resources as your project scales.

    Following these practices ensures your Laravel development environment is ideally configured, secure, and optimized.

    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.

    Frequently Asked Questions (FAQs)

    Laravel Valet is a development environment for macOS that allows you to easily create and test Laravel applications. Laravel Homestead is a virtualized development environment that works on macOS, Windows, and Linux. It offers a full LAMP stack inside a virtual machine. Homestead is a great choice for complex projects where you need a consistent setup for team collaboration.

    To connect your Laravel application to the database in your Homestead environment, start by configuring the database settings. Open the config/database.php file in your Laravel project and ensure the settings (database name, username, password, host) match those in Homestead.yaml. Homestead supports both MySQL and PostgreSQL. Connect to MySQL at 127.0.0.1 on port 33060 or PostgreSQL at 127.0.0.1 on port 54320, using homestead as the username and secret as the password. Use Laravel’s database features to interact with your database, or for direct access, use phpMyAdmin at http://homestead.test/phpmyadmin with your database credentials.

    To add multiple sites to your Laravel Homestead setup, you need to update the ‘Homestead.yaml’ file. Here’s a simple example:

    sites:
    - map: homestead.test
    to: /home/vagrant/project1/public
    - map: another.test
    to: /home/vagrant/project2/public

    In this example, we’ve added two sites: homestead.test and another.test. Each site is linked to its respective Laravel project’s public directory. Homestead will automatically configure Nginx to serve these sites. You can add more sites as needed.

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?