What is the difference between "~> 4.0.1", "4.0.1" and no version specifier in a Gemfile?

JVK picture JVK · Aug 25, 2012 · Viewed 14.8k times · Source

In Rails Gemfile, what are the differences between these :

gem "gemname", "~> 4.0.1"

and

gem "gemname", "4.0.1"

and

gem "gemname"

Also what should be used where and benefit of following that way?

Answer

traday picture traday · Aug 25, 2012

The first will tell bundler to load any gem that varies with the last (patch) number. So 4.0.x where x is 1 or greater.

The second will only load 4.0.1.

The third will get the highest value that works (depending on what the needs of the rest of your gems in your Gemfile) or will get whatever is specified in your Gemfile.lock, if you have one.

I missed your second question. Frankly, it depends. For the most part, I go with the first option, because it lets me pick up bug fixes without worrying about how it impacts my other gems.