After installation of Vagrant lets start with the basics.
Initialize Vagrantfile and add your first box:
Create a folder for example “/home/user/vagrant” or “C:\vagrant” where you can put your vagrant config files.
$ mkdir vagrant
$ cd vagrant
Next step is to init “Vagrantfile” which will be used to define your development environment and the guest configs. Below is the first command to initialize and add a box.
$ vagrant init precise64 https://vagrantcloud.com/hashicorp/boxes/precise64/versions/2/providers/virtualbox.box
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
This command above tells that first to initialize the Vagrant file and create a box named “ubuntu/precise64” and download it from the provided URL.
In link https://vagrantcloud.com/discover/featured you can find lots of ready made boxes.
Download the box and start the guest:
After Vagrantfile created the next step is to us “
vagrant up” command. It will start the download of the box file; create the guest virtual machine; and will make necessary configurations of it.
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box precise64' could not be found. Attempting to find and install...
default: Box Provider: virtualbox
default: Box Version: >= 0
==> default: Adding box 'hashicorp/precise64' (v0) for provider: virtualbox
default: Progress: 1% (Rate: 466k/s, Estimated time remaining: 0:11:35
==> default: Importing base box 'precise64'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: vagrant_default_1412516223000_70241
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
default: The guest additions on this VM do not match the installed version of
default: VirtualBox! In most cases this is fine, but in rare cases it can
default: prevent things such as shared folders from working properly. If you see
default: shared folder errors, please make sure the guest additions within the
default: virtual machine match the version of VirtualBox you have installed on
default: your host and reload your VM.
default: Guest Additions Version: 4.2.0
default: VirtualBox Version: 4.3
==> default: Mounting shared folders...
default: /vagrant => /Users/fuatu/vagrant
Now your guest computer is ready and running you can connect with command “
vagrant ssh“. Below you can see the terminal of “Ubuntu 12.04”
$ vagrant ssh
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic x86_64)
* Documentation: https://help.ubuntu.com/
New release '14.04.1 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
Welcome to your Vagrant-built virtual machine.
Last login: Fri Sep 14 06:23:18 2012 from 10.0.2.2
Important Details for Windows Users:
vagrant ssh” will not work on Windows. I suggest to install CygWin and its OpenSSH package. After installation you also need to set the “PATH” variable for ssh. For that right click on “My Computer” goto “Properties –> Advanced System Settings –> Environment Variables” section. There find the “PATH” variable under “System Variables” and add, at the end of it, your ssh.exe path. For my computer the path to be added was “C:\cygwin64\bin”.
Destroy the guest OS:
Lastly when you are done you can destroy the virtual machine with “
vagrant destroy” command.
$ vagrant destroy
default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Forcing shutdown of VM...
==> default: Destroying VM and associated drives...
Next step: The Vagrantfile