20

By default, when you sudo gem install thegemname it will install executables into /usr/bin/

Is there a way to change this? For example, to install them into /usr/local/rubygems/bin (or any other path)?

The path doesn't seem to be hard-coded into the gemspec file, so I don't see why this shouldn't be possible (although I have very little experience with Ruby/Gems)

dbr
  • 165,801
  • 69
  • 278
  • 343

4 Answers4

20

I'm adding this as an answer so that it is obvious when I run into this problem again :)

First, move all the bins in /var/lib/gems/1.8/bin/ to /usr/bin/. If you don't do this, then uninstalling or updating a gem will not remove the binary from the original bin directory.

You may also wish to remove the old path from $PATH

Next, edit ~/.gemrc and add (or update) the following line:

gem: --bindir /usr/bin

This overrides gem so that it always uses /usr/bin/ as the bin dir.

No need to update the path (especially messy for multiple-user machines).

dbr
  • 165,801
  • 69
  • 278
  • 343
BryanH
  • 5,826
  • 3
  • 34
  • 47
16

See http://www.rubygems.org/read/chapter/11 and specify a ~/.gemrc which defines a gemhome variable.

For example:

gemhome: /usr/local/rubygems

You can also place this file in /etc/gemrc

Alternatively you can set the GEM_HOME env-variable:

$ export GEM_HOME=/tmp/gemtest
$ gem install bundler
$ ls /tmp/gemtest/bin/
bundle

Update (10 years later):

Andrey Rodionov below suggest using

gem: --bindir /usr/bin
csl
  • 10,937
  • 5
  • 57
  • 89
  • 2
    Thanks! I hope you don't mind I updated your answer. I prodded around rubygems.rb - the bin dir is appended to gemhome, there doesn't seem any obvious way to override only this, but setting gemhome to /Library/Ruby/Gems/1.8/ (on OS X) gives a bin path of /Library/Ruby/Gems/1.8/bin which is perfect – dbr Dec 02 '08 at 10:54
  • 2
    Gah, not quite perfect. There is a default /usr/bin override for the Mac distribution.. – dbr Dec 02 '08 at 10:59
  • 1
    Not working. New error generated ```Unable to resolve dependency: user requested 'did_you_mean (= 1.0.0)'```. Changing to ```gem: --bindir /usr/bin``` helped. – dernasherbrezon May 01 '18 at 15:26
2

On OS X, the executable directory is overridden to /usr/bin in the file /Library/Ruby/Site/1.8/rubygems/defaults.rb

# The default directory for binaries
def self.default_bindir
  if defined? RUBY_FRAMEWORK_VERSION then # mac framework support
    '/usr/bin'
  else # generic install
    ConfigMap[:bindir]
  end
end

As a hackish work around, I changed /usr/bin to my desired bin location, which works correctly. There doesn't seem to be any way to override bindir from the ~/.gemrc config?

dbr
  • 165,801
  • 69
  • 278
  • 343
  • 1
    This is really annoying. There is no way to change this because it depends on the global constant. And if you do change it everytime rubygems is updated you have to change it again. I'd like to shoot the guy responsible for this appalling decision. – mxcl Oct 22 '09 at 15:01
  • 1
    I submitted a bug report for this: http://rubyforge.org/tracker/index.php?func=detail&aid=27641&group_id=126&atid=575 – mxcl Dec 31 '09 at 00:06
  • 1
    Actually, we were wrong. You can change the bindir. I documented it here: http://wiki.github.com/mxcl/homebrew/cpan-ruby-gems-and-python-disttools – mxcl Feb 07 '10 at 19:53
  • Updated wiki link that Max posted: http://wiki.github.com/mxcl/homebrew/gems-eggs-and-perl-modules – dbr Apr 21 '10 at 11:18
  • Updated link again since the wiki is gone: https://github.com/Homebrew/homebrew/blob/master/share/doc/homebrew/Gems,-Eggs-and-Perl-Modules.md – bpedman Oct 22 '15 at 19:54
  • If you remove the /Library/Ruby/Site folder, it'll default to the implementation in /System/Library/Frameworks/Ruby.framework/Versions/2.0.0/usr/lib/ruby/2.0.0/rubygems. There the folder set to /usr/local/bin. I suspect the Site folder in /Library/Ruby is a leftover from a previous Version of OSX – Karsten Mar 05 '16 at 10:20
0

To install the executable to a desired directory, the command line option --bindir may be used:

sudo gem install thegemname --bindir /usr/local/rubygems/bin

Tried this option successfully with gem version 2.0.14.1.

For more command line options, run gem install --help on the command line.

Sanjeev Sachdev
  • 1,241
  • 1
  • 15
  • 23