How to Install Laravel Using Homestead on Windows – The Easy Way
If you‘re a PHP developer looking to get started with the popular Laravel framework, using Homestead on Windows is a great option. Homestead is an official, pre-packaged Vagrant box that provides a fantastic development environment without requiring you to install PHP, a web server, and any other server software on your local machine.
In this comprehensive guide, we‘ll walk through the process of installing Homestead and setting up a new Laravel project on Windows, even if you‘re entirely new to Laravel. By the end, you‘ll have a full-featured development environment up and running!
Why Use Laravel and Homestead?
Laravel is one of the most popular PHP frameworks, and for good reason. It provides a powerful set of tools and conventions that can help you build applications faster and with less code. According to the 2021 JetBrains PHP Developer Survey, Laravel is used by over 50% of PHP developers, more than any other framework.
But getting started with Laravel can be tricky, especially if you‘re not familiar with setting up a PHP development environment. That‘s where Homestead comes in. As Laravel‘s official documentation states:
Laravel strives to make the entire PHP development experience delightful, including your local development environment. Vagrant provides a simple, elegant way to manage and provision Virtual Machines.
Laravel Homestead is an official, pre-packaged Vagrant box that provides you a wonderful development environment without requiring you to install PHP, a web server, and any other server software on your local machine. No more worrying about messing up your operating system! Vagrant boxes are completely disposable. If something goes wrong, you can destroy and re-create the box in minutes!
Using Homestead provides several key benefits:
- Consistency: Homestead offers a consistent environment across all systems, avoiding the "it works on my machine" problem.
- Simplicity: While Homestead has a few setup steps, it‘s still simpler than installing PHP, Nginx, MySQL, and other software directly on Windows.
- Parity: Homestead is built on Linux, using the same software you‘re likely to use in production, helping catch bugs early.
- Isolation: Keeping your development environment in a VM prevents cluttering your main Windows installation.
With those benefits in mind, let‘s dive into setting up Homestead!
Prerequisites
Before we install Homestead, make sure you have the following software installed on your Windows machine:
- Git: We‘ll use Git Bash as our command line. Git also installs SSH, which we‘ll use to access Homestead.
- VirtualBox: Homestead runs inside a VirtualBox virtual machine. Download and install the latest version for Windows.
- Vagrant: Vagrant is a tool for building and managing virtual machine environments. Download and install the latest version.
- Composer: Composer is a dependency manager for PHP. We‘ll use it to create our Laravel project.
Once you have those installed, you‘re ready to set up Homestead!
Adding the Laravel Homestead Box
Open your Git Bash terminal and run the following command to add the Laravel Homestead box to your Vagrant installation:
vagrant box add laravel/homestead
You‘ll see output like this:
==> box: Loading metadata for box ‘laravel/homestead‘
box: URL: https://vagrantcloud.com/laravel/homestead
==> box: Adding box ‘laravel/homestead‘ (v12.0.0) for provider: virtualbox
box: Downloading: https://vagrantcloud.com/laravel/boxes/homestead/versions/12.0.0/providers/virtualbox.box
box: Download redirected to host: vagrantcloud-files-production.s3.amazonaws.com
This will take a few minutes as the Homestead box is several gigabytes. Once it finishes, you‘ll have the Homestead box installed and ready to use with Vagrant.
Installing Homestead
Next, we‘ll clone the Homestead configuration repository. In your Git Bash terminal, run:
cd ~
git clone https://github.com/laravel/homestead.git Homestead
cd Homestead
This clones the Homestead repository into a Homestead
directory in your user‘s home directory.
Before we initialize Homestead, we need to generate an SSH key pair. If you don‘t already have one, run the following command, replacing the email address with your own:
ssh-keygen -t rsa -C "[email protected]"
Press enter to accept the default file location. You can leave the passphrase empty for convenience. This will create an SSH key pair at ~/.ssh/id_rsa
and ~/.ssh/id_rsa.pub
.
Now we can initialize Homestead with:
bash init.sh
This command creates the Homestead.yaml
configuration file which we‘ll use to configure our Homestead environment.
Configuring Homestead
Open the Homestead.yaml
file located at ~/Homestead/Homestead.yaml
in your favorite text editor. We‘ll make a few edits to configure our project.
First, configure the folders you want to sync between your local machine and the Vagrant environment. Find the folders
section:
folders:
- map: ~/code
to: /home/vagrant/code
Edit it to map your projects directory. For example, if your projects are in C:\Users\YourName\Projects
, change it to:
folders:
- map: C:\Users\YourName\Projects
to: /home/vagrant/code
Next, configure the sites Homestead will host. Find the sites
section:
sites:
- map: homestead.test
to: /home/vagrant/code/public
Change this to your Laravel project‘s directory mapping:
sites:
- map: myproject.test
to: /home/vagrant/code/myproject/public
Save the Homestead.yaml
file.
Now, we need to add our development domain to the Windows hosts file. Open Notepad as Administrator and open the file at C:\Windows\System32\drivers\etc\hosts
.
At the bottom of this file, add a line mapping your Vagrant machine‘s IP to the domain you configured in Homestead.yaml
:
192.168.10.10 myproject.test
Save the hosts file.
Starting Homestead
Navigate to your Homestead directory in Git Bash and run this command to start the Vagrant box and provision it:
vagrant up
This may take several minutes the first time as Vagrant needs to download the base image, boot it, and provision it. Subsequent startups will be much faster.
Once it‘s done, SSH into the running Vagrant box with:
vagrant ssh
You‘ll see a bash prompt for the Ubuntu virtual machine:
vagrant@homestead:~$
Navigate to your project directory:
cd code
Creating a New Laravel Project
Inside our Homestead environment, we can create a new Laravel project using Composer. Run this command, replacing myproject
with your desired project name:
composer create-project --prefer-dist laravel/laravel myproject
This will create a new directory myproject
with a fresh Laravel installation. It may take a few minutes to complete as Composer downloads Laravel and its dependencies.
Accessing the Project
Open your web browser and navigate to the domain you configured in Homestead.yaml
and your hosts file:
http://myproject.test
You should see the default Laravel homepage!
Congratulations, you now have a working Laravel development environment using Homestead on Windows!
Troubleshooting Common Issues
If you run into problems, consult this table of common issues and solutions:
Issue | Solution |
---|---|
Vagrant box doesn‘t start | Run vagrant reload --provision to reboot and re-provision the box |
Browser shows "No input file specified" | Check your Homestead.yaml and hosts file for typos in the paths and domains |
SSH authentication fails | Make sure you generated an SSH key pair and the files exist at ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub |
Composer fails with "proc_open(): CreateProcess failed" | Run vagrant reload --provision , then try the composer create-project command again |
If you‘re still stuck, the Laravel forums and StackOverflow are great resources.
Next Steps: Efficient Development with Homestead
With your Laravel Homestead environment up and running, you‘re ready to start building your application! Homestead provides a solid foundation, but there‘s more you can do to streamline your workflow.
One powerful technique is to use Vagrant‘s synced folders to edit your code in Windows while running it in the Linux VM. By keeping your code on your Windows machine, you can use your favorite Windows IDEs and tools. Homestead‘s default configuration already sets this up – any files you edit in C:\Users\YourName\Projects
will automatically sync to /home/vagrant/code
in your VM.
You can also automate common tasks using Composer scripts. For example, you might create a script to run your migrations and seeders:
{
"scripts": {
"post-root-package-install": [
"@php -r \"file_exists(‘.env‘) || copy(‘.env.example‘, ‘.env‘);\""
],
"post-create-project-cmd": [
"@php artisan key:generate --ansi"
],
"migrate-fresh": [
"@php artisan migrate:fresh --seed"
]
}
}
Then you can run your migrations and seeders with a single command:
composer migrate-fresh
As your application grows, you‘ll likely want to add other services like databases, caches, and queues. Homestead supports many of these out of the box – simply uncomment and configure the relevant sections in your Homestead.yaml
file.
Finally, Homestead is a great foundation for more advanced development practices like continuous integration and deployment. By scripting the provisioning of your Homestead environment, you can easily create identical environments for staging and production. Tools like Laravel Forge can even automatically deploy your code to Homestead-based servers whenever you push to your Git repository.
In my own experience, adopting Homestead was a turning point in my Laravel development career. It allowed me to spend less time worrying about environment inconsistencies and focus more on writing great code. I hope this guide has given you a solid foundation to do the same!
Learn More
We‘ve covered a lot, but there‘s always more to learn. Here are some additional resources to continue your Laravel and Homestead journey:
- Laravel Documentation – The official documentation is a great resource for learning more about Laravel‘s features and concepts.
- Laracasts – This subscription site offers high-quality video tutorials on Laravel, PHP, and related technologies.
- Laravel: Up & Running – This book by Matt Stauffer provides a comprehensive introduction to Laravel.
- Servers for Hackers – This newsletter and book series dives deep into server administration concepts that are valuable for any PHP developer.
Remember, the Laravel community is large and welcoming. If you get stuck or have questions, don‘t hesitate to reach out on the forums or StackOverflow. Happy coding!