How to export a Vagrant virtual machine to transfer it
Asked Answered
B

9

118

I have a vagrant box up and running (configured with a LAMP stack). I need to transfer it to another PC. How can I export it? I guess that I can get a file (or files) that can be copied to another PC, so there I can run some command to import the vagrant box.

Buckish answered 19/12, 2013 at 10:24 Comment(4)
I wrote some scripts for automating the transfer of vagrant boxes. See soniah/transfer_vagrant_boxesImmitigable
// , Has anyone tried out Sonia's scripts?Pals
@NathanBasanese AFAIU, those scripts help transfering base boxes, not the resulting running VMs, as they make use of vagrant box repackage (see its docs)Hogle
Possible duplicate of Is there any way to clone a vagrant box that is already installedChenab
E
131

You have two ways to do this, I'll call it dirty way and clean way:

1. The dirty way

Create a box from your current virtual environment, using vagrant package command:

http://docs.vagrantup.com/v2/cli/package.html

Then copy the box to the other pc, add it using vagrant box add and run it using vagrant up as usual.

Keep in mind that files in your working directory (the one with the Vagrantfile) are shared when the virtual machine boots, so you need to copy it to the other pc as well.

2. The clean way

Theoretically it should never be necessary to do export/import with Vagrant. If you have the foresight to use provisioning for configuring the virtual environment (chef, puppet, ansible), and a version control system like git for your working directory, copying an environment would be at this point simple as running:

git clone <your_repo>
vagrant up
Eugeniusz answered 19/12, 2013 at 11:40 Comment(14)
Trying to be that clean, foresightful admin... You don't have to put an OS install iso in your git repo do you? Are you suggesting that, for example. using ansible, your repo would contain the Vagrantfile and an ansible playbook. The Vagrantfile would 1. download a basebox from vagrantcloud 2. run the ansible playbook to provision/configure the basebox using other files/repos/cloud-storage. Right? Or is there something else in the git repo?Ramiform
You can use packer to do the install from the iso, which has a url and a md5sum etc.Mesencephalon
Does the "clean way" account for databases (mySQL, Wordpress) that might be inside the vm? How can that be synced over to the other pc?Disentitle
@Disentitle The clean way means that there is no information outside of the git repository that is required to start a new dev environment. This implies that installing and configuring the database will be completed during provisioning of the new vagrant box (via Chef/Puppet/what-have-you).Koester
So is the way recommended by Hashicorp also 'dirty'? i.e. package the box and upload it to the atlas server? I'm not sure if the 'clean' way will actually replicate the exact environment, which I thought was the whole purpose of vagrant....Ondrea
I don't understand how the official way is considered dirty. Any good soul could explain why?Aftercare
The supposed "clean way" is a really bad advice that people should avoid. The package command is the proper way to do it.Fiend
"The clean way" is not certainly clean. That is simply the recreation of an environment and certainly loses the purpose of using vagrant in the first place.Bevus
The "clean" was is not sufficient for a lot of people - I want to be able to move my DB data over, without storing a multi-gig SQL dump in my repo...Keystroke
The "clean" way assumes that your Vagrantfile defines the exact machine state that you need. So to export, you simply need the Vagrantfile on another machine. The dirty way, packages your box as a new vagrant box image so that you can physically (via a external drive/dropbox) copy it somewhere else and reuse it. Though, to reuse it, you'll now need a different Vagrantfile. I think this point is key, since your older Vagrantfile (probably) setup a box from a public image.Deyoung
@tcp2008 Sorry for the late react, but... yes, the official way is a dirty one, in my opinion.Nemathelminth
s/clean/stateless/ && s/dirty/stateful/. Migrating system services with state? Then go stateful. Setting up a new system with no transactions in it? Go stateless. A developer might see stateful as "dirty" (not a 12 factor app) while a system operator might see stateless as overly complicated; take it apart and build it again on the new system?! OP's question wasn't explicit but I see signs that they're trying to maintain state, thus, packaging the box is the "right" way. That question belongs on SuperUser though. This is SO, a dev site, so stateless is also the "right" way.Bosomy
+1 for dirty/stateful. My 3rd-party build script has an error so I can't reprovision and I don't have time to wait for them to fix it. So I am packaging/box-adding from an older machine that already has the box built.Obstetrics
Another aspect, which is important at least to me, is how trustable boxes available on vagrantcloud are. When in doubt, the "clean way" is probably not your best choice, I'd rather package my own box from sources I trust better, like a signed repository. And I'll need to export it at some point.Hue
M
32

