Laravel Homestead

Introduction

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!

Homestead runs on any Windows, Mac, or Linux system, and includes the Nginx web server, PHP 7.2, PHP 7.1, PHP 7.0, PHP 5.6, MySQL, PostgreSQL, Redis, Memcached, Node, and all of the other goodies you need to develop amazing Laravel applications.

{note} If you are using Windows, you may need to enable hardware virtualization (VT-x). It can usually be enabled via your BIOS. If you are using Hyper-V on a UEFI system you may additionally need to disable Hyper-V in order to access VT-x.

Included Software

  • Ubuntu 16.04
  • Git
  • PHP 7.2
  • PHP 7.1
  • PHP 7.0
  • PHP 5.6
  • Nginx
  • Apache (Optional)
  • MySQL
  • MariaDB (Optional)
  • Sqlite3
  • PostgreSQL
  • Composer
  • Node (With Yarn, Bower, Grunt, and Gulp)
  • Redis
  • Memcached
  • Beanstalkd
  • Mailhog
  • Elasticsearch (Optional)
  • ngrok

Installation & Setup

First Steps

Before launching your Homestead environment, you must install VirtualBox 5.2, VMWare, Parallels or Hyper-V as well as Vagrant. All of these software packages provide easy-to-use visual installers for all popular operating systems.

To use the VMware provider, you will need to purchase both VMware Fusion / Workstation and the VMware Vagrant plug-in. Though it is not free, VMware can provide faster shared folder performance out of the box.

To use the Parallels provider, you will need to install Parallels Vagrant plug-in. It is free of charge.

Because of Vagrant limitations, The Hyper-V provider ignores all networking settings.

Installing The Homestead Vagrant Box

Once VirtualBox / VMware and Vagrant have been installed, you should add the laravel/homestead box to your Vagrant installation using the following command in your terminal. It will take a few minutes to download the box, depending on your Internet connection speed:

  1. vagrant box add laravel/homestead

If this command fails, make sure your Vagrant installation is up to date.

Installing Homestead

You may install Homestead by cloning the repository. Consider cloning the repository into a Homestead folder within your "home" directory, as the Homestead box will serve as the host to all of your Laravel projects:

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

You should check out a tagged version of Homestead since the master branch may not always be stable. You can find the latest stable version on the GitHub Release Page:

  1. cd ~/Homestead
  2. // Clone the desired release...
  3. git checkout v7.1.2

Once you have cloned the Homestead repository, run the bash init.sh command from the Homestead directory to create the Homestead.yaml configuration file. The Homestead.yaml file will be placed in the Homestead directory:

  1. // Mac / Linux...
  2. bash init.sh
  3. // Windows...
  4. init.bat

Configuring Homestead

Setting Your Provider

The provider key in your Homestead.yaml file indicates which Vagrant provider should be used: virtualbox, vmware_fusion, vmware_workstation, parallels or hyperv. You may set this to the provider you prefer:

  1. provider: virtualbox

Configuring Shared Folders

The folders property of the Homestead.yaml file lists all of the folders you wish to share with your Homestead environment. As files within these folders are changed, they will be kept in sync between your local machine and the Homestead environment. You may configure as many shared folders as necessary:

  1. folders:
  2. - map: ~/code
  3. to: /home/vagrant/code

If you are only creating a few sites, this generic mapping will work just fine. However, as the number of sites continue to grow, you may begin to experience performance problems. This problem can be painfully apparent on low-end machines or projects that contain a very large number of files. If you are experiencing this issue, try mapping every project to its own Vagrant folder:

  1. folders:
  2. - map: ~/code/project1
  3. to: /home/vagrant/code/project1
  4. - map: ~/code/project2
  5. to: /home/vagrant/code/project2

To enable NFS, you only need to add a simple flag to your synced folder configuration:

  1. folders:
  2. - map: ~/code
  3. to: /home/vagrant/code
  4. type: "nfs"

{note} When using NFS, you should consider installing the vagrant-bindfs plug-in. This plug-in will maintain the correct user / group permissions for files and directories within the Homestead box.

