Vagrant makes it dead simple to create a portable server environment on your local development system. All future webapphero articles and videos about Linux sys admin and dev-ops will presume the use of Ubuntu Server. Use Vagrant to setup Ubuntu Server on a virtual machine within Windows, Mac, or Linux. This video walks through the basics of Vagrant and notes below provide a brief synopsis.
Why Vagrant? Vagrant produces portable, virtual machines (VM), such as Ubuntu Server, that can be easily shared as development environments among a team of developers, and also between local development and production systems. Vagrant automatically syncs files from our project folder to the /vagrant directory of the guest VM.
By default, Vagrant uses VirtualBox as a “provider” to create a “box”. Multiple boxes may also be produced to allow any variety of multi-system configuration, e.g. separate systems for web server and database.
Boxes are created according to customizable specifications located in the config file named “Vagrantfile”. Multiple Vagrantfiles can be combined to allow for special needs, but we work mainly with the Vagrantfile found in your “project_folder” upon running “vagrant init”. Essentially, there is 1 Vagrantfile per project.
The Vagrantfile defines:
- guest machine type
- guest machine configuration
- guest machine software installation (“provisioning”)
The Vagrantfile should be added to our version control repository so that each team member may simply checkout the repo and run “vagrant up” to produce a fully configured work environment. Powerful!
Vagrant commands, all starting with “vagrant”, can be run from any sub-directory within our project folder thanks to a bubble-up lookup pattern described in more detail here.
Setup goes like this:
$ mkdir project_folder
$ cd project_folder $ vagrant init $ vagrant box add precise32 http://files.vagrantup.com/precise32.box
edit project_folder/Vagrantfile appropriately with:
Vagrant.configure("2") do |config| config.vm.box = "precise32" config.vm.network :forwarded_port, host: 55555, guest: 80 end
$ vagrant up $ vagrant ssh
Explore the new guest VM:
vagrant@precise32:~$ ls -l /vagrant # shows the contents of project_folder
Soak in all the goodness Vagrant brings and enjoy a moment of inner reflection…
We’re back to the host:
Three ways to teardown our guest VM:
$ vagrant destroy #all traces removed
$ vagrant halt #graceful exit, disk saved, next boot time longer than suspend
$ vagrant suspend #quick next boot, but disk space hungry storage of VM RAM state
That’s it for this article. Please let me know if you have any good Vagrant tips as I’m just starting out with it.
Update on Friday, March 21, 2014:
After some period of vagrant usage, you will likely receive the following error upon running vagrant up from your host system:
“The guest additions on this VM do not match the installed version of
VirtualBox! In most cases this is fine, but in rare cases it can
cause things such as shared folders to not work properly. If you see
shared folder errors, please update the guest additions within the
virtual machine and reload your VM.
Guest Additions Version: 4.2.0
VirtualBox Version: 4.3″
Here are the steps I followed to fix it:
1) Install the vagrant-vbguest plugin from the host system terminal:
$ vagrant plugin install vagrant-vbguest
2) Destroy old VM
$ vagrant destroy
3) Create new VM
$ vagrant up
Note: You can safely ignore the following non-fatal error, if you see it after the last step:
“Installing the Window System drivers …fail!
(Could not find the X.Org or XFree86 Window System.)
An error occurred during installation of VirtualBox Guest Additions 4.3.4. Some functionality may not work as intended.
In most cases it is OK that the “Window System drivers” installation failed.”
You will continue to see this last error message every time you create a new VM.