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!
Get Started with Vagrant
- Vagrant! Vagrant is easy to install, and the process should only take a few minutes.
- 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
srcdirectory” 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
- Create a directory from which we can work. We’ll use the generic name
vagrantdirin this guide when referencing this folder.
- From the command line, move into the
- 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
- We can now create a basic Vagrantfile:
vagrant init --minimal
Should we run
vagrant initwithout the
--minimalflag, 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
- 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
Vagrant.configure(2) do |config|) and sets the guest machine to work from the “base” box (
config.vm.box = "base"). Note that the
configat the start of the box configuration references directly back to the
|config|value in the version line. All our configuration settings will start with
- Update the box to use the CentOS 7 one we just added:
Vagrant.configure(2) do |config| config.vm.box = "centos/7" end
- 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
- 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.
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.
- Create a directory under your environment’s directory, called src.
- To configure the
srcdirectory to sync to the
/var/www/htmldirectory, use the
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
- If so desired, add an
index.htmlfile to your
srcdirectory 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.
- Create a
$samplescriptvariable 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]
- 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
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 = <<SCRIPT yum install -y httpd systemctl enable httpd systemctl start httpd SCRIPT 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 config.vm.provider "virtualbox" do |vb| vb.memory = "1024" vb.cpus = "2" end end
Save and exit the file.
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:
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:
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:
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
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:
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:
To start a previously-halted or suspended machine, run:
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:
A restart is the same as running
vagrant halt, followed by
If you’re permanently done with your Vagrant environment and want to fully remove it from your system, use:
When prompted, confirm that you wish to destroy the machine(s).
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.