Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Vagrant Cheat Sheet + Get Started with Vagrant

Jun 08, 2023 • 0 Minute Read

Please set an alt value for this image...
  • Software Development
  • Cloud
  • Learning & Development

We’re back with another cheat sheet – this time for Vagrant! We define some common terms, give you a rundown of the most-used commands, and even include a sample Vagrantfile that provisions a basic CentOS 7 web server to experiment on.Check out the cheat sheet and get a basic Vagrant tutorial inside!

Cheat Sheet

Vagrant Cheat Sheet

Get Started with Vagrant

Prerequisites

  • Vagrant! Vagrant is easy to install, and the process should only take a few minutes.
  • VirtualBox
  • Linux, Mac, or Windows! Vagrant can be used on any operating system, and the commands are the same.
  • Basic command-line knowledge of your operating system. Since this guide is OS-agnostic, we will use generic terms like “change into the src directory” instead of using any specific commands; you should know how to perform the following commands on your system:
    • Change directories
    • Open text files
    • Add text files
  • Windows users should also have PuTTY installed.

Create Your Vagrant Project

  1. Create a directory from which we can work. We’ll use the generic name vagrantdir in this guide when referencing this folder.
  2. From the command line, move into the vagrantdir folder.
  3. We plan on creating a CentOS 7 guest machine. To do this, we need to find the appropriate box to use. Public boxes can be found here. In this instance, we’re using the centos/7 box, provided by CentOS. Beyond VirtualBox, this box is also compatible with VMWare and libvirt providers. Add the box:
     vagrant box add centos/7
  4. We can now create a basic Vagrantfile:
     vagrant init --minimal 
    Should we run vagrant init without the --minimal flag, our Vagrantfile generates with instructional commentary. For the sake of this guide, we chose not to include the additional commentary to provide us with a cleaner Vagrantfile to work from.

Configure the Guest Machine via the Vagrantfile

Basic Settings

  1. Open the newly-created Vagrantfile in your chosen text editor. Currently, it should resemble the following:
     Vagrant.configure(2) do |config|   config.vm.box = "base" end
    As it stands, all this Vagrantfile does is define the Vagrant configuration version as 2 (Vagrant.configure(2) do |config|) and sets the guest machine to work from the “base” box (config.vm.box = "base"). Note that the config at the start of the box configuration references directly back to the |config| value in the version line. All our configuration settings will start with config.
  2. Update the box to use the CentOS 7 one we just added:
     Vagrant.configure(2) do |config|   config.vm.box = "centos/7" end
  3. We also want to define the hostname, myhost, for this box. We can do this by simply adding config.vm.hostname = "myhost" to our code:
     Vagrant.configure(2) do |config|   config.vm.box = "centos/7"   config.vm.hostname = "myhost" end
  4. We can also configure networking settings in Vagrant. For this example, we’re going to set an IP address within a private network range:
     Vagrant.configure(2) do |config|   config.vm.box = "centos/7"   config.vm.hostname = "myhost"   config.vm.network "private_network", ip: "192.168.50.10" end
    If you are already using the private IP 192.168.50.10, update the Vagrantfile to use a different address accordingly.

Sync Folders

In Vagrant, we can sync a folder to a folder within our guest machine. Since we’re creating a sample web server, we want our local directory to hold our website’s files.
  1. Create a directory under your environment’s directory, called src.
  2. To configure the src directory to sync to the /var/www/html directory, use the synced_folder option:
     Vagrant.configure(2) do |config|   config.vm.box = "centos/7"   config.vm.hostname = "myhost"   config.vm.network "private_network", ip: "192.168.50.10"   config.vm.synced_folder "src/", "/var/www/html" end
  3. If so desired, add an index.html file to your src directory with whatever contents you wish.

Set Up Provisioning

