Ruby 2 on rails 4 environment

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:
    vagrant -v

    You should see:

    Vagrant version 1.2.7
  2. create a folder for your Rails application and go to it
    mkdir rorapp
    cd rorapp
  3. initialize your Vagrant machine
    vagrant init precise32 http://files.vagrantup.com/precise32.box

    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:

    # config.vm.network :forwarded_port, guest: 80, host: 8080

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

    config.vm.network :forwarded_port, guest: 3000, host: 3000

    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
    vagrant up

    After that, you should see something like

    vagrant up
    [default] Setting the name of the VM...
    [default] Clearing any previously set forwarded ports...
    [default] Creating shared folders metadata...
    [default] Clearing any previously set network interfaces...
    [default] Preparing network interfaces based on configuration...
    [default] Forwarding ports...
    [default] -- 22 => 2222 (adapter 1)
    [default] -- 3000 => 3000 (adapter 1)
    [default] Booting VM...
    [default] Waiting for VM to boot. This can take a few minutes.
    [default] VM booted and ready for use!
    [default] Mounting shared folders...
    [default] -- /vagrant

    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
    vagrant 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:

    nano Vagrantfile

    or you can use your favorite console editor for that …

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

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

    curl -L https://get.rvm.io | bash -s stable --ruby

    Reload your profile:

    source ~/.profile

    You should have rvm installed just fine now:

    vagrant@precise32:/vagrant$ rvm -v
    rvm 1.22.3 (stable) by Wayne E. Seguin <wayneeseguin@gmail.com>, Michal Papis <mpapis@gmail.com> [https://rvm.io/]

    And also the Ruby should be at its latest version:

    vagrant@precise32:/vagrant$ rvm list
    
    rvm rubies
    
    =* ruby-2.0.0-p247 [ i686 ]
    
    # => - current
    # =* - current && default
    # * - default

    But you’re not yet using the latest:

     ruby -v
  10. Add this line to your ~/.profile file:
    [[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"

    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:
    rvm use ruby-2.0.0-p247

    You can check your version with:

    vagrant@precise32:/vagrant$ ruby -v
    ruby 2.0.0p247 (2013-06-27 revision 41674) [i686-linux]

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

  12. Install Rails
    gem install rails

    this installs the latest version of Rails.
    It will install a bunch of packages (takes a while too).
    Check the version with

    vagrant@precise32:/vagrant$ rails -v
    Rails 4.0.0
  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

    rails new /vagrant

    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:

    gem 'execjs'
    gem 'therubyracer'

    If you don’t have it there, add it and then run

    bundle install
  14. Let’s run it and see what we’ve done
    rails server

    you should see it running like this:

    vagrant@precise32:/vagrant$ rails server
    => Booting WEBrick
    => Rails 4.0.0 application starting in development on http://0.0.0.0:3000
    => Run `rails server -h` for more startup options
    => Ctrl-C to shutdown server
    [2013-09-02 14:57:14] INFO WEBrick 1.3.1
    [2013-09-02 14:57:14] INFO ruby 2.0.0 (2013-06-27) [i686-linux]
    [2013-09-02 14:57:14] INFO WEBrick::HTTPServer#start: pid=12919 port=3000

    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 this

That’s all, enjoy!