Getting Vagrant – From Vagrant Site
If you are running an older version of Ubuntu, like I usually do (I run only LTS releases indeed) it’s probably better to download the latest Vagrant available on the Vagrant site.
$ cd /tmp $ mkdir vagrant $ cd vagrant $ wget http://files.vagrantup.com/packages/a40522f5fabccb9ddabad03d836e120ff5d14093/vagrant_1.3.5_x86_64.deb $ sudo dpkg --install vagrant_1.3.5_x86_64.deb
Install some machines
Vagrant is about building and running virtual machines described through a file, in order to let all developers to have the same machine. What vagrant really does is to keep locally a series of bare machines that will then be cloned to produce the environment you are describing. This means that before doing whatever in Vagrant, you should first of all populate this local repository with the bare machines you’d like to use.
On internet you can find some repositories of Vagrant boxes:
So, let’s decide to test Vagrant with an ubuntu machine.
$ vagrant box add precise32 http://files.vagrantup.com/precise32.box Downloading or copying the box... Extracting box...te: 536k/s, Estimated time remaining: --:--:--) Successfully added box 'precise32' with provider 'virtualbox'!
Let’s check whether the box has been successfully installed
$ vagrant box list precise32 (virtualbox)
Setting up a Machine
Let’s try to set up a first experiment with Vagrant. More or less following what the official guides states. First of all let’s create a folder to host our experiment, and let’s move in it.
$ cd /tmp $ mkdir vagrant-tutorial $ cd vagrant-tutorial/
Time to initialize the Vagrant project. That is done with the “init” command. Just check what “init” does before issuing the command.
$ vagrant init -h
Then, let’s go with the initialisation.
$ vagrant init
A file called Vagrantfile appears after initialisation. Just give it a look. It’s full of commented options along with bried description, so, giving it a look can provide you with hints about what you can expect from Vagrant. The only not commented instruction is the one that tells Vagrant what machine we want to build on top of. It is currently the “base” box, that we are missing.
# Every Vagrant virtual environment requires a box to build off of. config.vm.box = "base"
So, let’s change it to
# Every Vagrant virtual environment requires a box to build off of. config.vm.box = "precise32" and save the file.
Now, the magic all of you are waiting for
$ vagrant up Bringing machine 'default' up with 'virtualbox' provider... [default] Importing base box 'precise32'... [default] Matching MAC address for NAT networking... [default] Setting the name of the VM... [default] Clearing any previously set forwarded ports... [default] Creating shared folders metadata... [default] Clearing any previously set network interfaces... [default] Preparing network interfaces based on configuration... [default] Forwarding ports... [default] -- 22 => 2222 (adapter 1) [default] Booting VM... [default] Waiting for machine to boot. This may take a few minutes... [default] Machine booted and ready! [default] Mounting shared folders... [default] -- /vagrant
Wow… do we really have a running machine ? Yes. Just type…
$ vagrant ssh
And you are connected to the vagrant running machine in a SSH session.
Turn off the machine
Exit from the SSH session and type…
$ vagrant status
To have a list of currently running machines. Turn off the machine with following command.
vagrant halt default [default] Attempting graceful shutdown of VM...
$ vagrant status Current machine states: default poweroff (virtualbox)
Remove the machine
When you are done, just issue a destroy command to completely remove any reference to your machine.
$ vagrant destroy Are you sure you want to destroy the 'default' VM? [y/N] y [default] Forcing shutdown of VM... [default] Destroying VM and associated drives...