5

I've read numerous posts but none so far has done the trick.

How can I, on OSX (El Capitan Beta) install a gem for my own user? Posts suggest specifying -user-install for example but I cannot call gem without sudo at all as it throws permission errors.

I followed "Install gem as user on OSX 10.10 Yosemite" but still cannot call gem.

I installed Homebrew, installed rbenv per the above guide, installed a version of Ruby with rbenv, checked it was loaded correctly, but the final step in the guide is to call gem install which I still cannot do.

Calling gem after installing rbenv results in:

/Users/Me/.rbenv/versions/2.1.5/lib/ruby/2.1.0/rubygems/stub_specification.rb:71:in `initialize': 
Permission denied @ rb_sysopen - /Users/Ne/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/speci

rbenv env dump:

RBENV_VERSION=2.1.5
RBENV_ROOT=/Users/Me/.rbenv
RBENV_HOOK_PATH=:/Users/Me/.rbenv/rbenv.d:
  /usr/local/etc/rbenv.d:
  /etc/rbenv.d:
  /usr/lib/rbenv/hooks
PATH=/Users/Me/.rbenv/versions/2.1.5/bin:
  /usr/local/Cellar/rbenv/0.4.0/libexec:
  /Users/Me/.rbenv/plugins/rbenv-env/bin:
  /Users/Me/.rbenv/shims:
  /usr/local/heroku/bin:
  /usr/local/sbin:
  /usr/local/bin:
  /usr/local/share/npm/bin:
  /Applications/Atom.app/Contents/Resources/app/apm/bin:
  /Users/Me/.bin:
  /Applications/Atom.app/Contents/Resources/app/apm/node_modules/atom-package-manager/bin:
  /usr/local/bin:/usr/bin:
  /bin:
  /usr/sbin:/sbin:
  /opt/X11/bin:
  /usr/local/MacGPG2/bin
RBENV_DIR=/Users/Me/.rbenv/plugins

"brew and gem both throws 'Permission denied' error whenever I run it" seems related though it was not solved except by using some other method which I may have to do.

Community
  • 1
  • 1
cyberwombat
  • 38,105
  • 35
  • 175
  • 251
  • 1
    Correct, don't use `sudo` because that modifies the Ruby installed for Apple's use. Are you using rbenv or RVM to put Ruby in a sandbox in your own home directory? Please don't ask us to go to other sites and read their documentation just to help you because links rot and then your question will be useless to anyone else. Tell us what you've tried, and what errors you got or why they didn't work. – the Tin Man Jul 14 '15 at 18:20
  • Add the output of `rbenv env` to your question, formatted for readability. It's available at https://github.com/ianheggie/rbenv-env. Also, what is the output of `rbenv whence gem`? `gem` is installed by Ruby so unless you're using a really old (and unsupported version of Ruby) it should be installed. – the Tin Man Jul 14 '15 at 18:25
  • Did you follow the rbenv installation commands on the rbenv site? If not, delete the ~/.rbenv directory, remove the version of rbenv that Homebrew installed, and redo your installation of rbenv following [the official documentation](https://github.com/sstephenson/rbenv). Don't follow directions put up by other people when it comes to fast-moving tools like rbenv or RVM. The people who actually know how it works are the ones you should follow. Homebrew is a wonderful tool for missing libraries and such, but follow the rbenv and/or RVM documentation for those tools. – the Tin Man Jul 14 '15 at 18:52
  • --user-install (two minus sign before 'user') works for me on Sierra – Chih-Hsuan Yen May 04 '17 at 18:19

4 Answers4

6

If you can't use gem without sudo, it sounds like you haven't initialized rbenv correctly because the shims aren't available.

These steps are from the documentation. Confirm you did them ALL:

Add ~/.rbenv/bin to your $PATH for access to the rbenv command-line utility.

$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile

Ubuntu Desktop note: Modify your ~/.bashrc instead of ~/.bash_profile.

Zsh note: Modify your ~/.zshrc file instead of ~/.bash_profile.

Add rbenv init to your shell to enable shims and autocompletion.

$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

Same as in previous step, use ~/.bashrc on Ubuntu, or ~/.zshrc for Zsh.

Restart your shell so that PATH changes take effect. (Opening a new terminal tab will usually do it.) Now check if rbenv was set up:

$ type rbenv
#=> "rbenv is a function"
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • I have the above - to be sure I redid it all, opened a new tem but still get the `stub_specification.rb` error – cyberwombat Jul 14 '15 at 18:37
  • Here's a post I just found with same issue - not clear how to solve it though http://stackoverflow.com/questions/29555478/brew-and-gem-both-throws-permission-denied-error-whenever-i-run-it – cyberwombat Jul 14 '15 at 18:40
  • Ok. Deleting my `.rbenv` directory and reinstalling ruby did the trick. I have had an older installe with weird permissions. – cyberwombat Jul 14 '15 at 18:54
  • 1
    It is CRITICAL that you have BOTH the .rbenv/shims and .rbenv/bin in your PATH. – Dave Collins Nov 18 '16 at 16:57
  • The [default install of rbenv](https://github.com/rbenv/rbenv#installation) is the authoritative source for what to do to install rbenv. Follow those directions and it should result in a running rbenv managed environment. The line `echo 'eval "$(rbenv init -)"' >> ~/.bash_profile` will specify what else is needed based on a particular environment, which is the `shims` path on my machines. Don't just blindly add `shims` to the PATH because something else `rbenv init` would have done might be missed. – the Tin Man Nov 18 '16 at 17:12
3

I would firstly recommend Homebrew, a popular choice among developers on OSX and it doesn't require sudo. You can do brew install ruby and go from there.

Also try sandboxed environments like RVM and rbenv, for the same reason.

mahemoff
  • 44,526
  • 36
  • 160
  • 222
1

I think that the best solution to get working your gems running on all projects and local is to work with bundler and make sure to install running

bundle install --path vendor/bundle

That creates a vendor/bundle folder in your project and you should add to your .gitignore

Horacio
  • 2,865
  • 1
  • 14
  • 24
1

As others have mentioned, there are safe ways to install Ruby gems on a Mac. At a high level, this involves six steps:

  1. Install Homebrew (which also installs the prerequisite Apple command line tools)
  2. Install a Ruby manager (such as chruby, rbenv, asdf, RVM) - most of these can be installed with Homebrew
  3. Configure the Ruby manager by adding the appropriate lines to your shell file (~/.bash_profile or ~/.zshrc) - each manager will have instructions for doing this, and this is a typical step that people miss
  4. Restart the terminal for the shell changes to take effect - another step that is often overlooked
  5. Install a specific version of Ruby using the Ruby manager
  6. Switch to that version of Ruby using the Ruby manager

My personal preference is chruby with ruby-install. For more details and a script that can automate this whole process, check out my answer here: https://stackoverflow.com/a/54873916/928191

monfresh
  • 7,974
  • 1
  • 25
  • 22