How to Install Laravel Homestead for Local Development?

The Laravel Homestead Vagrant box offers a preconfigured environment for building Laravel-based web apps. Homestead allows you to easily create a local development environment consistent with the environment in which your Laravel application will operate in production. In this tutorial, we will show you how to install Laravel Homestead on your local machine for use in development.

Step 1: Install Required Software

VirtualBox and Vagrant-Local Development

Some software has to be set up before we can begin setting up Laravel Homestead. Our first step is to set up VirtualBox, a free and open-source virtualization program enabling us to run many virtual computers on a single host computer. VirtualBox will be used to power our Homestead VM. VirtualBox is available for download at this URL.

The next step is to set up Vagrant, a command-line tool for creating and maintaining virtual machine environments. Our Homestead virtual machine will be set up and managed with the help of Vagrant. Vagrant is available for download at this URL.

When you’ve set up VirtualBox and Vagrant, you’re ready to go on to the next phase.

Recommended for you: 5 Good Reasons to Pick Laravel for a Web Development Project.

Step 2: Install Homestead

Install Laravel Homestead

First, we’ll need to clone the Homestead source from GitHub to begin the installation process. Launch your terminal or command prompt and enter:

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

The Homestead repository will be cloned into your system’s ‘~/Homestead’ folder. The ‘Homestead.yaml’ configuration file must be created by navigating to the ‘~/Homestead’ directory and executing the ‘bash init.sh’ command:

cd ~/Homestead
bash init.sh

In the current working directory ‘~/Homestead’, this action will generate the ‘Homestead.yaml’ configuration file.

Step 3: Configure Homestead

Configure Laravel Homestead

After we have the ‘Homestead.yaml’ file, we may tailor it to our specifications. Go out of your text editor and load up the ‘Homestead.yaml’ file. A setup example like this will appear:

ip: "192.168.10.10"
memory: 2048
cpus: 1
provider: VirtualBox
authorize: ~/.ssh/id_rsa.pub
keys:
    - ~/.ssh/id_rsa
folders:
    - map: ~/code
      to: /home/vagrant/code
sites:
    - map: Homestead.test
      to: /home/vagrant/code/Laravel/public
databases:
    - Homestead
Let’s go over all of the settings that may be adjusted:
  • ‘ip’: Our Homestead virtual machine will use this IP address in the future. If the present IP address is in use on your network, you may change it to any other available IP address.
  • ‘memory’: We’ll allocate this much RAM (in MB) to our Homestead VM. Depending on your machine’s hardware, you can tweak this setting.
  • ‘cpus’: The number of processors made available to our Homestead VM. Depending on your machine’s capabilities, you may tweak this setting.
  • ‘provider’: This will host our Homestead virtual machine in the virtualization environment. The provider we set up in Step 1 (VirtualBox) is the default.
  • ‘authorize’: The location of the SSH public key that will be used for authentication with the Homestead VM is shown below.
  • ‘keys’: Locate the SSH private key file used for authentication with the Homestead VM and copy the contents into the appropriate location.
  • ‘folders’: The following is a directory tree of the Homestead virtual machine’s shared folders with our local system. The’map’ option designates the local folder’s location, while the ‘to’ choice sets the folder’s location on the Homestead VM.
  • ‘sites’: Our Homestead virtual machine will host the following websites. Using the ‘map’ option, we tell Homestead the domain name we want to use to access the site locally, and with the ‘to’ option, we tell Homestead the path to the site’s public directory on our virtual machine.
  • ‘databases’: The following is a directory containing the Homestead database schemas that will be implemented.

You may adjust the setup settings to suit your needs. The RAM of the virtual machine may be expanded, for instance, or new sites or databases can be added.

Step 4: Start Homestead

Start Laravel Homestead

After setting up Homestead, we can start the VM by issuing the following command in the ‘~/Homestead’ folder:

vagrant up

The ‘Homestead.yaml’ file contains the configuration details for the virtual machine, which will be started and provisioned when you issue this command. Depending on the speed of your local system and the resources available to the virtual machine, this might take a few minutes. After the virtual machine is ready, you may log into it using the command:

vagrant ssh

The Homestead virtual machine may be accessed using an SSH connection with this command. You may use this terminal to manage your Laravel app and perform commands.

Step 5: Access Your Laravel Application

Access Your Laravel Application

To access your Laravel application locally for Laravel Homestead for local development, you must create an entry in your host file that points to the Homestead virtual machine’s IP address (as given in the ‘Homestead.yaml’ file).

With your preferred text editor, open the host file. This file can be found at /etc/hosts and runs on Linux and macOS. It may be found under C:\Windows\System32\drivers\etc\hosts on Windows.

