10

I'm using Ruby 2.1 and Rails 4.1 on Windows 7. Whenever I run bundle install, all gems are installed in the system path c:/Ruby21/lib/ruby/gems/2.1.0/gems/. I also found the vendor directory in my project.

Coming from PHP composer and node.js npm background, all dependencies should be locally installed in the project vendor folder or node_modules folder. So, my questions are:

  1. Should I install gems in the system path or vendor/bundle?
  2. If all gems or some gems should be installed in the system path, how could it affect the production environment where I may not have shell access?
  3. Should all gems or specific gems be installed in vendor/bundle?
  4. How can I install gems in vendor/bundle?
Sithu
  • 4,752
  • 9
  • 64
  • 110

1 Answers1

10

When you run bundle install, you are using a tool called Bundler.

  1. Bundler takes care of managing your dependencies in a similar way as Composer, but instead of installing everything in the project folder, it installs your gems system-wide, that are shared among all your projects. It keeps track of what project requires which libraries by using the Gemfile in your project folder. So, you should just let Bundler do its thing, it does it very well and is the standard package manager for Rails.

  2. If your host supports Ruby and Rails applications (for example, a PaaS like Heroku), it definitely will support Bundler and all the necessary gems will be installed. If you're talking about a cheap shared hosting without shell access, you won't be able to deploy a Ruby application there anyway because you will need to install the actual Ruby interpreter and other things, which would require shell access.

  3. No.

  4. You shouldn't. There's this article describing how to do it, but it seems to me that

    countless times where installing gems globally leaked into other projects on the same machine and led to weird behavior that was annoying to debug

    has only ever happened to the author of this article, and I don't think Bundler is at fault. In any case, you should always prepend gem commands with bundle exec (as in bundle exec rspec) and you will never have the mentioned problem. bundle exec makes sure that when you execute a command from a gem, the correct version defined in your Gemfile is called, it is important if you have several version of the same gem installed in your system.

A few years ago when RVM was popular, gemsets achieved a similar goal but got mostly deprecated by rbenv and Bundler.

p4sh4
  • 3,292
  • 1
  • 20
  • 33
  • 5
    I think it's a bit badly communicated that `bundle exec` is required and why it is required, so that's why people commonly think that dependencies should be installed in the project folder. Also, most other package managers (composer, npm, bower, etc) do this, so the different common pattern can also be confusing. – mark.sagikazar Oct 09 '15 at 17:06
  • I don't understand about the point "you should always prepend gem commands with `bundle exec`" in the answer. – Sithu Oct 14 '15 at 07:57
  • @Sithu see [this question and answer](http://stackoverflow.com/questions/6588674/what-does-bundle-exec-rake-mean). In short, it makes sure that when you execute a command that's from a gem, the correct version defined in your Gemfile is called. It is important if you have several version of the same gem installed in the system. As a rule, every time you execute a command in the context of your project, you should prepend it with `bundle exec`. – p4sh4 Oct 15 '15 at 01:54