Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I install ruby gems in system repository (globally) or the project vendor/bundle (locally)?

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?
like image 459
Sithu Avatar asked Oct 05 '15 02:10

Sithu


People also ask

What is Ruby gem bundle?

Bundler provides a consistent environment for Ruby projects by tracking and installing the exact gems and versions that are needed. Bundler is an exit from dependency hell, and ensures that the gems you need are present in development, staging, and production. Starting work on a project is as simple as bundle install .

Where does bundler install gems?

Show activity on this post. I know that when using gem install , the gem will be stored under /home/username/. rvm/gems/, under which gemset the gem was installed.

Where are RubyGems installed?

When you use the --user-install option, RubyGems will install the gems to a directory inside your home directory, something like ~/. gem/ruby/1.9. 1 . The commands provided by the gems you installed will end up in ~/.


1 Answers

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.

like image 199
p4sh4 Avatar answered Oct 22 '22 08:10

p4sh4