Skip to content

Instantly share code, notes, and snippets.

Forked from juanje/gist:3797297
Created September 28, 2012 09:04
Show Gist options
  • Save millisami/3798773 to your computer and use it in GitHub Desktop.
Save millisami/3798773 to your computer and use it in GitHub Desktop.
Mount apt cache of a Vagrant box in the host to spin up the packages installation

This is a little trick I use to spin up the packages instalation on Debian/Ubuntu boxes in Vagrant.

I add a simple function that checks if a directory named something similar to ~/.vagrant.d/cache/apt/opscode-ubuntu-12.04/partial (it may have another path in Windows or MacOS) and create the directory if it doesn't already exist.

def local_cache(box_name)
  cache_dir = File.join(File.expand_path(Vagrant::Environment::DEFAULT_HOME),
  partial_dir = File.join(cache_dir, 'partial')
  FileUtils.mkdir_p(partial_dir) unless File.exists? partial_dir

I put this funcion in my Vagrantfile so I can use it like this: do |config| = "opscode-ubuntu-12.04"
  config.vm.box_url = ""
  cache_dir = local_cache(

  config.vm.share_folder "v-cache",


The result is that the apt cache inside the VM (/var/cache/apt/archives/) is always mounted on the same directory on the host (~/.vagrant.d/cache/apt/opscode-ubuntu-12.04/ in this case) and apt just download the packages the first time or if the package has been updated.

This save me a lot of time while I'm developing or debuging Chef cookbooks. Mostly when my recipes need to install many packages.

Copy link

Nifty - thanks very much for sharing.

Copy link

gimler commented Jun 6, 2013

Vagrant::Environment::DEFAULT_HOME no more exists change File.expand_path(Vagrant::Environment::DEFAULT_HOME) to File.dirname(FILE)

Copy link

Doesn't that make the cache local to the Vagrant project instead of the user's home directory?

Copy link

@gimier File.dirname(__FILE__) is just the path to your Vagrantfile, which means it will not be in the home directory.
Maybe instead hardcode ~/.vagrant/d instead?

Also everybody should be using instead. It's like this gist, but a proper Vagrant plugin... and it's awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment