Laravel Homestead Vagrant copy files on provision

Laravel Homestead Vagrant copy files on provisionPhoto by Victor Garcia on Unsplash

Quick tip for how to automatically copy files from your local box when you provision your Laravel Homestead Virtual Machine.

Whenever you create a new Homestead dev environment, or make changes to the VM, or update the underlying OS or host, you copy over pretty good defaults from the Homestead package.

However, Homestead, which is based on Vagrant, enables us to customize our initial dev environment by automatically copying your files as specified in your Homestead.yaml config file, which configures Vagrant.

For example, I like to use a few shell aliases for convenience and speed, which I would normally add to ~/.bash_aliases in the VM but if I re-provision the VM, e.g. to allocate more memory, these will be wiped out. So, instead, let’s tell Homestead to copy across these files when provisioning a VM.

Create your local files to copy

I suggest you create a directory for the files you want to automatically copy to the Homestead environment outside of your Homestead directory so that they are not overwritten or otherwise lost in an upgrade to the Homestead app itself.

Add your files in the new directory we just created.

Edit Homestead.yaml

Now, add a new stanza to your Homestead.yaml listing the files you want to copy. Note: I’m copying two files, up one level, in the HomesteadCopy directory previously created, to the default user, vagrant, home directory:

Finally, re-provision the Homestead environment:

These files should now be copied automatically (or overwritten in the case of existing files) to your Homestead environment every time you provision a new VM.

Leave a message:

Your email address will not be published. Required fields are marked *