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.
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.
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.
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.
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.
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.
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:
The output looks like:
This box can work with multiple providers! …
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.
You should see something like this:
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:
After successfully cloning the laravel files, go inside this folder.
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
For Laravel Homestead Windows…
After successful, you should see a response like this: Homestead initialized!
Now configure Homestead, navigate back one step in the terminal so that you are inside the Documents/laravel folder.
After that run the below command to create a folder named code. This folder has all your projects/coding files and folders.
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:
And then save the file.
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.
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.
and then to re-running the Homestead
Same way, below command also able to reprovision your server but I will suggest below one:
To suspend vagrant run below command
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.
Above command will bring up the command line which looks like below. In the ssh you can access your project folders and files.
Enter the command ls to check if our configuration was correct and you should see our newly created code directory listed there.
– code
To logout from the ssh write below command.
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
To download the Laravel installer using Composer run the below command:
To create fresh laravel project run the below command
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.
sites: - map: myproject1.test to: /home/vagrant/code/myproject1/public - map: myproject2.test // For another project2 to: /home/vagrant/code/myproject2/public
Databases:
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.
Save the file. Your project is configured now.
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
For Window
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.
## # 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.
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.
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:
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.
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.
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.
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.
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.
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.
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.
When setting up Laravel Homestead for local development, consider these best practices to ensure a smooth and efficient workflow:
Customize the Homestead.yaml file to your project needs by defining sites, databases, folders, and additional services required for your Laravel application.
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.
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.
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.
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.
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.
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.
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.