169

Running 'sudo gem list --local' and 'gem list --local' give me differing results. My gem path is set to my home folder and only contains the gems from 'gem list --local'.

It's probably not good to have gems installed in different directories on my computer, so should I have the gem path set differently, and should I always use sudo when installing something?

my ~/.profile
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"

~/.bash_profile is empty.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
raphael_turtle
  • 7,154
  • 10
  • 55
  • 89
  • 2
    I think this is a valid question for those of us who have to use a gem that requires root privileges. – Kelly Aug 17 '11 at 14:37
  • 1
    Related: http://stackoverflow.com/questions/21141584/rails-is-not-using-my-global-ruby-version. I use rbenv for managing Ruby versions and ran into an issue because I used `sudo gem install rails` instead of `gem install rails`. – Dennis Jan 15 '14 at 17:04
  • 4
    "I think this is a valid question for those of us who have to use a gem that requires root privileges." I think any gem that needs root privileges, either to be installed, or to run, is highly suspicious. Gems should be able to run in a sandbox and run with the user's permissions. – the Tin Man Jul 31 '14 at 18:47
  • 2
    `sudo` is a loaded gun pointed toward your foot. Using it without understanding what it does and how it can affect your system is like pulling the trigger with your eyes closed. You might shoot a hole in your foot, you might not, but either way you don't want to run the risk unless you know how to undo the damage. Using `sudo` writes into the system-owned Ruby, which, on Mac OS, was installed by Apple for their own uses. We can piggyback on it, but changing the wrong thing can break their code. That's why we install from source or use something else to install where we can safely tweak it. – the Tin Man Nov 06 '15 at 16:24

8 Answers8

162

You can install gems in your local environment (without sudo) with

gem install --user-install <gemname>

I recommend that so you don't mess with your system-level configuration even if it's a single-user computer.

You can check where the gems go by looking at gempaths with gem environment. In my case it's "~/.gem/ruby/1.8".

If you need some binaries from local installs added to your path, you can add something to your bashrc like:

if which ruby >/dev/null && which gem >/dev/null; then
    PATH="$(ruby -r rubygems -e 'puts Gem.user_dir')/bin:$PATH"
fi

