Vagrant VM for starting a Rails project


This is something off-topic for this blog, but after spending several hours setting up an environment for developing and testing a Ruby on Rails project, I’d like to share my solution.

I recently had an idea for a small web-project, for that I’d like to use Ruby on Rails. From previous attempts of using Rails, I knew that Ruby and Rails are updated regularly and that the setup of the environment might be tricky. And using my Mac not only for web-development, I did not want to have many versions of Ruby, Python, whatnot, in parallel on it. And as I read about Vagrant some weeks ago, I wanted to give it a try.

It took quite some time to set-up the provisioning script properly. I tried using Chef, but it made things more complex and I appreciated the simplicity of a shell script.

Requirements

My configuration works fine with the below software. From my past experience, any new update might break everything.1

  • Vagrant 1.8.1
  • Virtual Box 5.0.10

Virtual Box is installed automatically when first firing up Vagrant, so you should only need to install Vagrant (which is convenient on OS X).

You can also install the Guest Additions, but I don’t see this as a requirement:

vagrant plugin install vagrant-vbguest

Provisioned Software

With my Vagrantfile and the provisioning script vagrant.bootstrap.sh you will get a VM with Ubuntu 15.04 the following software:

  • git (latest version at apt repository)
  • rbenv (latest version at apt repository)
  • Ruby 2.3.0
  • bundler (latest version at gem repository)
  • Rails 4.2.6

The versions of Ruby and Rails can easily be changed with a variable within vagrant.bootstrap.sh. All dependencies are installed via apt-get:2

sudo apt-get install -y build-essential bison openssl libreadline6 libreadline6-dev zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev autoconf libc6-dev ssl-cert subversion ruby-dev

Download and Use

I have uploaded my Vagrantfile and the provisioning script to GitHub, you find the repository here: https://github.com/neurotroph/rails-vagrant-base

After downloading the two files or cloning the repository:

git clone https://github.com/neurotroph/rails-vagrant-base.git .

You just have to vagrant up. It should download Virtual Box and the bento/ubuntu-15.04 base box. The first start might take a couple of minutes for downloading, compiling and installing everything (especially compiling Ruby from source and installing Rails takes a while).

If you want to start a new Rails project from scratch, you can just start right away with rails new <projectname>. For an existing project, make sure to use bundle install to download all required gems.

Comments and Feedback

I’m no expert with Vagrant or Ubuntu, so there might be a more elegant way.3 If you find any bugs, feel free to fork, enhance or comment at the GitHub repository.

  1. Yeah, sometimes I really hate Linux.
  2. I found this list somewhere on the internet and it was said to include all and any requirements for Ruby and Rails.
  3. Using Chef or Puppet is not “more elegant”. It introduces a lot of complexity and more possible points of failure.

Leave a Reply

Your email address will not be published. Required fields are marked *