Working with Vagrantfile

In this lesson, we'll learn more about Vagrantfile and dive deeper into its configurations.

So far, we’ve just spun up our Vagrant VM.

Now, let’s get our hands dirty with Vagrantfile.

Vagrantfile

Vagrantfile is a configuration file that Vagrant creates during the vagrant init <box name> process. This file consists of the configuration for your VM. This configuration file is defined completely in the Ruby language. Don’t worry if you don’t know the Ruby language, as it is quite easy to use. When we run the vagrant up command, Vagrant locates this config file in the current directory and loads the guest machine using the configuration defined in the file.

We can do a lot more than run a default configured VM. Let’s look at some of our options.

Customizing Vagrantfile

Now, let’s spin up our Vagrant VM again, but this time, with a new image and in minimal mode.

When we first built our VM, we ran the command: vagrant init ubuntu/xenial64. By running this command, we successfully generated our Vagrantfile. But, in that config file, there are a lot of comments, and it’s quite long, isn’t it? Just forget about those long comments for now! We’ll build something cleaner.

You remember boxes, right? We’ll add a box first, and, then we’ll do init in --minimal mode.

Adding a box

Make sure you’re in the Vagrant directory that we created earlier. Then, run the command below.

$ vagrant box add centos/7

This time, we’re running a centos image - A Redhat Distro.

Run Cleaner Mode

Run the following command to initialize the Vagrantfile in minimal mode.

$ vagrant init --minimal

Now, if you open up your Vagrantfile, it’ll look something like this.

Vagrant.configure("2") do |config|
  config.vm.box = "base"
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "1024"
    vb.cpus = "2"
  end
end

Note, the config.vm.box is set to "base", i.e., we’re working with a “base” box. We need to change this. Let’s add the box that we’ve created in the previous step.

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "1024"
    vb.cpus = "2"
  end
end

Now this config file tells us that it is running the Vagrant configuration version, configure("2"), and the box or image we’re using is config.vm.box = "centos/7".

Providers

Let’s learn about the code that I’ve added below.

config.vm.provider "virtualbox" do |vb|
    vb.memory = "1024"
    vb.cpus = "2"
  end

Vagrant needs providers to run their VMs. In our case, we’re using VirtualBox, so we have to define our provider configuration using the config.vm.provider method. Without this block of code, Vagrant won’t be able to boot up our Virtual Machine.

Explore more about providers here: Providers.

The information discussed so far is sufficient enough for you to tell what’s really going on in the config file.

Updating Vagrantfile

To update the Vagrantfile, just replace base with the box name from the previous step.

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
end

Now, let’s declare a hostname for our Virtual Machine.

A hostname is a unique name for a computer or network node in a network. Hostnames are specific names, or character strings, that refer to a host and make it usable for the network and users.

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.hostname = "server.example.com"
  config.vm.provider "virtualbox" do |vb|
    vb.memory = "1024"
    vb.cpus = "2"
  end
end

Now, let’s learn about networking.

Get hands-on with 1400+ tech skills courses.