Laravel Homestead Vagrant copy files on provision
Photo by Sebastian Herrmann 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.
1 2 3 |
cd ~/Documents/Homestead cd .. mkdir HomesteadCopy |
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:
1 2 3 4 5 |
copy: - from: ../HomesteadCopy/.bash_aliases to: /home/vagrant/ - from: ../HomesteadCopy/.profile to: /home/vagrant/ |
Finally, re-provision the Homestead environment:
1 |
vagrant reload --provision |
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.