Vagrant 101: Basic Virtual Machine Customizations

Vagrant is a powerful too, and it’s power comes from the modifiable, programmable configuration file. The configuration file allows us to define how the guest virtual machine that will be launched by vagrant should look like. If you haven’t already read my very simple Vagrant 101: Getting Started guide, please read it now.

For our examples we will focus on Virtualbox, as I also use it for my day-to-day tasks and it is easily available for all platforms. There are many ways you can customize virtual machine, but imagine keeping your virtual machine customization with your project under version control. Can you see the power it will give you over your development? Can you imagine the possibilities?

Enough talk, let’s see how we can actually do it in Vagrant. Vagrant stores all of the vagrant configuration for a project in Vagrantfile which we already added to our version control. Let’s go it and modify different aspects of our Virtual machine. All the virtual machine customization is done under config.vm.provider section. As we are using virtual box our provider should be virtualbox, so our section will look like following:

config.vm.provider "virtualbox" do |vb|
    # VM Customizations go here

Search for the line config.vm.provider “virtualbox” and add the following customization options.

Modify CPU Configuration

You can modify number of CPUs available to your virtual machine by specifying the the number in this section. For example if you want to assign 2 CPUs to your VM, you can use the following example customization, or else specify your own number instead of 2.

vb.customize ["modifyvm", :id, "--cpus", "2"]

Modify Memory Configuration

Just like you modified your CPU configuration, you can also modify your virtual machine memory configuration, for example if your want to assign your virtual machine 2 GB memory, you can use the following customization option. Remember have to provide the memory size in MBs.

vb.customize ["modifyvm", :id, "--memory", "2048"]

Disable USB 2.0

As I mostly use Virtualbox to do development and don’t need USB access, I can disable it safely, this might be true for everyone at least in 90% of the cases. Disabling USB support on your guest virtual machine is very simple and it requires the following 2 customization options.

vb.customize ["modifyvm", :id, "--usb", "off"]
vb.customize ["modifyvm", :id, "--usbehci", "off"]

How to apply the changes?

Find the status of your virtual machine using status command.

vagrant status

If your virtual machine is already running, use the reload command.

vagrant reload

If your virtual machine is not running, just use the up command.

vagrant up

What else can be customized?

By now you might be wondering what aspects of your virtual machine you can customize. Almost all of them, to see the list of options, use the following command (tested on linux)

vboxmanage modifyvm

The above command will show you all the options available, you just need to use the option in Vagrantfile in the same way we have modified the CPU and memory above and provide the correct values depending on the feature you are customizing.

Complete section.

Just for your verification and to clear any confusion you might have, your config.vm.provider section should look like following:

customize.vm.provider "virtualbox" do |vb|
  # Assign 2 CPUs
  vb.customize ["modifyvm", :id, "--cpus", "2"]
  # Assign 2 GB Memory
  vb.customize ["modifyvm", :id, "--memory", "2048"]
  # Disable USB
  vb.customize ["modifyvm", :id, "--usb", "off"]
  vb.customize ["modifyvm", :id, "--usbehci", "off"]

What’s next?

In the next few articles I will write about shared folders, networking, sharing and provisioning which are other powerful options vagrant provides you. Let me know in comments if you have any other questions regarding these basic VM customziations?

Vagrant 101: Getting Started

Although late to party, but finally I joined. Until now I had been manually managing my VMs in Virtualbox without Vagrant, but that process was very tedious and slow. Find the image, download it, import it, boot the machine, configure networking, assign the IP addresses, setup the machine. But with Vagrant its as easy as:

Note: Dont’ worry about what it means, I will explain in later chapters.

vagrant init <url to box>
vagrant up
vagrant ssh

As you can see how easy it is to get your new sandbox environment up and running within few minutes. Its not just the basic setup, but Vagrant allows you to completely setup your new VM with everything you need in your dev environment.

The great part is you don’t have to leave your local machine to write code. Confused? Vagrant actually mounts your local folder (where you ran vagrant init) in your VM, so what ever changes you make on your local machine will be available in your vagrant box.

Let’s Dive into Vagrant

Enough of the praising, let’s dive in and experience the power of vagrant. First you need to find Vagrant boxes (VM images), so you can use them for your development. The following instructions assumes that your are using some flavor of linux (the concept is same for other OS, you just have to convert the commands as per your OS).

The best place to find the Vagrant boxes are Vagrant Cloud. Once you find the suitable box for your development. Create your development directory and do the vagrant init (remember git or hg init?) with the the name of the box from Vagrant Cloud. See the commands below:

mkdir web-app-dev
cd web-app-dev
vagrant init hashicorp/precise32

This will create the Vagrantfile and add the box name to the Vagrantfile. You are ready to launch your first vagrant controlled sandboxed dev environment. Just run the vagrant up to get the VM up and running. See the example below (run the command in same folder where you ran vagrant init):

vagrant up

This will download the box (VM image) from the Vagrant cloud, after downloading it will boot the box, do the provisioning and then it will mount your development folder on VM at /vagrant. After few minutes your new VM will be up and running.

Once the VM is up, you can login to the VM by just doing a vagrant ssh. Yes it is this simple, see the example below:

vagrant ssh

Now that you are logged into the VM, you can configure it for your development manually (for now). Later on I will write about how can you automate the setup part too, based on your development needs.

Checkout the folder share, by creating files in /vagrant folder on your new VM and then go back to your local folder and see them appear there. Similarly you can create files on local and they will immediately appear in your VM.

Where is the Version Control?

You can add Vagrantfile to your version control so you can replicate your Vagrant setup to other devs or on other locations too. Don’t add other Vagrant files to version control, for now it is just Vagrantfile. Simply just run the following commands (from same folder where you ran vagrant init):

git init
git add Vagrantfile
git commit -m "Add Vagrantfile"

What’s next?

I tried my best to make this getting started guide as simple as I could, and not be confusing for the new developers who are still trying to figure out how linux works. Later on I will write about provisioning and advanced networking setup in Vagrant.

If you have any questions, confusions feel free to ask them in comments and I will try to help you as much as I can.