Introduction to Vagrant for Rails Developers

Written by

Vagrant is a tool for creating virtual machines on your development machine. It manages getting them to a known state (database installed, ruby installed), and then running code on them. This lets you have an instant, complete development environment that acts just like production environment.

One of the coolest things is that vagrant creates a tunnel between your local files and the inside of the virtual machine:

Vagrant tunnel

There are only a few commands you need to know to be productive with vagrant: vagrant up, vagrant ssh, and vagranthalt`.

Vagrant command

Getting Vagrant started

Before you can run vagrant, you’ll need to download and install a few things:

  • The Vagrant tool – This is the actual vagrant tool itself. It manages virtual machines
  • Virtualbox – Virtualbox is the virtual machine where your code will run

If you have an app that already has a Vagrantfile in it, it’s very easy to get started. At the command line change into the folder with the Vagrantfile and run:

vagrant up

This starts the virtual machine in VirtualBox and runs scripts to get the environment setup.

Getting stuff done in Vagrant

You want your code to run in the virtual machine, so you need to get into that access to that virtual machine. This happens over ssh (just like you were connecting to a remote server). You can access the command line of the virtual machine by running:

vagrant ssh

Vagrant Setup

Now you’re inside the virtual machine. Your code is available at /vagrant. You can get to your code by running

cd /vagrant

From here you can do all of the things you’re used to with rails:

bundle exec rake db:migrate
bundle exec rails server

Shutting down vagrant

When you’re done, it’s easy to get out of vagrant, just exit:


And then you can shutdown vagrant by running:

vagrant halt

This turns off the virtual machine, forcing it to give back all of its memory and claim to the CPU. It’s good practice to halt the virtual machine when you’re not using it.

Coming back to vagrant

When you’re ready to do more development, just repeat the same process:

vagrant up  
vagrant ssh  
cd /vagrant  
# ... Important work   
vagrant halt  

Learn more, learn differently

This guide should give you a good reference to get started with vagrant but I recommend also checking out:

Comments or Questions? Contact Nick @nixterrimus on twitter.

Nick is a software engineer, geek, web enthusaist, open source contributor, home automation tinkerer, ocean admirer and all around general optimist living in San Francisco. Want to get in touch about professional matters? Nick Rowe is also available on LinkedIn.