89

I've the problem that my home directory is actually located on a remote server and with ~/.vagrant.d sitting on that server, the performance of vagrant decreases heavily (and file-server backup size increases).

So is there any way to move at least ~/vagrant.d/boxes out of the home directory?

Cheers.

pagid
  • 13,559
  • 11
  • 78
  • 104

8 Answers8

128

By default Vagrant uses ~/.vagrant.d. Fortunately, vagrant provides an environment variable called VAGRANT_HOME by which you can set vagrant home.

Just do the following to change the vagrant home (this only works in the current session)

export VAGRANT_HOME=/path/to/vagrant

To make it permanent, add this to your ~/.bash_profile (for login shell).

Update: VAGRANT_HOME has been added to the documentation - Environmental Variables

VAGRANT_HOME can be set to change the directory where Vagrant stores global state. By default, this is set to ~/.vagrant.d. The Vagrant home directory is where things such as boxes are stored, so it can actually become quite large on disk.

Terry Wang
  • 13,840
  • 3
  • 50
  • 43
  • 8
    Actually I found that VAGRANT_HOME has to be set and that the property "machinefolder" in VirtualBox has to be set through "VBoxManage setproperty machinefolder " __ Thanks for the hint – pagid Feb 12 '13 at 08:45
  • 3
    My personal findings: When making the environment variable permanent (in `~/.bash_profile` or if you use ZSH in `~/.zshrc`, etc.) , make sure you write `export` as well. Check if it's set with `env` on the command line. When using virtualbox as provider, there is also a setting in the GUI for it's default path. – Urs Feb 22 '16 at 18:27
  • 3
    For Windows use the environment variables dialog and add VAGRANT_HOME with for instance value d:\vagrant. Works perfectly well with version 2.2.4 at least. Remember to close the existing PowerShell console and open a new one to load the new environment variable. – Eivind Gussiås Løkseth Jun 10 '19 at 20:12
  • On WSL2 - I had an issue where I deleted a virtual box created by vagrant and suddenly it no longer was able to make a vagrant up - I managed to fix it by copying the ~/.vagrant.d folder to my windows c drive - /mnt/c/ Afterwards I could again make vagrant up – serup Nov 02 '20 at 11:06
  • since i am using git bash in windows 10, instead of using `~/.bash_profile` use `~/.bashrc` – Christian Noel Apr 12 '22 at 01:04
16

VAGRANT_HOME does not work well in my Windows 8.1. It worked for me when I changed the file

D:\HashiCorp\Vagrant\embedded\gems\gems\vagrant-1.5.3\lib\vagrant\environment.rb

on line 117 to

@home_path = Util::Platform.fs_real_path("D:/vagrant/home/")

like Steve H said and it works fine.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Awesome
  • 181
  • 1
  • 3
  • 4
    If you use windows local path remember to escape backslash (this caught me out). I.e. "D:\\vagrant\\home" – Guerrilla Jun 05 '15 at 14:25
  • 2
    It works well now, at least with version 2.2.4. I added VAGRANT_HOME to the environment variables dialog and set its value to d:\vagrant, which worked perfectly fine after closing and opening a new PowerShell console. – Eivind Gussiås Løkseth Jun 10 '19 at 20:10
  • Yes, it works. No need to escape backslashes. Simply `VAGRANT_HOME=d:\my\alternative\path`. I guess you didn't reload the environment (by closing and restarting the console for example) after you created the environment variable. – David Ferenczy Rogožan Aug 09 '19 at 14:51
  • **Never** do exactly what you did (modified a file which is part of a software you're using). You would have to reapply your modification every time you reinstall or upgrade Vagrant to a newer version. – David Ferenczy Rogožan Aug 09 '19 at 14:54
13

It might be useful to permanently set this on a Windows box by executing

setx VAGRANT_HOME "/d/.vagrant.d/"
ivica
  • 1,388
  • 1
  • 22
  • 38
  • 1
    You have to run it from path where setx app is located. Most common path is: C:\Windows\System32 – versedi Apr 04 '15 at 13:33
  • setx can be executed without having to move to `C:\Windows\System32`, as `C:\Windows\System32` is normally on the `%path%`. – lucid_dreamer May 01 '18 at 23:33
5

On Windows change line 17 of environment.rb located at:
vagrant\embedded\gems\gems\vagrant-1.x.x.dev\lib\vagrant\environment.rb

Storsey
  • 301
  • 1
  • 3
  • 14
  • 3
    Not sure why I was voted down. This is relevant to the question - it wasn't labelled linux and could help someone like it did me. – Storsey Aug 09 '13 at 23:05
  • 15
    Messing around in the ruby files is not a good advice at all and the VAGRANT_HOME environment variable is also working fine on Windows. – pagid Aug 30 '13 at 16:35
  • Didn't realise the env variable was available on Win.. sorted that right out, thanks! – Storsey Jan 09 '14 at 11:18
  • 1
    This is a valid response. Not everyone wants to (or knows to) restart Windows for the new environment variables to take effect. Chances are if someone is looking at changing the core files, it is either temporary or desperation has set it. https://support.microsoft.com/en-us/kb/821761 – TH_ Jan 07 '16 at 10:05
  • Why exactly should you need to restart Windows? That's definitely not needed. I just created an environment variable right now and it's working fine. No restart. You may just need to reload an environment in a console or just start it after you created the environment variable. Don't modify the files which are part of some software or package. – David Ferenczy Rogožan Aug 09 '19 at 14:59
2

an other place (the root place where it read ENV variables) is in shared_helpers.rb, line 71 (vagrant v 1.6.5) :

 # This returns the path to the ~/.vagrant.d folder where Vagrant's
  # per-user state is stored.
  #
  # @return [Pathname]
  def self.user_data_path
    # Use user spcified env var if available
    path = ENV["VAGRANT_HOME"]

    # On Windows, we default to the USERPROFILE directory if it
    # is available. This is more compatible with Cygwin and sharing
    # the home directory across shells.
    if !path && ENV["USERPROFILE"]
      path = "#{ENV["USERPROFILE"]}/.vagrant.d"
    end

    # Fallback to the default
    path ||= "~/.vagrant.d"

    Pathname.new(path).expand_path
end

Anyway, I think the best way is to use the environment variable VAGRANT_HOME, in case of vagrant version upgrade.

You can use this function like:

disk_path = self.user_data_path().to_s
Andomar
  • 232,371
  • 49
  • 380
  • 404
Christophe
  • 21
  • 1
1

For Windows users, set the environment variable VAGRANT_HOME to the new location. You might need to restart your PC for it to take effect.

Dalton Tan
  • 485
  • 1
  • 6
  • 17
1

It works for me on Linux creating a simbolic link from a folder on my secundary hard drive to ~/.vagrant.d/boxes/.

ouflak
  • 2,458
  • 10
  • 44
  • 49
Juam Sousa
  • 11
  • 1
0

Set of VAGRANT_DOTFILE_PATH environmental variable helped me on Windows machine. VAGRANT_HOME didn't work out.

Nazar
  • 624
  • 6
  • 7
  • Yes I found VAGRANT_HOME set the box directory. But the individual vm files wouldn't move unless VAGRANT_DOTFILE_PATH was set too. – RM1358 Mar 16 '22 at 07:21