My hard drive in my Mac was making beeping noises in the middle of a project so I decided to install a SSD. I needed to move my project from one disk to another. A few things to consider:

  • I'm vagrant w/ virtualbox on a Mac
  • I'm using git

This is what worked for me:

1.) Copy your ~/.vagrant.d directory to your new machine.
2.) Copy your ~/VirtualBox\ VMs directory to your new machine. 
3.) In VirtualBox add the machines one by one using **Machine** >> **Add**
4.) Run `vagrant box list` to see if vagrant acknowledges your machines. 
5.) `git clone my_project`
6.) `vagrant up`

I had a few problems with VB Guest additions.

enter image description here

I fixed them with this solution.

Malcommalcontent answered 29/4, 2014 at 11:48 Comment(2)
// , I am going to try this, @KJPrince. Thanks for breaking it down for us.Pals
// , I tried it, but I got the errors at the following link when I copied over my VirtualBoxVMs directory over to ~/VirtualBox\ VMs, and ran vagrant box list: 0bin.net/paste/iCiaqNhU-i3bbfY2#-KDiXYcRFni1RNgNJhOBrIs/…Pals
R
26

This is actually pretty simple

  1. Install virtual box and vagrant on the remote machine
  2. Wrap up your vagrant machine

    vagrant package --base [machine name as it shows in virtual box] --output /Users/myuser/Documents/Workspace/my.box

  3. copy the box to your remote

  4. init the box on your remote machine by running

    vagrant init [machine name as it shows in virtual box] /Users/myuser/Documents/Workspace/my.box

  5. Run vagrant up

Ruysdael answered 6/3, 2017 at 19:17 Comment(4)
If your machine's name is long and you are too lazy to look at Virtualbox GUI and type it's name or id - you can list all your VM's names and id's with this terminal command: VBoxManage list vms. After you can just copy and do what is told in @Phil 2nd and 4th steps. :) Yey.Pangermanism
@Pangermanism why not just $ vagrant box listLeanoraleant
@KenRatanachaiS because your command lists boxes, e.g.: (scotch/box (virtualbox, 3.0)). My command - virtual machines with identifier, e.g.: "Win10 - SandBox" {bc251c06-501f-4c0f-bgfa-21f2815ba2cd}Pangermanism
This just gives me a blank vm.Instate
V
6

The easiest way would be to package the Vagrant box and then copy (e.g. scp or rsync) it over to the other PC, add it and vagrant up ;-)

For detailed steps, check this out => Is there any way to clone a vagrant box that is already installed

Vociferant answered 20/12, 2013 at 2:53 Comment(0)
G
2

None of the above answers worked for me. I have been 2 days working out the way to migrate a Vagrant + VirtualBox Machine from a computer to another... It's possible!

First, you need to understand that the virtual machine is separated from your sync / shared folder. So when you pack your machine you're packing it without your files, but with the databases.

What you need to do:

1- Open the CMD of your computer 1 host machine (Command line. Open it as Adminitrator with the right button -> "Run as administrator") and go to your vagrant installed files. On my case: C:/VVV You will see your Vagrantfile an also these folders:

/config/
/database/
/log/
/provision/
/www/
Vagrantfile
...

The /www/ folder is where I have my Sync Folder with my development domains. You may have your sync folder in other place, just be sure to understand what you are doing. Also /config and /database are sync folders.

2- run this command: vagrant package --vagrantfile Vagrantfile

(This command does a package of your virtual machine using you Vagrantfile configuration.)

Here's what you can read on the Vagrant documentation about the command:

