I do a lot of C# web development which means I have to work with Visual Studio on Windows. I usually grab the Mac to do my Ruby on Rails development, but it would be easier not to switch every time I have to do C# and Ruby development.
A while ago I tried several things getting a fast Rails experience on Windows, but it failed. It was just too slow … until now! In this post I’d like to share my current setup which I plan to use as long as it pleases me.
What are we going to do?
We are going to install Vagrant which uses VirtualBox to build a complete development environment. Also we will create and use some recipes for Chef to automatically install and configure the development environment. Think of installing Ruby, PostgreSQL, and MySQL etc. automatically.
Installing Vagrant and VirtualBox
First let’s setup our Windows machine with the needed software.
- Install VirtualBox
- Install Vagrant
- After reboot add the VirtualBox folder to your PATH variable
(something like:c:\Program Files\Oracle\VirtualBox
) - Install a base box (I used Ubuntu precise 64 VirtualBox) with
the following command
vagrant box add precise64 https://files.vagrantup.com/precise64.box
Prepare your Rails project
Next we have to configure our project to create a box from the base box and make it install all our goodies we need to run our Rails application.
Setup Chef (Librarian)
I’m using a gem called librarian-chef which actually is a bundler for your Chef recipes.
To install run the following from the root of you project:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
WUT?! gem install librarian-chef?
I know we are trying to create a non-Ruby Windows solution but we do need this gem to work on Windows to complete our setup with Chef. If you really don’t want Ruby being installed on Windows, you should do the above steps on UNIX or OSX and also have to commit the cookbooks. This means we’re back at 1900 where everybody stuffed there vendor folder with code they found… If you do want to perform this on Windows and haven’t already installed Ruby, you should do this now (I suggest RubyInstaller).
Configure Chef
The command librarian-chef init
created the file chef/Cheffile
where we can add
our cookbooks. Mine contains the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
As you can see I’ve setup Git, Ruby with rbenv, SQLite, MySQL, PostgreSQL and node.js for the asset
pipeline. The apt
cookbook is for running apt-get dist-upgrade
and the cookbook
database
will be used later to create users to access the databases.
You could skip one or two of the database servers if you like, but it can’t harm if you do install them all and don’t use them.
Note: the database
cookbook is provided with additional actions to set superuser privileges
to PostgreSQL and MySQL.
Downloading the cookbooks
Now that we have defined our cookbooks we need to run our final command so that Chef downloads them. Just
as you would do with bundler we now run the following command from our chef/
folder
1
|
|
Adding our custom recipes
I had some difficulties with running bundle install
because the Vagrant user didn’t
have permissions on the rbenv gem folder, so I had to add a custom recipe for this. Also I’ve added
a recipe to modify the pg_hba.conf
file of PostgreSQL so that we can connect with the virtual
machine. Finally I’ve created a file to add our database users with full permissions. Here are the 3
files I added:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
1 2 3 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Adding our role
Next we have to create a role that defines what recipes should be executed in what order. Create the file
chef/roles/rails-dev.rb
containing the following content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
If you removed some cookbooks from the chef/Cheffile
you should also remove them here.
Setup Vagrant
Next we need to create a Vagrantfile
that contains the configurations for creating a box
from the base box. Create the config file by running from the root of you project:
1
|
|
Note: precise64
is the name we used with the vagrant box add
command from step
4 above.
Also we want to add the virtual machine folder to our .gitignore
file:
1
|
|
Modify the Vagrantfile
Here’s the stripped version of how my file looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
Ready to rock!
Alright we’re ready. We now can boot up Vagrant which will take some time the first time because it will create a new box from the base box and install all the cookbooks. From then you’re able to connect with the box and use it just like you would do on UNIX or OSX.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
What we’ve created
A VirtualBox with Ubuntu precise 64 installed and containing the following software/configurations:
- Software:
- rbenv with ruby-build
- ruby-2.0.0-p247
- Git
- SQLite
- MySQL with user:
rails
with a empty password - PostgreSQL with user:
rails
with a empty password - node.js
- Ports:
localhost:3000
goes tovagrant-box:3000
localhost:3306
goes tovagrant-box:3306
localhost:5432
goes tovagrant-box:5432
Develop workflow
If you have to run specific Ruby (on Rails) tasks like rspec
or
bundle exec rails g migration
you should do this within the ssh session of your box.
Editing files can be done on your Windows machine, because the root of you project is directly binded to
the /vagrant/
folder on the box.