Eyes, JAPAN Blog > How to switch to rbenv. Part 2.

How to switch to rbenv. Part 2.

denvazh

この記事は1年以上前に書かれたもので、内容が古い可能性がありますのでご注意ください。

Continuing our talk

In “How to switch to rbenv. Part 1.” I explained how it is possible to switch/install rbenv environment and set it with basic configuration.

From now on if there would be necessary to install certain gems, it would be done using bundler. Let’s see how this works.

Checking gems from any directory that doesn’t have Gemfile and bundler files there

[~]$: gem list --local

*** LOCAL GEMS ***

bigdecimal (1.2.0)
bundler (1.3.5)
io-console (0.4.2)
json (1.7.7)
minitest (4.3.2)
psych (2.0.0)
rake (0.9.6)
rdoc (4.0.0)
test-unit (2.0.0.0)

Let’s install Rails without actually installing rails gem!

  • Creating directory to store rails code
  • [~]$: mkdir -p /tmp/rails_app && cd /tmp/rails_app
    

  • Create file Gemfile and add the following lines there
  • source 'https://rubygems.org'
    
    gem 'rails', "~> 4.0.0"
    

  • Execute bundle to create local install of rails gem
  • [/tmp/rails_app]$: bundle install --no-binstubs
    Fetching gem metadata from https://rubygems.org/...........
    Fetching gem metadata from https://rubygems.org/..
    Resolving dependencies...
    Installing rake (10.1.0) 
    Installing i18n (0.6.4) 
    Installing minitest (4.7.5) 
    Installing multi_json (1.7.8) 
    Installing atomic (1.1.12) 
    Installing thread_safe (0.1.2) 
    Installing tzinfo (0.3.37) 
    Installing activesupport (4.0.0) 
    Installing builder (3.1.4) 
    Installing erubis (2.7.0) 
    Installing rack (1.5.2) 
    Installing rack-test (0.6.2) 
    Installing actionpack (4.0.0) 
    Installing mime-types (1.23) 
    Installing polyglot (0.3.3) 
    Installing treetop (1.4.14) 
    Installing mail (2.5.4) 
    Installing actionmailer (4.0.0) 
    Installing activemodel (4.0.0) 
    Installing activerecord-deprecated_finders (1.0.3) 
    Installing arel (4.0.0) 
    Installing activerecord (4.0.0) 
    Using bundler (1.3.5) 
    Installing hike (1.2.3) 
    Installing thor (0.18.1) 
    Installing railties (4.0.0) 
    Installing tilt (1.4.1) 
    Installing sprockets (2.10.0) 
    Installing sprockets-rails (2.0.0) 
    Installing rails (4.0.0) 
    Your bundle is complete!
    It was installed into ./.bundle
    

  • The most interesting parts happens now. We will use rails gem installed below “.bundle” directory and use it to create rails template in current directory
  • [/tmp/rails_app]$: [ -d bin ] && rm -rf bin && bundle exec rails new . -f
    

  • If you see output like below, then everything is fine and you can start your development using brand new Rails 4.
  •        exist  
          create  README.rdoc
          create  Rakefile
          create  config.ru
          create  .gitignore
           force  Gemfile
          create  app
    
    [...]
    
    It was installed into ./.bundle
    Post-install message from rdoc:
    Depending on your version of ruby, you may need to install ruby rdoc/ri data:
    
    <= 1.8.6 : unsupported
     = 1.8.7 : gem install rdoc-data; rdoc-data --install
     = 1.9.1 : gem install rdoc-data; rdoc-data --install
    >= 1.9.2 : nothing to do! Yay!
    

  • From this point, if you’d need to use certain command-line tool provided by gem installed to .bundle, you have to use “bundle exec command_from_gem” all the time. Because it might be quite annoying to type it always, it is generally good idea to make an alias
  • [/tmp/rails_app]$: alias be='bundle exec'
    [/tmp/rails_app]$: be rake -T
    rake about                  # List versions of all Rails frameworks and the environment
    rake assets:clean           # Remove old compiled assets
    rake assets:clobber         # Remove compiled assets
    rake assets:environment     # Load asset compile environment
    [...]
    

Conclusion

Combining rbenv with bundle it became much more easy to store and manage application settings and libraries within application without relying on specific system configuration.
It is not so much important when project and source-code base is managed by single person, but it undoubtedly much more useful when there are big development team with a lot of
collaboration in development and also when development and production environment is different.

Comments are closed.