By the end of this article you’ll know: How to customize your virtual machine to your liking and how to access the vagrant machine through the browser, ssh and shared folders.
In our previous Vagrant article we initialized and started up a Linux Ubuntu 14.04 virtual machine by just running two commands:
$ vagrant init ubuntu/trusty64
$ vagrant up
The first command placed the Vagrantfile in our folder and the second one set up and started our virtual machine. However, we don’t know much about this machine apart from that it is an Ubuntu 14.04. To find out more and to change anything about the machine we have to look into the Vagrantfile.
The Vagrantfile is the file that determines the type of machine your require for aproject and allows you to configure the machine.
If you open the Vagrantfile that was created in the first article you’ll notice that it contains many comments (lines that start with a # ). These comments try to guide you on how to configure your machine.
If you remove the comments you’ll end up with this:
Those three lines are responsible for the virtual machine with we created. Let’s go through each line.
Vagrant.configure("2") do |config|: The configuration of any Vagrant box begins with this line. It creates a configuration object called “config” that will be used to configure the virtual machine.
The number 2 represents the Vagrant version of configuration. At the moment there are only two versions. At the time of writing, two is the latest and most used.
Also, the object config can be named as anything. There is no need to change that though.
config.vm.box = "ubuntu/trusty64": First of all, since the object is called “config” the object is referred through that name all through the Vagrantfile. When “.vm” is appended to “config” is creates a namespace that can be used to configure the virtual box.
This particular line is responsible for configuring what base box to use for the virtual machine. These are boxes hosted in the Vagrant cloud. In this case the “ubuntu/trusty64”.
Notice that the line is indented.
end: Simply marks that conclusion of the configuration.
If you are going to use your virtual machine to emulate a server you’ll probably need to determine how you are going to access it through a network.
The namespace for network configuration is:
config.vm.network. With this you can determine if you want to access the virtual box through port forwarding, a private network or a public network. I advise using the first two because of security and complexity reasons. Only use public networks if you’re sure about what you’re doing.
Synced folders enable Vagrant to sync a folder on the host machine to the guest machine, allowing you to continue working on your project’s files on your host machine, but use the resources in the guest machine to compile or run your project.
By default, Vagrant will share your project directory (the directory with the Vagrantfile) to
/vagrant. To set your own synced folders you use the namespace:
A provider is the virtualization software. Vagrant supports VirtualBox and Hyper-V out of the box.
You can configure your provider to allocate specific resources to your virtual machine and to behave in specific ways when the Vagrant machine is launched.
To configure a provider we use:
It is also important to note that Vagrant aims for portability. If you use a Vagrant script with provider settings of a provider you don’t have Vagrant will ignore that part. Also a single Vagrant script can have multiple provider settings.
Provisioning allows you to automatically install software and alter configurations during the
vagrant up process.
To create provisions we use:
SSH into your VM
To SSH into a vagrant machine is easy. After starting up your machine with
vagrant up just execute
vagrant ssh. No credentials. You just have to make sure you are in the same folder as the Vagrantfile and that the virtua;l machine is actually running.
Vagrant can seem as a bit too much in the beginning but if you get a hold of it you’ll save a lot of time in terms of setting environments for development or testing.
For example; you can check out my Vagrant setup for PHP development on my Github. All you have to do is clone and run
You can also check the vagrant cloud for LAMP, WAMP and MEAN stacks to quickly setup your environment.