Vagrant is able to pair with a number of provisioning tools to configure your server upon creation. The most basic uses simple shell scripting. This is what we’ll be using. Specifically, we will be using Ruby syntax to feed a series of commands into a variable, and then call that variable in the line that configures our provisioning.
  1. Create a $samplescript variable that installs, enables, and starts the Apache. Place this before the Vagrant configuration stanza:
     $samplescript = <<SCRIPT yum install -y httpd systemctl enable httpd systemctl start httpd SCRIPT Vagrant.configure(2) do |config| [snip]
  2. Add the provisioner configuration line to the main code block:
     Vagrant.configure(2) do |config|   config.vm.box = "centos/7"   config.vm.hostname = "myhost"   config.vm.network "private_network", ip: "192.168.50.10"   config.vm.synced_folder "src/", "/var/www/html"   config.vm.provision "shell", inline: $samplescript end

Configure VirtualBox

Finally, we can use our Vagrantfile to configure VirtualBox. This is done by adding a code block specific to VirtualBox within the main section of our Vagrantfile:
config.vm.provider "virtualbox" do |vb|  vb.memory = "1024"  vb.cpus = "2"end
Notice how the format is similar to the set up of the Vagrant configuration line, only instead of defining and then prepending config to the configure, we’re using vb (for VirtualBox; note that this value does not have to be the same).

The Final Vagrantfile

Your final Vagrantfile should resemble the following:
$samplescript = <<SCRIPTyum install -y httpdsystemctl enable httpdsystemctl start httpdSCRIPTVagrant.configure(2) do |config|  config.vm.box = "centos/7"  config.vm.hostname = "myhost"  config.vm.network "private_network", ip: "192.168.50.10"  config.vm.synced_folder "src/", "/var/www/html"  config.vm.provision "shell", inline: $samplescript  config.vm.provider "virtualbox" do |vb|    vb.memory = "1024"    vb.cpus = "2"  endend
Save and exit the file.

Vagrant Up!

With our Vagrantfile finished, return to the command line and ensure you're in the vagrantdir directory. From here, we can start our virtual machine. Starting a virtual machine in Vagrant is easy. Just run:
vagrant up
Wait for the provisioning process to finish. When finished, you can check to see if the web server is working by visiting 192.168.50.10:80 in your browser.We can also SSH into our server. This process depends on your workstation computer.

SSH from Linux and Mac

To SSH into your new virtual machine from a Linux or Mac workstation, simply use:
vagrant ssh
Feel free to take this moment to look around the CentOS environment. exit back to your workstation when done.

SSH from Windows

Since you cannot SSH into your virtual machine from the command line within Windows, we need to use an SSH client, such as PuTTY.First, from the command line, run vagrant ssh-config to retrieve the IP address and port of the virtual machine:
vagrant ssh-config
Next, take this information and input it into PuTTY; the default username and password for Vagrant-provisioned virtual machines are both vagrant. From here, we should be able to connect to your server.Take this moment to explore the guest machine. Close the connection when finished.

Stopping, Starting, and Destroying

Congratulations! You’ve just spun up your first Vagrant box and logged in! Usually, you would now continue to use your Vagrant environment for whatever project you’re working on. But what about when you’re done for the day? You can turn off your machine in two ways, vagrant halt and vagrant suspend.

Halt

To halt the machine – that is, attempt a graceful shut down, wherein the machine turns off the same as it would if it were a physical machine, run the following command:
vagrant halt

Suspend

A suspended machine is a machine that is turned off but not shut down; essentially, it is saved in the exact state that it was in when the suspend was run. To suspend a machine, run:
vagrant suspend

Resume

To start a previously-halted or suspended machine, run:
vagrant resume

Restart

You may also need to perform a restart on your machine – whether to ensure your system settings persist through reboot or otherwise. Use the following command to restart the machine:
vagrant restart
A restart is the same as running vagrant halt, followed by vagrant resume.

Destroy

If you’re permanently done with your Vagrant environment and want to fully remove it from your system, use:
vagrant destroy
When prompted, confirm that you wish to destroy the machine(s).

Conclusion

Vagrant is a powerful and highly-useful tool for ensuring your development systems run the same regardless of what computer they are running on. Feel free to adapt and expand the example Vagrantfile as much or as little as you wish and be sure to download the cheat sheet for a quick-reference if you get stuck! Keep practicing, and you’ll be a Vagrant expert in no time.