codedecoder

breaking into the unknown…

bundle exec in rails

3 Comments

Bundler maintains a consistent environment for ruby applications. It tracks an application’s code and the rubygems it needs to run, so that an application will always have the exact gems (and versions) that it needs to run. This is the definition of bundler available on its site. The site already have all the detail in concise way. I’am just paraphrasing them here with my own observation.

First, thing is that it is really good gem to manage all your gem dependency as well as there consistency. So, better to install it at the start of your project. If You are using RVM it automatically install it for you under global gemset. The global gemset get created for every ruby version you installed. The default gems created within global gemset is as below.

$ gem list #listing all the gem within global gemset

*** LOCAL GEMS ***

bundler (1.1.5)
rake (0.9.2.2)
rubygems-bundler (1.0.3)
rvm (1.11.3.5)

Now, any gemset you create, inherit all the gem within global gemset, show basically you do not need to install them. So let say, our bundle is working.

So, what is the problem ..???

The problem is that, if you have multiple version of some executable(which is used to run other files) like rake, rspec, cap etc in your gemset , it will throw error due to conflict. The common one is for rake below :

$ rake db:migrate # any command with rake will fail with this error
rake aborted!
You have already activated rake 10.0.3, but your Gemfile requires rake 10.0.2

When I checked my gemset, I do find multiple versions of  rake as below

rake (10.0.3, 10.0.2, 0.9.2.2)

SOLUTION :

1 -> prefix bundle exec to run all your exicutable # It will run the version associated with your gem file, thus removing the conflict

$ bundle exec rake db:migrate # it will run rake-10.0.2 associated with your gemfile

2 -> remove the gem causing the conflict # if you do not want to prefix bundle exec with your executable command like rake respec etc, remove the conflicting gem

$ gem uninstall rake -v 10.0.3

NOTE :

It is good practice to run all the executable commands with bundle exec prefix, which will ensure that your command will always work. It is must when you are writing deployment script with capistrano mina etc, so that your deployment do not fail due to conflicting executable

Author: arunyadav4u

over 10 years experience in web development with Ruby on Rails.Involved in all stage of development lifecycle : requirement gathering, planing, coding, deployment & Knowledge transfer. I can adept to any situation, mixup very easily with people & can be a great friend.

3 thoughts on “bundle exec in rails

  1. thaks a lot..
    it helped me out..
    🙂

  2. Should -version on solution 3 have a double dash?

    • Ya russell,

      It should be double dash. Infact, I have written it with double dash in the post, but the two dash should not have any space between them. I think the wordpress showing them as a single dash. I have replaced the command with shorthand form which work with single dash

      As a thumb rule:
      If any command take options, the option can be passed in shorthand form and fullform. The shorthand form always precede by one dash and fullform with two dash without any space. Taking the same example above you can specify version option to gem uninstall command in shorthand form as -v or in fullform as – -version

      $ gem uninstall rake – -version 10.0.3 #full form of option version is passed
      $ gem uninstall rake -v 10.0.3 #short form of option version is passed

      NOTE : If you want to figure out what options a perticuler command can take you can find it by typing that command with -h or – -help options

      Example: finding out available options for gem uninstall
      $ gem uninstall – -help # it will show you all available options, and how to use them

      or
      $ gem uninstall -h

Leave a comment