How to save/remove existing Vagrant box and restore the saved vagrant box

We know that Vagrant is a tool for building complete development environments. It allows us to imitate the setup of a server on our local machine which can be easily reproduced on any system.

This article will show three useful routines for Vagrant: saving an existing Vagrant box setup, removing old/unused Vagrant box and restoring saved vagrant box. It is assumed that you are using Windows as host machine's OS and Cygwin command line interface. Throughout this article, we will be using centos7drupal name for our Vagrant box.

Saving an existing Vagrant box:

  1. If your Vagrant box is still running, make sure to shut it down first:

          
    vagrant halt
          
        
  2. Execute create Vagrant box package:

          
    vagrant package --output centos7drupal.box
          
        
  3. The Vagrant box package will be saved as centos7drupal.box file.

Removing an existing Vagrant box:

  1. If your Vagrant box is still running, make sure it is shutdown:

          
    vagrant halt
          
        
  2. Execute destroy Vagrant box:

          
    vagrant destroy
          
        

    Note: If you received this error message:

          
    Vagrant is attempting to interface with the UI in a way that requires
    a TTY. Most actions in Vagrant that require a TTY have configuration
    switches to disable this requirement. Please do that or run Vagrant
    with TTY.
          
        

    ... execute this command instead:

          
    vagrant destroy -f
          
        
  3. Backup the existing Vagrant config file and delete it:

          
    cp Vagrantfile Vagrantfile.bak
    rm Vagrantfile
          
        
  4. Delete the actual Vagrant box image:

          
    rm ~/.vagrant.d/boxes/centos7drupal
          
        

Restoring saved Vagrant box:

  1. Execute add Vagrant box:

          
    vagrant box add centos7drupal centos7drupal.box
          
        
  2. Initialize your Vagrant box:

          
    vagrant init centos7drupal
          
        
  3. That command will create generic Vagrant config file. If you prefer to use the previous Vagrant config file, execute the following commands:

          
    rm Vagrantfile
    mv Vagrantfile.bak Vagrantfile
          
        
  4. Boot your Vagrant box:

          
    vagrant up
          
        

    Only the initial boot-up will take longer time than the succeeding boots.

Comments

Hi,
Creating a box in the manner suggested and then using it causes the new box to override the mounted folders in the original box and that is replaced by a default folder. Is there any way to :-
1. preserve the original synced folders information in the new box.
2. the best / simplest way to manually mount the folders in the new box so that they are preserved between vagrant ups or reloads.

Thanks for the guide. But it should be rm -r rm ~/.vagrant.d/boxes/centos7drupal since it is a directory.

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.