You may also pass any options supported by Vagrant's Synced Folders by listing them under the options key:

  1. folders:
  2. - map: ~/code
  3. to: /home/vagrant/code
  4. type: "rsync"
  5. options:
  6. rsync__args: ["--verbose", "--archive", "--delete", "-zz"]
  7. rsync__exclude: ["node_modules"]

Configuring Nginx Sites

Not familiar with Nginx? No problem. The sites property allows you to easily map a "domain" to a folder on your Homestead environment. A sample site configuration is included in the Homestead.yaml file. Again, you may add as many sites to your Homestead environment as necessary. Homestead can serve as a convenient, virtualized environment for every Laravel project you are working on:

  1. sites:
  2. - map: homestead.test
  3. to: /home/vagrant/code/Laravel/public

If you change the sites property after provisioning the Homestead box, you should re-run vagrant reload —provision to update the Nginx configuration on the virtual machine.

The Hosts File

You must add the "domains" for your Nginx sites to the hosts file on your machine. The hosts file will redirect requests for your Homestead sites into your Homestead machine. On Mac and Linux, this file is located at /etc/hosts. On Windows, it is located at C:\Windows\System32\drivers\etc\hosts. The lines you add to this file will look like the following:

  1. 192.168.10.10 homestead.test

Make sure the IP address listed is the one set in your Homestead.yaml file. Once you have added the domain to your hosts file and launched the Vagrant box you will be able to access the site via your web browser:

  1. http://homestead.test

Launching The Vagrant Box

Once you have edited the Homestead.yaml to your liking, run the vagrant up command from your Homestead directory. Vagrant will boot the virtual machine and automatically configure your shared folders and Nginx sites.

To destroy the machine, you may use the vagrant destroy —force command.

Per Project Installation

Instead of installing Homestead globally and sharing the same Homestead box across all of your projects, you may instead configure a Homestead instance for each project you manage. Installing Homestead per project may be beneficial if you wish to ship a Vagrantfile with your project, allowing others working on the project to vagrant up.

To install Homestead directly into your project, require it using Composer:

  1. composer require laravel/homestead --dev

Once Homestead has been installed, use the make command to generate the Vagrantfile and Homestead.yaml file in your project root. The make command will automatically configure the sites and folders directives in the Homestead.yaml file.

Mac / Linux:

  1. php vendor/bin/homestead make

Windows:

  1. vendor\\bin\\homestead make

Next, run the vagrant up command in your terminal and access your project at http://homestead.test in your browser. Remember, you will still need to add an /etc/hosts file entry for homestead.test or the domain of your choice.

Installing MariaDB

If you prefer to use MariaDB instead of MySQL, you may add the mariadb option to your Homestead.yaml file. This option will remove MySQL and install MariaDB. MariaDB serves as a drop-in replacement for MySQL so you should still use the mysql database driver in your application's database configuration:

  1. box: laravel/homestead
  2. ip: "192.168.10.10"
  3. memory: 2048
  4. cpus: 4
  5. provider: virtualbox
  6. mariadb: true

Installing Elasticsearch

To install Elasticsearch, add the elasticsearch option to your Homestead.yaml file and specify a supported version. The default installation will create a cluster named 'homestead'. You should never give Elasticsearch more than half of the operating system's memory, so make sure your Homestead machine has at least twice the Elasticsearch allocation:

  1. box: laravel/homestead
  2. ip: "192.168.10.10"
  3. memory: 4096
  4. cpus: 4
  5. provider: virtualbox
  6. elasticsearch: 6

{tip} Check out the Elasticsearch documentation to learn how to customize your configuration.

Aliases

You may add Bash aliases to your Homestead machine by modifying the aliases file within your Homestead directory:

  1. alias c='clear'
  2. alias ..='cd ..'

After you have updated the aliases file, you should re-provision the Homestead machine using the vagrant reload —provision command. This will ensure that your new aliases are available on the machine.

Daily Usage

Accessing Homestead Globally

Sometimes you may want to vagrant up your Homestead machine from anywhere on your filesystem. You can do this on Mac / Linux systems by adding a Bash function to your Bash profile. On Windows, you may accomplish this by adding a "batch" file to your PATH. These scripts will allow you to run any Vagrant command from anywhere on your system and will automatically point that command to your Homestead installation:

Mac / Linux

  1. function homestead() {
  2. ( cd ~/Homestead && vagrant $* )
  3. }