A common misconception is that the --vagrantfile option will package a Vagrantfile that is used when vagrant init is used with this box. This is not the case. Instead, a Vagrantfile is loaded and read as part of the Vagrant load process when the box is used. For more information, read about the Vagrantfile load order.

https://www.vagrantup.com/docs/cli/package.html

When finnished, you will have a package.box file.

3- Copy all these files (/config, /database, Vagrantfile, package.box, etc.) and paste them on your Computer 2 just where you want to install your virtual machine (on my case D:/VVV).

Now you have a copy of everything you need on your computer 2 host.

4- run this: vagrant box add package.box --name VVV

(The --name is used to name your virtual machine. On my case it's named VVV) (You can use --force if you already have a virtual machine with this name and want to overwrite it. (Use carefully !))

This will unpack your new vagrant Virtual machine.

5- When finnished, run: vagrant up

The machine will install and you should see it on the "Oracle virtual machine box manager". If you cannot see the virtual machine, try running the Oracle VM box as administrator (right click -> Run as administrator)

You now may have everything ok but remember to see if your hosts are as you expected:

c:/windows/system32/hosts

6- Maybe it's a good idea to copy your host file from your Computer 1 to your Computer 2. Or copy the lines you need. In my case these are the hosts I need:

192.168.50.4 test.dev
192.168.50.4 vvv.dev
...

Where the 192.168.50.4 is the IP of my Virtual machine and test.dev and vvv.dev are developing hosts.

I hope this can help you :) I'll be happy if you feedback your go.

Some particularities of my case that you may find:

When I ran vagrant up, there was a problem with mysql, it wasn't working. I had to run on the Virtual server (right click on the oracle virtual machine -> Show console): apt-get install mysql-server

After this, I ran again vagrant up and everything was working but without data on the databases. So I did a mysqldump all-tables from the Computer 1 and upload them to Computer 2.

OTHER NOTES: My virtual machine is not exactly on Computer 1 and Computer 2. For example, I made some time ago internal configuration to use NFS (to speed up the server sync folders) and I needed to run again this command on the Computer 2 host: vagrant plugin install vagrant-winnfsd

Gael answered 14/2, 2018 at 21:18 Comment(1)
The point that the same software is not installed should be an obvious indication that it's not the same VM but in fact another one with the same name.Instate
E
2

I had to migrate my Vagrant boxes to a new machine and found this dirty but simple method to work well (it's for VirtualBox-based machines, I assume it can be adapted to other providers):

  • copy over the VirtualBox box, e.g. by using the "Export/Import Appliance" functionality in the VirtualBox GUI
  • copy over the Vagrant directory
  • edit .vagrant/machines/default/virtualbox/id in the Vagrant directory to the new VirtualBox ID (which you get via VBoxManage list vms).
Exclusive answered 15/1, 2023 at 2:55 Comment(2)
Credit for the last step to @ingmurillo in this question.Exclusive
This solution actually did not work for me as-is, as running vagrant up still created a new VM. However I was able to attach the old machine's disk to the new one using the VirtualBox GUI and this now works as desired.Instate
S
1

As stated in

How can I change where Vagrant looks for its virtual hard drive?

the virtual-machine state is stored in a predefined VirtualBox folder. Copying the corresponding machine (folder) besides your vagrant-project to your other host should preserve your virtual machine state.

Signorina answered 19/12, 2013 at 11:58 Comment(0)
I
0

I also had to copy the private_key file from .vagrant/machines/default/virtualbox folder in order for vagrant ssh to work

Indonesia answered 1/9, 2021 at 17:37 Comment(0)
D
-1

As of 2019 there is a subcommand: vagrant box repackage

vagrant box repackage --help 
Usage: vagrant box repackage <name> <provider> <version>
    -h, --help                       Print this help

You can find name provider and version by running vagrant box list

vagrant box list
macinbox (virtualbox, 10.14.5)

The ouput of vagrant box repackage is a file called package.box which is basically a tgz file which the content can be listed as below:

tar tzf package.box
./metadata.json
./box.ovf
./Vagrantfile
./box-disk001.vmdk
Delamare answered 12/7, 2019 at 10:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.