Ruby on Rails on Vagrant

Having gotten Vagrant working generally to host a development environment on Windows, I have two main objectives left:

  1. Run a Rails server locally in my Vagrant vm so that I can do rails dev on Windows.
  2. Push a vagrantfile onto AWS so that I can “deploy” a rails server from Windows.

In pursuit of goal 1, I spent quite a bit of time looking at different posts that claimed to set up a rails environment on Virtual box in different ways. I learned that there’s a reasonable amount of variability in what people call a rails stack. Some of the posts depended on Chef, which is fine, but it’s not the tool I’m trying to use right now. Other posts depended on setting up a bare VM and SSHing into it and running a million little commands to get rails running properly, but that seems contrary to what I think Vagrant is for. I want to get all my Vagrantfiles and‘s right, and then just vagrant up. I don’t want to spend all day babysitting some VM that has weird locality errors when you install Postgresql.

I realized that what I’m really looking for is a Vagrant box that is already configured for rails development, so that I don’t have to mess around too much with setting up the server itself. I finally found this box by searching on HashiCorp’s Atlas for “rails”.

Now we finally seem to be in business. I launched the box, ssh’ed into it (on the first try!) and typed ruby -v. Sure enough, ruby is installed and it seems like things are good to go.

I moved to the shared directory and did gem install rails which worked and rails new site which also worked. (Note: In rails new site site is name of the new site I’m making; it doesn’t tell rails that I’m making a site as opposed to, say, a pancake.) Then I cd’ed into the ./site and ran bin/rails server and I got an error: no JavaScript runtime installed.

Part of my philosophy here is that I’m not going to give the VM a lot of care and feeding. It’s missing a dependency; that means the start up script is wrong. Shut down, edit the script, and try again. So I edit to add apt-get install -y node, then edit the Vagrantfile to make sure that gets run by adding:

config.vm.provision "shell", inline: <<-SHELL
    apt-get update

And now rails server (executed from /vagrant/site/bin) will launch the site. Of course, the site doesn’t do anything yet, but that seems like a problem for the text post.

Till next time, happy learning!


Leave a Reply

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

You are commenting using your 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