Make sure to tweak the ~/Homestead path in the function to the location of your actual Homestead installation. Once the function is installed, you may run commands like homestead up or homestead ssh from anywhere on your system.

Windows

Create a homestead.bat batch file anywhere on your machine with the following contents:

  1. @echo off
  2. set cwd=%cd%
  3. set homesteadVagrant=C:\Homestead
  4. cd /d %homesteadVagrant% && vagrant %*
  5. cd /d %cwd%
  6. set cwd=
  7. set homesteadVagrant=

Make sure to tweak the example C:\Homestead path in the script to the actual location of your Homestead installation. After creating the file, add the file location to your PATH. You may then run commands like homestead up or homestead ssh from anywhere on your system.

Connecting Via SSH

You can SSH into your virtual machine by issuing the vagrant ssh terminal command from your Homestead directory.

But, since you will probably need to SSH into your Homestead machine frequently, consider adding the "function" described above to your host machine to quickly SSH into the Homestead box.

Connecting To Databases

A homestead database is configured for both MySQL and PostgreSQL out of the box. For even more convenience, Laravel's .env file configures the framework to use this database out of the box.

To connect to your MySQL or PostgreSQL database from your host machine's database client, you should connect to 127.0.0.1 and port 33060 (MySQL) or 54320 (PostgreSQL). The username and password for both databases is homestead / secret.

{note} You should only use these non-standard ports when connecting to the databases from your host machine. You will use the default 3306 and 5432 ports in your Laravel database configuration file since Laravel is running within the virtual machine.

Adding Additional Sites

Once your Homestead environment is provisioned and running, you may want to add additional Nginx sites for your Laravel applications. You can run as many Laravel installations as you wish on a single Homestead environment. To add an additional site, add the site to your Homestead.yaml file:

  1. sites:
  2. - map: homestead.test
  3. to: /home/vagrant/code/Laravel/public
  4. - map: another.test
  5. to: /home/vagrant/code/another/public

If Vagrant is not automatically managing your "hosts" file, you may need to add the new site to that file as well:

  1. 192.168.10.10 homestead.test
  2. 192.168.10.10 another.test

Once the site has been added, run the vagrant reload —provision command from your Homestead directory.

Site Types

Homestead supports several types of sites which allow you to easily run projects that are not based on Laravel. For example, we may easily add a Symfony application to Homestead using the symfony2 site type:

  1. sites:
  2. - map: symfony2.test
  3. to: /home/vagrant/code/Symfony/web
  4. type: "symfony2"

The available site types are: apache, laravel (the default), proxy, silverstripe, statamic, symfony2, and symfony4.

Site Parameters

You may add additional Nginx fastcgi_param values to your site via the params site directive. For example, we'll add a FOO parameter with a value of BAR:

  1. sites:
  2. - map: homestead.test
  3. to: /home/vagrant/code/Laravel/public
  4. params:
  5. - key: FOO
  6. value: BAR

Environment Variables

You can set global environment variables by adding them to your Homestead.yaml file:

  1. variables:
  2. - key: APP_ENV
  3. value: local
  4. - key: FOO
  5. value: bar

After updating the Homestead.yaml, be sure to re-provision the machine by running vagrant reload —provision. This will update the PHP-FPM configuration for all of the installed PHP versions and also update the environment for the vagrant user.

Configuring Cron Schedules

Laravel provides a convenient way to schedule Cron jobs by scheduling a single schedule:run Artisan command to be run every minute. The schedule:run command will examine the job schedule defined in your App\Console\Kernel class to determine which jobs should be run.

If you would like the schedule:run command to be run for a Homestead site, you may set the schedule option to true when defining the site:

  1. sites:
  2. - map: homestead.test
  3. to: /home/vagrant/code/Laravel/public
  4. schedule: true

The Cron job for the site will be defined in the /etc/cron.d folder of the virtual machine.

Configuring Mailhog

Mailhog allows you to easily catch your outgoing email and examine it without actually sending the mail to its recipients. To get started, update your .env file to use the following mail settings:

  1. MAIL_DRIVER=smtp
  2. MAIL_HOST=localhost
  3. MAIL_PORT=1025
  4. MAIL_USERNAME=null
  5. MAIL_PASSWORD=null
  6. MAIL_ENCRYPTION=null

