Easy Rails with Vagrant + VirtualBox on Windows

There are plenty of posts describing on how to install Ruby and Rails on Windows. But there are two main approaches:

  • Install Ruby and Rails directly, I mentioned that before in Installing Ruby On Windows – The Right Way. The main disadvantage of this approach is that is limited to Ruby 1.9.x on Windows (well, last time I tried – two weeks ago). You can install Ruby 2.x+, but Rails won’t work. I don’t know why, but after googling for a couple of hours I found that it’s a common problem.
  • Second way is to install Vagrant + VirtualBox. Folks say it’s easy and the best way to go. I can’t say it isn’t the best way to go, but it’s not easy as I really expected. So in this post I’ll cover that approach so you guys hopefully can save a couple of hours and use them for the having fun with Ruby and Rails.

Why it should be easy, and why it’s not? Reason one – VirtualBox. It’s widely used, but it can refuse to work on your computer. If it doesn’t work, try to uninstall VirtualBox and Vagrant, and reinstall them again. Install VirtualBox, and then Vagrant. But let’s start from the beginning.

1. Go and grab VirtualBox. If it didn’t work yesterday, it can work today. Versions are updating pretty often. If it doesn’t work, try to rollback few versions back. Try to disable (uninstall) antiviruses. I have to admit that after lots of attempts I’m still unable to run it on one of my computers. I don’t know why, may be complete Windows reinstall can help. But it works pretty well on another computer, and I’m able to use it.

2. Install Vagrant. You can download it from the website http://www.vagrantup.com. “vagrant up” is the command you’ll use frequently to initialize your virtual machine.

3. Download rails dev box.  Don’t clone repository.  If you use msysgit for Windows, git can be configured to checkout files with CRLF endings and bash scripts won’t work in that case. So just download zip-file and unpack.

4. Switch to directory with Vagrantfile and run “vagrant up” (I use Far commander for file operations, if you don’t have any kind of file manager, you will need to run cmd.exe before this step). It will take some time do download and configure Ubuntu box.

It should go pretty well, but I had few BSODs on another because of VirtualBox. After that run “vagrant ssh” to check if you’re able to log on to linux machine. If everything’s fine, you’ll be able to see welcome message. You can type “exit” to exit from linux back to windows console (virtual machine will keep on running), but don’t do it now, we still have some things to do.

5. Install rails with command “sudo gem install rails”.

6. When rails is installed we can go and create our rails app. /vagrant folder in linux is now mapped to our Windows filesystem. Let’s switch to this folder in linux and create an app there:

cd /vagrant
mkdir rails
cd rails
rails new devhq
cd devhq

We created /vagrant/rails directory, then created app called devhq in this directory and switched to this directory.

7. Now it’s a good time to run newly created app inside of a linux box and open it in a browser on Windows. Type “rails server” and go to “http://localhost:3000”. Oops! Doesn’t work. The reason is… Rails 4.2. It listens on localhost:3000 by default instead of as before. So you have to stop the server (Ctrl+C) and run “rails s -b”. Now you can open your app in the browser.

It’s done! Now you can edit your app with your favorite editor on Windows and run your app on Linux. Cool, isn’t it? Here is the list of commands you should also know:

vagrant ssh – you know already, use this command to log in to your linux box

vagrant up – run the virtual machine

vagrant halt – stop the virtual machine

vagrant reload – reload vagrant configuration, remap file system, remap ports, etc.. Can be useful if you have any errors.

vagrant provision – run provisioning scripts again

sudo reboot – try to reboot linux machine if you have any errors

su – switch to root user. The password is “vagrant”


4 thoughts on “Easy Rails with Vagrant + VirtualBox on Windows

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s