(from http://guides.rubygems.org/faqs/#user-install)

Nils
  • 5,612
  • 4
  • 34
  • 37
  • 29
    +1 for this option rather than the "Yet Another Configuration Tool" solutions above, RBENV / RVM. – jjpe Nov 11 '14 at 14:23
  • 1
    I am creating a gem and according to manuals I have to do `rake install` and to fix the sudo isse, I had to patch manually the `/Library/Ruby/Gems/2.0.0/gems/bundler-1.7.3/lib/bundler/gem_helper.rb` adding `--user-install` there. Can't find better solution, because looks like `rake install` doesn't accept additional parameters. – Nakilon Mar 09 '15 at 01:48
  • I am absolutely thrilled with that first command. I have been trying to install gems and have been having issues because I don't have sudo privileges. That command worked!!!! – Caleb Kleveter Dec 15 '15 at 16:16
  • `ruby -r rubygems -e 'puts Gem.dir'` and `ruby -r rubygems -e 'puts Gem.user_dir'` print paths of global and user gems. `gem env home` and `gem env user_gemhome`(Ruby 3.2.0) do the same things. – Míng Jan 10 '23 at 10:22
154

Contrary to all the other posts I suggest NOT using sudo when installing gems.

Instead I recommend you install RVM and start a happy life with portable gem homes and different version of Ruby all living under one roof.

For the uninitiated, from the documentation:

RVM is a command line tool which allows us to easily install, manage and work with multiple ruby environments and sets of gems.

The reason why installing gems with sudo is worse than just gem install is because it installs the gems for ALL USERS as root. This might be fine if you're the only person using the machine, but if you're not it can cause weirdness.

If you decide you want to blow away all your gems and start again it's much easier, and safer, to do so as a non-root user.

If you decide you want to use RVM then using sudo will cause all kinds of weirdness because each Ruby version you install through RVM has its own GEM_HOME.

Also, it's nice if you can make your development environment as close to your production environment as possible, and in production you'll most likely install gems as a non-root user.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
jonnii
  • 28,019
  • 8
  • 80
  • 108
  • 15
    +1 for the rvm suggest, but -1 for the confusing condemnation of the alternate possibility of just using sudo – tfwright Jan 22 '10 at 18:13
  • 10
    Ok, but all your reasons boil down to "because RVM is so great" not because using sudo is especially harmful. The only time you wouldn't want to is in the specific situation that you're on a shared computer. If you want to recommend rvm just do that. No need to resort to FUDD. – tfwright Jan 22 '10 at 18:56
  • 7
    I try not spread FUD, and I tried to give non-RVM reasons, I apologise if it seems that way. – jonnii Jan 22 '10 at 19:19
  • 10
    RVM is a good option. But you should try a better one, RBENV. It's less intrusive than RVM. – Andres Nov 24 '12 at 16:30
  • 3
    As somebody who do not know much about Ruby, I miss the the reasons behind every statement of this answer. Why I am supposed to use different versions of Ruby at the same time? Why it is a problem to instal a program for all users. It is the standard practice in *nix to install things as root, but run them as a restricted user. Why does this differ in the Ruby world? What kind of weirdness do you talk about? And so on. – Hontvári Levente Dec 17 '14 at 07:32
  • 4
    @HontváriJózsefLevente it's hard to address all of your questions in a comment, but the short of it is that you want your development environment to be as close as possible to production. if you are working on two apps, one of which uses ruby 2.1 and the other which users 1.9 then you'll need to make sure your local environment is using the right version. that is what RVM or RBENV does for you. You would use it for the same reason you would use any sandbox environment, but the main benefit in this case is isolation. I hope this helps! – jonnii Dec 17 '14 at 15:14
  • @jonii: As I understand it, the recommendation against sudo is valid for development machines. What about servers? Should we use sudo there? – Hontvári Levente Dec 18 '14 at 21:43
  • @HontváriJózsefLevente it depends on your environment. i don't have any specific recommendations other than see what works best. – jonnii Dec 20 '14 at 20:15
  • 1
    Note that broken or malicious gem may execute arbitrary commands. For example https://github.com/wmorgan/killergem attempts during installation to delete every file in root directory. – reducing activity Jan 13 '15 at 10:02
  • In practicality you'll rarely be hosting multiple apps with different ruby versions. RVM is overkill for production, so until you really need it, don't bother using it. I've been using RVM for years in production and removed it and now its one less thing I have to worry about. When you have docker containers its so easy to rebuild with a new ruby version if you need to upgrade. – Andrew WC Brown Nov 16 '15 at 20:46
42

Better yet, put --user-install in your ~/.gemrc file so you don't have to type it every time

gem: --user-install
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
allyraza
  • 1,376
  • 11
  • 7
18

In case you

  • installed ruby gems with sudo
  • want to install gems without sudo
  • don't want to install rvm/rbenv

add the following to your .bash_profile :

export GEM_HOME=/Users/‹your_user›/.gem
export PATH="$GEM_HOME/bin:$PATH"

Open a new tab in Terminal OR source ~/.bash_profile and you're good to go!

David Salamon
  • 2,361
  • 25
  • 30
  • 11
    For someone not working with Ruby, Rails, Rake, and whatever else Ruby Devs use / want to use THIS solution is a lot easier than using rvm. I really don't care about having different ruby versions I just want to run some shell programs distributed as gems. Thank you! – Kevin G. Dec 14 '16 at 14:46
10
sudo gem install --no-user-install <gem-name>

will install your gem globally, i.e. it will be available to all user's contexts.

  • 1
    It can also overwrite a vendor installed gem, which that OS might expect to be set to a specific version. And that could break code that relies on it. – the Tin Man Nov 06 '15 at 16:19
  • 3
    ...or it could install a gem that a vendor doesn't provide, and do so in a way that makes it available to all users on a system. It's mildly aggravating that all the answers everywhere assume the reader is a dev v/s a sysadmin who actually _does_ want to make a specific version of a gem available to all users. :D – dannysauer Apr 19 '19 at 15:20
9

Related (for bundler users), if you want a lighter alternative to RVM which will put everything in a user-specific well known directory, I recommend using:

bundle install --path $HOME/.gem

if you want to install gems to the same place that

gem install --user-install GEMNAME

will install them, .gem/ruby/RUBYVERSION in your homedir. (See the other comment on this question about --user-install.)

This will make the gems visible to gem list, uninstallable via gem uninstall, etc. without needing sudo access. Runnable scripts installed by gem or bundler can be put into your path by adding

$HOME/.gem/ruby/RUBYVERSION/bin

to your $PATH. gem itself tells you about this if it isn't set when you do gem install --user-install.

Todd Vierling
  • 431
  • 4
  • 7
  • Thanks for this. Is it possible to make bundler do this by default? (And if so, would it be a good idea?) – joshtch Mar 12 '19 at 22:54
3

You can install gems into a specific folder (example vendor/) in your Rails app using :

bundle install --path vendor
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Chadi
  • 97
  • 6
  • This. These days the only program that should need sudo to install software is your package manager. – tjbp Jul 22 '16 at 09:49
1

Installing Ruby gems on a Mac is a common source of confusion and frustration. Unfortunately, most solutions are incomplete, outdated, and provide bad advice. I'm glad the accepted answer here says to NOT use sudo, which you should never need to do, especially if you don't understand what it does. While I used RVM years ago, I would recommend chruby in 2020.

Some of the other answers here provide alternative options for installing gems, but they don't mention the limitations of those solutions. What's missing is an explanation and comparison of the various options and why you might choose one over the other. I've attempted to cover most common scenarios in my definitive guide to installing Ruby gems on a Mac.

monfresh
  • 7,974
  • 1
  • 25
  • 22