Ports

By default, the following ports are forwarded to your Homestead environment:

  • SSH: 2222 → Forwards To 22
  • ngrok UI: 4040 → Forwards To 4040
  • HTTP: 8000 → Forwards To 80
  • HTTPS: 44300 → Forwards To 443
  • MySQL: 33060 → Forwards To 3306
  • PostgreSQL: 54320 → Forwards To 5432
  • Mailhog: 8025 → Forwards To 8025

Forwarding Additional Ports

If you wish, you may forward additional ports to the Vagrant box, as well as specify their protocol:

  1. ports:
  2. - send: 50000
  3. to: 5000
  4. - send: 7777
  5. to: 777
  6. protocol: udp

Sharing Your Environment

Sometimes you may wish to share what you're currently working on with coworkers or a client. Vagrant has a built-in way to support this via vagrant share; however, this will not work if you have multiple sites configured in your Homestead.yaml file.

To solve this problem, Homestead includes its own share command. To get started, SSH into your Homestead machine via vagrant ssh and run share homestead.test. This will share the homestead.test site from your Homestead.yaml configuration file. Of course, you may substitute any of your other configured sites for homestead.test:

  1. share homestead.test

After running the command, you will see an Ngrok screen appear which contains the activity log and the publicly accessible URLs for the shared site. If you would like to specify a custom region, subdomain, or other Ngrok runtime option, you may add them to your share command:

  1. share homestead.test -region=eu -subdomain=laravel

{note} Remember, Vagrant is inherently insecure and you are exposing your virtual machine to the Internet when running the share command.

Multiple PHP Versions

{note} This feature is only compatible with Nginx.

Homestead 6 introduced support for multiple versions of PHP on the same virtual machine. You may specify which version of PHP to use for a given site within your Homestead.yaml file. The available PHP versions are: "5.6", "7.0", "7.1" and "7.2" (the default):

  1. sites:
  2. - map: homestead.test
  3. to: /home/vagrant/code/Laravel/public
  4. php: "5.6"

In addition, you may use any of the supported PHP versions via the CLI:

  1. php5.6 artisan list
  2. php7.0 artisan list
  3. php7.1 artisan list
  4. php7.2 artisan list

Web Servers

Homestead uses the Nginx web server by default. However, it can install Apache if apache is specified as a site type. While both web servers can be installed at the same time, they cannot both be running at the same time. The flip shell command is available to ease the process of switching between web servers. The flip command automatically determines which web server is running, shuts it off, and then starts the other server. To use this command, SSH into your Homestead machine and run the command in your terminal:

  1. flip

Network Interfaces

The networks property of the Homestead.yaml configures network interfaces for your Homestead environment. You may configure as many interfaces as necessary:

  1. networks:
  2. - type: "private_network"
  3. ip: "192.168.10.20"

To enable a bridged interface, configure a bridge setting and change the network type to public_network:

  1. networks:
  2. - type: "public_network"
  3. ip: "192.168.10.20"
  4. bridge: "en1: Wi-Fi (AirPort)"

To enable DHCP, just remove the ip option from your configuration:

  1. networks:
  2. - type: "public_network"
  3. bridge: "en1: Wi-Fi (AirPort)"

Updating Homestead

You can update Homestead in two simple steps. First, you should update the Vagrant box using the vagrant box update command:

  1. vagrant box update

Next, you need to update the Homestead source code. If you cloned the repository you can git pull origin master at the location you originally cloned the repository.

If you have installed Homestead via your project's composer.json file, you should ensure your composer.json file contains "laravel/homestead": "^7" and update your dependencies:

  1. composer update

Provider Specific Settings

VirtualBox

natdnshostresolver

By default, Homestead configures the natdnshostresolver setting to on. This allows Homestead to use your host operating system's DNS settings. If you would like to override this behavior, add the following lines to your Homestead.yaml file:

  1. provider: virtualbox
  2. natdnshostresolver: off

If symbolic links are not working properly on your Windows machine, you may need to add the following block to your Vagrantfile:

  1. config.vm.provider "virtualbox" do |v|
  2. v.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/v-root", "1"]
  3. end