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 0.0.0.0:3000 as before. So you have to stop the server (Ctrl+C) and run “rails s -b 0.0.0.0”. 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”

Advertisements

Installing Ruby For Windows – The Right Way

It’s unbelievable. There are plenty of manuals available on the Internet, but there is no right one. Installing Ruby and Rails on Windows is pain, comparing to Linux and OS X. So in this topic I’ll cover it step by step. Hope it can help you to save some time.

How to install Ruby and Rails – the right way:

1. Install Ruby with Ruby Installer. Pick your version. I use the latest one (2.0 when I’m writing this post). Pick your operating system: 32 or 64 bit. In this guide I use x64 version of Ruby installed in C:\Ruby200-x64

2. Make sure you have updated PATH variable pointing to: “C:\Ruby200-x64\bin” (without quotes). Press Win+R, type cmd, then type irb. You must have irb in your path. Type ruby -v to show ruby version.  Note: if you use Far Commander or other commanders you might need to restart this application.

3. Ruby is installed, it’s time to install gems. Before you can install gems, you need to install Development Kit. Go to Ruby Installer Downloads page. Pick development kit for your version of Ruby and operating system. Unpack development kit to C:\RubyDevKit

4. Change directory to C:\RubyDevKit with your favorite file manager (mine is Far Commander) or via cmd, and type ruby dk.rb init

5. Open config.yml file and add the following line right after “—” (triple dash)  if it’s not there already:
– c:/Ruby200-x64 (don’t copy and paste, just type dash, space and then type your path)

6. Run ruby dk.rb install . Here is the sample output:
[INFO] Updating convenience notice gem override for ‘c:/Ruby200-x64’
[INFO] Installing ‘c:/Ruby200-x64/lib/ruby/site_ruby/devkit.rb’

7. Now you can install gems. Try installing hipchat gem: gem install hipchat . If everything’s fine, you’re almost done. Try gem install rails .

8. If you still have errors installing gems, try

gem update --system

9. Now you need to update your SSL certificates. You don’t have any SSL certificates at the moment, so there is a big change that some of your gems won’t work (if they access a server by https for instance, like hipchat). Now it’s time to install these certificates. Run this script to download cacert.pem. Make sure you have added SSL_CERT_FILE variable to your environment pointing to downloaded cacert.pem.

Now you’re done! Happy coding!

TODO: powershell script to automate this job.