Extending Homestead: Building A Laravel Dev Environment on Mac OS X

By · Published · php, mac, macos, laravel, dns, howto

I'm a big fan of Laravel and I've really enjoyed building things on it. So I'm going to walk you through a quick lesson in getting a dev environment set up.

Setting up Local DNS

A good first place to start is setting up local DNS. In my case, I use the *.dev TLD for my local projects. Rather than reproduce all the steps, I will instead point you at this very good guide on using DNSMasq on OS X.

The one thing to note is that, when it mentions pointing your domain to, you instead need to point it to This is the local address for the Homestead virtual machine.


Setting Up Homestead


One of the neat things about Laravel is Homestead, a virtual machine ready to go with all you need to get you up and going as quickly as possible. But, one of the things I don't like about it is that you either have to have multiple virtual machines for each laravel project (which wastes space) or manually enter each one. So you end up with something like this.

    - map: homestead.app
      to: /home/vagrant/Development/homestead/public
    - map: laravel.app
      to: /home/vagrant/Development/laravel/public
    - map: llama.app
      to: /home/vagrant/Development/llama/public

This repetition is unnecessary. If you follow the same format and put all your Laravel projects in the same location, you can do something like this instead:

    - map: '~^(?<project>.+)\.dev$'
      to: /home/vagrant/Development/\$project/public

Now, going to llama.dev will direct you do the code in /home/vagrant/Development/llama/public. Easy!


But what about databases? Well there's a solution for this too. In your homestead directory, edit the after.sh file like this:


for file in /home/vagrant/Development/*/.env; do
    . $file;
    DBEXISTS=$(mysql -u root -p{password} --batch --skip-column-names -e "SHOW DATABASES LIKE '"$DB_DATABASE"';" | grep "$DB_DATABASE" > /dev/null; echo "$?")
    if [[ ! $DBEXISTS -eq 0 ]]; then
        mysqladmin -u root -p{password} create $DB_DATABASE;
        mysql -u root -p{password} -e "grant all privileges on $DB_DATABASE.* to [email protected]'localhost' identified by '$DB_PASSWORD';";

This little script will read the .env file in each of your Laravel directories and, if the database doesn't yet exist, it will create it. So any time you need to create a new project, just re-run vagrant provision.


Now, starting a new Laravel project is as easy as issuing just a few commands:

laravel new blog

Edit the .env as needed.

cd ~/Homestead;
vagrant provision

And you're ready to start coding.

( Comments )

Did something I wrote help you out?

That's great! I don't earn any money from this site - I run no ads, sell no products and participate in no affiliate programs. I do this solely because it's fun; I enjoy writing and sharing what I learn.

All the same, if you found this article helpful and want to show your appreciation, here's my Amazon.com wishlist.

Related Posts

Merging M4V files on a Mac ... with chapters!

Monitoring for Filesystem Changes using PHP and Laravel

comments powered by Disqus