The file should have the following line added at the end:

192.168.10.10 homestead.test

Change ‘homestead.test’ using the ‘Homestead.yaml’ file’s domain name specification. After saving the file, close your text editor. Now that everything is set up, you should be able to visit your Laravel application by going to ‘http://homestead.test’ in your web browser.

You may like: 10 Reasons to Opt No-Code Development Approach for Mobile App Development.

Step 6: Develop Your Laravel Application

Develop Your Laravel Application

You may now build your Laravel application on Homestead precisely as you would on a production server after it has been installed and operational. You may access your program in your web browser and execute commands on the terminal while editing files with your preferred text editor.

Your application will immediately update when you change the code since it runs on the Homestead virtual machine. Further pointers and advice for using Laravel Homestead are provided below:

Database Management

A MySQL server is created when Homestead is set up, and you can use it to build and manage databases for your Laravel application. To administer your databases, use a program like phpMyAdmin. Go to ‘http://homestead.test/phpmyadmin’ in your web browser to view phpMyAdmin. Log in using the username and password in the ‘Homestead.yaml’ file.

Using Multiple Sites

Adding more entries to the site’s portion of the Homestead.yaml file may set up Homestead to serve several areas. Each location on the Homestead virtual machine should have its separate domain name and accompanying directory. The following item would be added to the sites section of the ‘Homestead. yaml’ file to create a second site with the domain name ‘example.com’, for instance:

- map: example.com
  to: /home/vagrant/sites/example/public

On the Homestead virtual machine, create the directory ‘/home/vagrant/sites/example’ and transfer your Laravel application files to the ‘public’ directory. You must execute the ‘vagrant reload —provision’ command after changing the ‘Homestead.yaml’ file for the changes to take effect.

Xdebug Configuration

It is possible to debug PHP code with the Xdebug PHP extension. While it is already pre-installed on Homestead, Xdebug is not, by default, turned on. Add the following lines to the ‘Homestead.yaml’ file to enable Xdebug:

sites:
    - map: Homestead.test
      to: /home/vagrant/code/laravel/public
      xdebug: true

Run the ‘vagrant reload —provision’ command after changing the ‘Homestead.yaml’ file to take effect. You may debug your Laravel application with a PHP debugger like Visual Studio Code when Xdebug has been enabled.

Sharing Files with Your Local Machine

Your local computer and the Homestead virtual machine’s files may be automatically synchronized by using the ‘vagrant rsync-auto’ command. This can be helpful if you want to use a text editor or IDE to edit files on your local computer while running the program on the Homestead virtual machine. Go to the the’~/Homestead’ directory and issue the following command to utilize ‘vagrant rsync-auto’:

vagrant rsync-auto

After doing this, a background operation will automatically synchronize modified files between your local computer and the Homestead virtual machine.

Customizing the Virtual Machine Configuration

By altering the ‘Vagrantfile’ file located in the ‘~/Homestead’ directory, you may change how the Homestead virtual machine is configured. By including the following line in the ‘Vagrantfile’, for instance, you may increase the amount of RAM allotted to the virtual machine:

config.vm.provider "virtualbox" do |vb|
  vb.memory = "2048"
End

You must use the ‘vagrant reload —provision’ command to put the changes into effect after modifying the ‘Vagrantfile’.

You may also like: Know if is Laravel Reliable for eCommerce Development.

Conclusion

conclusion

Setting up Laravel Homestead for local development has been taken you through. Following these steps, you may rapidly create a finished, ready-to-use development environment for your Laravel apps. You can create and test your applications in a setting that resembles the one used for production with Homestead, making it simpler to identify and address problems before they occur in production. For the local development of Laravel apps, Laravel Homestead is a potent tool.

You may quickly build up a comprehensive development environment that closely resembles the production environment by following the procedures described in this article. You can confidently create and test your apps with Homestead because you know they will function as intended after they are used.

Author: Atman Rathod

This article is written by Atman Rathod. Atman is the Co-founder at CMARIX TechnoLabs Pvt. Ltd., a leading web and mobile app development company with 14+ years of experience. He loves to write about technology, startups, entrepreneurship, and business. His creative abilities, academic track record, and leadership skills made him one of the key industry influencers as well.

Disclosure: Some of our articles may contain affiliate links; this means each time you make a purchase, we get a small commission. However, the input we produce is reliable; we always handpick and review all information before publishing it on our website. We can ensure you will always get genuine as well as valuable knowledge and resources.
Share the Love

Related Articles

Published By: Souvik Banerjee

Souvik BanerjeeWeb Developer & SEO Specialist with 15+ years of experience in Open Source Web Development specialized in Joomla & WordPress development. He is also the moderator of this blog "RS Web Solutions".