How I use Laravel Homestead everyday
Joe • June 24, 2015homestead laravel vagrant
How I use Homestead in my Laravel (and non-Laravel) Applications
I feel like I've been talking about homestead a lot lately. I feel like Vagrant is such an important part of a developer's workflow. If you are still using MAMP, WAMP, or installing Virtual Machines manually you are wasting so much of your own time (and your clients money) by not using prebuilt development environments.
Homestead is what Taylor Otwell created to make it ridiculously easy for Laravel users to develop in similar environments that Forge uses for deployments making the developer experience from Homestead to Forge very seamless.
The first version of Homestead (1.x) was rough around the edges compared to the easy to use version 2.x. I really enjoyed the 1.x version since it was easy to drop in a vagrant configuration per project. As it turns out this wasn't how Taylor and others used Homestead.
The 2.0 version introduced a global command to use that interacted with a global vagrant machine. This was quite easy to configure because the homestead executable did all of the vagrant commands for the user. You had one yaml configuration file to manage as many projects as you wanted to configure. This was a great solution to a lot of people. What about when it comes to easily sharing your development environment with contributors (or making it easier on potential new contributors)?
I prefer to have my open source projects contain a Vagrant environment so that any potential contributor can easily clone my repository and run "vagrant up". Collaborators running the same development environment as I am is just as important since so many potential environment differences can be mitigated easily. Also I can further configure the environment by adding to the "after.sh" shell script. I often use this to add tools such as phantomJS and screen for doing acceptance testing with Codeception.
Starting a new Project: (screenshot)
$ laravel new AmazingApplication Crafting application... Generating optimized class loader Application key [7rEysXrLXcM4zzo5sN88SwuR8rUTxHa3] set successfully. Application ready! Build something amazing.
Adding Homestead as a dependency: (screenshot)
$ composer require -- dev laravel/homestead Using version ~2.1 for laravel/homestead ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) - Installing laravel/homestead (v2.1.2) Loading from cache Writing lock file Generating autoload files Generating optimized class loader composer require laravel/homestead: 00:58
Make the Homestead configuration for this project: (screenshot)
$ php vendor/bin/homestead make -- after -- aliases Homestead Installed!
Now we can run vagrant up: (screenshot)
$ vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Importing base box 'laravel/homestead'... ==> default: Matching MAC address for NAT networking... ==> default: Checking if box 'laravel/homestead' is up to date... ==> default: Setting the name of the VM: amazingapplication ==> default: Clearing any previously set network interfaces... ==> default: Preparing network interfaces based on configuration... default: Adapter 1: nat default: Adapter 2: hostonly ==> default: Forwarding ports... default: 80 => 8000 (adapter 1) default: 443 => 44300 (adapter 1) default: 3306 => 33060 (adapter 1) default: 5432 => 54320 (adapter 1) default: 22 => 2222 (adapter 1) ... Lots of scrolly text here ... ==> default: php5-fpm stop/waiting ==> default: php5-fpm start/running, process 2231 ==> default: Running provisioner: shell... default: Running: inline script ==> default: Updating to version c43a39f7334ae3df968cd36a6eff0436bea0da75. ==> default: Downloading: Connecting... ==> default: ==> default: Downloading: 100% ==> default: ==> default: ==> default: Use composer self-update -- rollback to return to version bdb6ecb29e9624fb767aa690d09c780249050fc6 vagrant up: 00:46
Now we should have a fully working vagrant machine. You can quickly check by going to http://localhost:8000 and you should see the default Laravel welcome screen. I always map a fake domain to my projects so if you add this line to your hosts file (/etc/hosts on *nix systems, C:\Windows\System32\Drivers\etc\hosts on Windows) you can use the http://homestead.app url to see your application.
What about non-Laravel applications?
Follow the same steps and then edit Homestead.yaml in the root of your project and adjust those settings as needed. if you need to do more customizations edit the after.sh script, it will be run the first time you run
vagrant up' and if something isn't working right, simplyvagrant destroy' and `vagrant up' again.
Sharing your Homestead environment
You should add these files to your version control:
aliases . You should not version control
Homestead.yaml (make sure you add it to .gitignore!) since the paths of the folder will not be the same on someone else's system. Instead you should instruct potential collaborators to run the make command for themselves, no existing files will be overwritten:
$ php vendor/bin/homestead make Homestead Installed!
What about existing Laravel applications?
Follow the same steps and you'll be up and running with your own Homestead in minutes!
Thanks for reading.
As Seen On
- The Workshop: Run Amazon Linux Locally - php[architect] Magazine July 2019
- The Workshop: The Road to 7.3 (Part 2) - php[architect] Magazine February 2019
- The Workshop: The Road to 7.3 (Part 1) - php[architect] Magazine January 2019
- The Workshop: Producing Packages (Part 3) - php[architect] Magazine December 2018
- The Workshop: Producing Packages (Part 2) - php[architect] Magazine November 2018
- All Posts