Since a developer time is most valuable when dealing with actual … development tasks, setting up the environment for a new project better take less or no time, so that the developer can focus on building the actual product rather than focusing on setting up things.

That being said AND the fact that I am kinda lazy developer (to be read as “I automate everything so I won’t have to do it again”) – here’s how to setup a new Rails environment that can be reused, reinstalled easily by anyone in your team.

I’ll be talking about the mac OS X now, a Windows similar blog will come in the near future.

What you need:

  • Vagrant installed on your local machine (OS X)

What you’ll get:

  • a fully functional virtual machine (Ubuntu 12.04) that you can: start, use, shutdown, share with your coworkers.
  • Rails 4 and Ruby 2 environment
  • working dir shared with your local machine so you can use your own IDE and have your work testable in the virtual machine instantly

How to do it:

  1. download and install Vagrant – I use version 1.2.7 You can check that with:

    You should see:
  2. create a folder for your Rails application and go to it
  3. initialize your Vagrant machine

    This is your virtual machine (server) that will hold and run your Rails application.
    If you check now, Vagrant created a config file in your rorapp folder, called Vagrantfile. You’ll change that a little on next step.
  4. make some setup configuration before starting your VM
    Since you’ll want your application to run on VM (as server) but access it from your local machine (client) by simply going to an URL in your browser, you need to forward everything that calls port 3000 to your VM.
    For that, please edit the Vagrantfile and change the port forwarding setting.
    Find the next line:

    change it as below (don’t forget to uncomment it: delete the "#" before it)

    Cool. You’ve just forwarded everything that your local machine browser calls on port 3000 to your vagrant VM on the same port.
  5. start your Vagrant machine

    After that, you should see something like

    Note how it tells you it just forwarded the 3000 port on adapter1.
    Also, your current folder on local machine (rorapp) is mounted as /vagrant on your VM. Here we will build our Rails application forward. This means you can easily use your favorite IDE on local machine and your code changes will be instantly saved in your VM too.
  6. after VM is started, then access it through SSH

    This will open up a SSH to your VM
  7. In your VM, go to your application folder
    cd /vagrant
    Note: you can easily check that is the same folder (rorapp on your local machine) by peaking into the only file there: Vagrantfile. Go ahead and open it in your VM and notice your previous change for the 3000 port.
    You can do something like:

    or you can use your favorite console editor for that …
  8. Do an apt-get update, to be sure you have the latest
  9. We’re going to install the Ruby 2.* version using RVM, and for that we need curl installed too.

    Then install RVM and latest Ruby version, in one single command:

    Reload your profile:

    You should have rvm installed just fine now:

    And also the Ruby should be at its latest version:

    But you’re not yet using the latest:
  10. Add this line to your ~/.profile file:

    Otherwise, it’s possible that you cannot change between the Ruby versions – more details here;
  11. Now you can switch to Ruby2.0 with the following command:

    You can check your version with:

    You’re done with Ruby, let’s take care of the Rails too, then move forward to our application
  12. Install Rails

    this installs the latest version of Rails.
    It will install a bunch of packages (takes a while too).
    Check the version with
  13. Ok, now that you have Ruby2 and Rails4 installed, let’s create a sample application to check the whole thing.
    Create the app with

    this will install a new Ruby on Rails application in the /vagrant folder (current one)
    Now, before you fire it, you need to do a small change. Open your Gemfile and make sure you have the following packages in there:

    If you don’t have it there, add it and then run
  14. Let’s run it and see what we’ve done

    you should see it running like this:

    Then go to your local machine, open a browser window and go to: http://localhost:3000
    If all went well, you should be seeing something like thisRuby on Rails localhost

That’s all, enjoy!

Privacy Preference Center