How to Ensure Ruby Gems Are Installed in Right Place to Be Executed by Bundler

How do I ensure ruby gems are installed in right place to be executed by bundler?

From a clean start

You do not have to do anything apart from the plain steps stated on the Bundler home page:

  • Do NOT use sudo bundle ...
  • Do NOT change anything to your PATH
  • Do not touch any ~/.file

  • Copy require 'bundler/setup' at the top of your first application file

  • DO use bundle exec in front of all your Bundler-managed "binaries"

Everything just works using the nice defaults for a developer workstation. When you want to go further like running multiples Rubies side by side or deploying to prod or are fed up with typing bundle exec (which you should alias to bex using alias bex='bundle exec') you can then Read The Full Manual, type bundle install --binstubs and install one of the Rubymongers (RBEnv, RVM).

In your specific situation

I would restart from scratch:

  • uninstall RBEnv (rm -rf ~/.rbenv and delete rbenv mentions in grep rbenv ~/.bashrc ~/.bash_profile ~/.zshrc /etc/profile /etc/profile.d/*)
  • uninstall Bundler

Bundler: ensure production gems installed system-wide?

There is no way to get Bundler to use System gems, though I think there should be: https://github.com/bundler/bundler/issues/1964

The most straightforward solution for you would be to package the gems into your Git repo: http://bundler.io/v1.12/bundle_package.html.
This is really how the bundler team recommend that it should be run in a case where you want a user to be able to run your app without having to install gems locally.

A second option would be for you to use the --path option to bundle install and point that to a shared location visible to all your users. This option is remembered, so check .bundle into your Git repo and then your users would use the same configuration and reference the same location when they run bundle. Since all of the gems would already be installed there by you, they would have no problems.

Where does bundler store gems?

It depends. In the usual development setup they are installed where they would be when you install a gem "normally" (by running gem install foo) and bundler won't reinstall gems that are already there. This location depends on how rubygems itself is configured.

If you run bundle install with the --deployment option then the gems will be installed in a location unique to your app (you can pass this as a separate option but it defaults to vendor/bundle)

You can also run bundle package to store all the .gem files your app uses in vendor/cache. Running bundle install will prefer gems in vendor/cache to gems in other locations.

Specify gem installation directory

You can add the following to your config.ru file:

ENV['GEM_HOME']="#{ENV['HOME']}/projects/shared/gems/ruby/1.8/gems"
ENV['GEM_PATH']="#{ENV['GEM_HOME']}:/var/lib/ruby/gems/1.8"
require 'rubygems'
Gem.clear_paths

This will tell your rack app where to look for gems.

Also configure your server .bashrc:

export GEM_HOME="$HOME/projects/shared/gems/ruby/1.8/gems"
export GEM_PATH="$GEM_HOME:/var/lib/ruby/gems/1.8"

Bundler installing gem that's already installed

Try running bundle env to verify that the install location is what you expect.

If not, check whether there is a .bundle/config or ~/.bundle/config file overriding the install path. The output of bundle env will tell you what configuration it is using and how it was determined (i.e., which file it was in or whether it was picked up from an environment variable).

Bundle show. Where are gems installed when I run bundle install? Are they just on my Ruby?

Gem installations will be namespaced by the Ruby version. So if you're using Rbenv or RVM and have a few different Ruby versions installed, running gem install will only install the gem for the version of Ruby you have currently selected.

To respond to your comment, there is no alternative to installing a gem locally if you want to use it. "Locally" meaning "on your computer".

To answer your question about the path:

/Users/jeffrey.wan/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/devise-3.5.4
  1. You are using rbenv, so all your ruby installations are namespaced in the ~/.rbenv directory.
  2. You are currently using ruby version 2.1.6, so your gems are being installed to that folder
  3. To explain the 2.1.0 folder - basically when gems are published, they are pinpointed to a specific Ruby version. So when you request the devise gem be installed to your ruby 2.1.6, the closest matching devise is chosen (in this case, the one build for ruby 2.1.0).

By the way, it can sometimes be useful to use bundle show when you want to debug a gem you've installed. You can go into the source code and add a breakpoint. It's not something I recommend doing often, but can help when you're messing with old, partially-functional gems.

By the way, when you that gems are installed "on your Ruby", that's maybe not the best wording. Installing a gem doesn't patch the Ruby language. All it does is install a library that you can choose to include in your programs (via require). Some gems do create shell commands, too (something like rake, for example).

Installing Gems with Bundler == Big problem

That is because you also have to address where the gem location ( specifically where bundler is installed ) in your nginx start script as well.

bin/start

#!/bin/bash

TMPDIR=/home/shadyfront/webapps/truejersey/tmp GEM_HOME=/home/shadyfront/.rvm/gems/ruby-1.8.7-p330@true /home/shadyfront/webapps/truejersey/nginx/sbin/nginx -p /home/shadyfront/webapps/truejersey/nginx/


Related Topics



Leave a reply



Submit