Skip to main content
Development Tools:

How To Setup Vagrant For Rails Development

Episode 53 · April 29, 2015

Learn how to use Vagrant to set up a headless virtual machine for Ruby on Rails development


Earn a free month

Vagrant is a tool that allows you to run the same operating system in development as you will use in production using a virtual machine. You might be running Mac, Linux or Windows, and you can actually run Linux inside of any of those. The same version of Linux that you'll be running on the server. That means that in development, you can know that your code that runs there will also run perfectly in production. We're going to talk about how to set Vagrant up, and configure that. Real quick before we get started, I want to mention that all of the commands that we use here and the text will be in the notes on the website under "Guides", "Using Vagrant for Rails Development", and those will be the latest version of these configurations, and this changes a lot, so make sure that you take a look there. The first thing that we need to do is set up Vagrant, and that's going to be two things, the first is going to be Vagrant itself, and the second is going to be VirtualBox. You wanna download whichever. This is for the operating system you're using, so for me, I'm using Mac OS X, and I will download these two and install them, and when you're done with that, we'll come back to the tutorial to set up our application. Now, once you've got virtual box and Vagrant installed, you can install the two plug ins we're going to need to set up Vagrant. Here we're going to type vagrant plugin install vagrant-vbguest, and then we're going to do vagrant plugin install vagrant-librarian-chef-nochef. This is going to be a version of the vagrant librarian plugin that should work on windows as well. Then, once you've got both of these installed, we can dive into our rails application that we want to use vagrant with, and I'm going to use one called cd code/landingpage and then inside of here you can just type vagrant init. This will create the vagrant file for us, and then we're also going to touch Cheffile, pay attention to the capital "C" there because that's important. Now, we can open up that Cheffile and paste in some code. I'm just going to grab this from the example here, and you'll paste that in, save it and you can do the same thing with the vagrant file. We'll go grab that from here. Don't worry about overriding everything in there, feel free to take a look and read about all the options, and take a look at their example config, we're going to replace that with ours, and it's going to install the Ubuntu 14.04 trusty tahr version 64-bit. If you want the 32-bit, feel free to change the 64 to 32 bit, if you know that your operating system on your computer doesn't support that go right ahead. We're going to set it to two gigs of RAM, to use for memory so we have plenty of space, you're also free to tweak that as much as you like. We're going to take the port 3000 in our computer and forward that over to vagrant. A few of the other things we've got here is that we're going to use the aptitude recipe, we're going to install nodejs for the asset pipeline, we're going to use ruby build to build version 2.2.1 of ruby, it's also going to install the bundler gem, then rbenv is going to be used to install those, and we're gong to also include vim and MySQL server and client, so we'll be able to build the MySQL gem as well as talk to MySQL server that will run inside of our vagrant virtual machine. You're free to configure these to maybe be PostgreSQL for example, and do with this what you like. There's plenty you can do with this and take advantage of that as well. Back in your terminal, you can just run the command vagrant up and you'll be using this every time you want to start your vagrant virtual machine to work out. Now the first time that this happens it's going to start downloading the Ubuntu trusty 64 ISO, so this is the iamge of the operating system that's going to download. It's already preconfigured for virtual box and vagrant, and that's going to be great in the sense that we just download it and it's pretty much good to go, we'll have to install some things like ruby on it, but vagrant knows how to do that, so we just let this command run and it will download it, it won't have to download it in the future once we've got a cache to our hard drive, and once you let this run, as long as there are no errors, you'll be able to ssh into the virtual machine. I'm going to let this run and we'll be back in a few seconds.

We're back, now that you see the output from vagrant setting up our virtual machine, you can see if you look at these logs, there's a whole bunch of stuff that happens, it sets up MySQL, your user, installs packages, ruby, all of that will happen, and then eventually you should get a bunch of green messages and report handlers complete and chef run complete is the important part here, you'll see that has finished, and once chef is done, basically your virtual machine is all set up.

Now, anytime you want to work on your rails application, you can jump into your rails folder and you can say vagrant up and we just ran that command, and that will make sure that the virtual machine is started, so we did that already, and then after it started, you type vagrant ssh. This will drop you right into a prompt inside of the virutal machine. this will ssh in for you, and you'll get the exact same log in as you might expect from a virtual server somewhere. What you want to do is type cd /vagrant and this is going to be the directory you work out of. If you list the files in here, you'll notice that these are actually the exact same files inside my landing page rails app, so where you did vagrant init and vagrant ssh, from those exact same file and folders are going to be the ones inside of this /vagrant folder. So it automatically mounts this from the host into the guest virtual machine for you. Once you're in here and you've gone through the process, you should be able to have the bundle command, if you don't, you can always gem install bundle, but you also have your ruby version that you specified in the vagrant file, so 2.2.1 is what I have here in mine, we can open that up and we'll see in the chef.json config, we've installed the ruby 2.2.1 under the vagrant user, and then I've also told it to install the bundler gem, so since we've got the bundler gem, we can just run bundle like we normally would inside the vagrant folder. This installs all your gems and then you can also run rbenv rehash so that any of the executables like the rails executables or rake or annotate or any of those other executables that come from gems are available and that was just a piece of functionality the rbenv requires you to do.

From here, you can just run rails s like normal, and you'll see that this will start a rails server, and it will listen on port 3000, just like you normally see that, and this time in your browser, if you open up localhost:3000, you'll get nothing. That means that we can run bundle just like normal to install all of our gems inside the virtual machine. You won't need to install these gems inside your development computer anymore because they can all be self contained inside the virtual machine, and that means that now we have all the gems, we have everything set up, you can run rake db:migrate, and all of those database commands here, and you're also able to run your rails server. This one, you can run rails server like normal, but I'm going to recommend you say rails s -b The reason is because that's a public facing interface, and the way that virtual box passes over localhost:3000 in your browser through that is going to make sure that virtual machine connects everything and it listens to that address properly. You can see here that we got our rails application to respond, and that means that vagrant has been fully connected, so we've got localhost:3000, but this is actually routing to our virtual machine that vagrant started for us. You can take this vagrant file and the chef file and commit that to your git repository so that anyone else on your team or you in the future can set up your virtual environment and work with no problems in theory if nothing changes with these recipes from chef. If you do run into any issues with Chef, please post a note in the comments of what the issue is, and maybe if you find a solution on how to fix it, this seems to be changing a lot, so your feedback on kind of all of those things that change would be great and I think I can help use that to keep this up to date.

I hope you enjoyed this episode, and I've got to let you go because I have a rails application to debug.


Subscribe to the newsletter

Join 31,353+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.