Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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(basebox_name)
  cache_dir ='cache', 'apt', basebox_name)
  partial_dir = cache_dir.join('partial')
  partial_dir.mkdir unless partial_dir.exist?

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.


This comment has been minimized.

Copy link

sneak commented Feb 4, 2014

If you are using a mac, ~/Library/Caches/ already exists for this purpose. Please don't make life harder for those who maintain backup script exclude paths.


This comment has been minimized.

Copy link

bronger commented Nov 13, 2015

And on Ubuntu, use ~/.cache/


This comment has been minimized.

Copy link

fxkr commented Nov 27, 2015

Actually, only use ~/.cache if $XDG_CACHE_HOME is not set or empty. If it's set, use that.

Here's the spec:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.