Moving Vagrant Boxes and related VirtualBox VMs to another drive

If you’re like me using Vagrant to run your development environment(s), you may have already noticed that Vagrant boxes and their related VirtualBox Vitual Machines are taking lots of space on your hard drive (12Go in my own case).

And if your hard drive is a SSD or just badly partitioned, you may quickly be running out of space…

Here is the way I moved my Vagrant boxes and related VirtualBox VMs from my C to my D drive on a Windows 8 computer :

(I’m using git bash to run Unix commands, but you can do this with console.exe or even manually)

  • First, backup your work, just in case 😉
  • Stop every running Vagrant boxes
> cd path_to_vagrant_project_folder
> vagrant halt
  • Move your .vagrant.d folder
> mv c:/Users/[username]/.vagrant.d d:/[path]/.vagrant.d
  • Create a new VAGRANT_HOME environment variable and set its path to the new .vagrant.d location
  • Create a new folder for your VirtualBox VMs on your D drive
> mkdir -p d:/[path]/.VirtualBoxVagrantVMs
  • Move the VirtualBox VMs directories related to your Vagrant boxes.
> mv c:/Users/[username]/.VirtualBoxVMs/[each VM directory] d:/[path]/.VirtualBoxVagrantVMs
  • Edit the VMs paths in your VirtualBox.xml settings file, located in:
c:/Users/[username]/.VirtualBox/VirtualBox.xml

For each VM related to a Vagrant box, edit the path included in the <MachineRegistry> tag of VirtualBox.xml as follow:

<MachineRegistry>
<MachineEntry uuid=”{vagrant_vm_uuid}” src=”d:\[path]\.VirtualBoxVagrantVMs\[vagrant_vm_name]\[vagrant_vm_name].vbox”/>
<MachineEntry uuid=”{vagrant_vm2_uuid}” src=”d:\[path]\.VirtualBoxVagrantVMs\[vagrant_vm2_name]\[vagrant_vm2_name].vbox”/>

<MachineEntry uuid=”{other_vm_uuid}” src=”C:\Users\[username]\.docker\machine\machines\default\default\default.vbox”/>
</MachineRegistry>
  • And you’re done 😉
  • Now let’s try to run one of your old Vagrant project and check everything is running as it was before:
> cd path_to_vagrant_project_folder
> vagrant up

I hope this helped you saving a bit of space in your C drive.