Kimmo Hintikka Kimmo Hintikka - 1 year ago 40
Ruby Question

Is there a difference between using RVM local gemset and using Bundle exec?

There is fundamental problem with ruby gem sets where commands like rake xxxx might create error because command might use global version of rake while your gemfile requires very specific version.

I know 2 ways to mitigate this.

  1. Use
    bundle exec
    in front of rake and rails commands. For example
    bundle exec rake db:migrate
    Adding this in front makes sure command is executed in relation of the current bundle meaning the bundle that got created when you run
    bundle install

  2. Using RVM. There is less known way of creating local gemset with RVM. First you create directory for your new app in this case
    mkdir myapp
    cd myapp
    and then run
    rvm use ruby-2.3.0@myapp --ruby-version --create
    for creating local gem set with nothing in it. Now you can run
    gem install rails
    rails new .
    to create rails myapp with it's dedicated gem environment in the folder you are in.

As nice extra RVM will swap to right gemset every time you cd to myapp directory automatically.

As for as I understand both approaches work, but are there any known issues why you would not use one of approaches above?

Answer Source

If you're an RVM fan like myself, I would go with creating my own gemset for every rails project I start. This will make it easier for me to just use rake and rails commands without specifying bundle exec every time.

Put in mind when sharing your code that some developers might be using rbenv or maybe not using a ruby version manager at all. One way you can make sure that everyone is happy and working with the same version of ruby, is to provide two additional files in your project directory (.ruby-version and .ruby-gemset) and track them in your project.

The .ruby-version file would contain the version of ruby you're using for example 2.4.1. Note that this is compatible with both RVM and rbenv and will switch to the correct ruby version.

The .ruby-gemset file is identified only by RVM; thus, switching to the correct gemset you have setup for the project.

This will make your project compatible with developers using rbenv while still making those of us using RVM happy :)

Putting all these considerations in mind, now you should care less wither people use bundle exec or not!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download