Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bundle vs npm approach or why does bundle/gem installs globally?

I am confused by the Bundle/Gem approach of global dependencies. I thought global dependencies are a thing of a past. I present to you two approaches of handling dependencies, NPM vs Bundle/Gem.

NPM

Let me demonstrate the power of NPM. Say you have an app with the following folder structure:

/app
  /admin
     package.json
  /front-end
     package.json

You can see that inside my app I have two other mini-apps. Each of those has its own package.json.

Say both of them have q as a requirement, but with different version. Running npm install in both of these folders will install appropriate versions to node_modules of each mini app. Everything is nicely isolated and works very well.

Bundle/Gem

Bundles approach is slightly different. You use Gemfile to specify your dependencies. After running bundle install the gems will be put into a system wide location. That means that if I have an app that requires different versions of a gem within its mini-apps then it wont work.

Work arounds that make no sense to me:

  • I am aware that there is a bundle install --deployment, but its used only for deployments in production. How is my code supposed to work in development?

  • Also, I am aware that you have an options to require a specific version of the gem in your code but that beats the convenience of having Gemfile with specific versions in it. It also makes no sense in production environment since you already have bundle install --deployment.

I am 100% sure I'm missing something very trivial here. Please point me into the right direction.

like image 345
Denis Pshenov Avatar asked Jan 07 '14 09:01

Denis Pshenov


People also ask

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 does bundle install run?

Install Bundler Select Tools | Bundler | Install Bundler from the main menu. Press Ctrl twice and execute the gem install bundler command in the invoked popup. Open the RubyMine terminal emulator and execute the gem install bundler command.

Is a Gemfile like package JSON?

package.json is roughly equivalent to Bundler's Gemfile . Project dependencies and their required versions are listed here, as well as configuration, and customized scripts relating to the project. When entering an unfamiliar project, this is the first place to look.


2 Answers

I found the 'answer'.

Ruby unlike Node.js does not have a built in support for local node_modules like implementation. Thus you have to use Bundler. But to use the dependencies in your code you need to let Ruby know where these dependencies are located. Bundler makes it easy for you with the following lines in your app code:

require 'rubygems'
require 'bundler/setup'

This will read your Gemfile.lock and apply proper paths when your require something form your app. This is the part that I missed.

You can also use bundle exec {rubygem} to run gems that are installed by bundler for your app and not those that are installed globally.

I hope this helps someone!

like image 149
Denis Pshenov Avatar answered Sep 18 '22 19:09

Denis Pshenov


If you use bundle install --binstubs path=vendor then you get locally (to the project) installed gems, not system wide. Bundler still takes a different approach to libraries that it needs yet have different versions, by installing one that is compatible to both.

like image 44
ian Avatar answered Sep 17 '22 19:09

ian