cyberwombat
cyberwombat

Reputation: 40104

How to install gems without sudo on Mac OS X

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.

Upvotes: 6

Views: 9779

Answers (4)

monfresh
monfresh

Reputation: 8056

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

Upvotes: 1

the Tin Man
the Tin Man

Reputation: 160551

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"

Upvotes: 7

Horacio
Horacio

Reputation: 2965

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

Upvotes: 1

mahemoff
mahemoff

Reputation: 46419

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.

Upvotes: 3

Related Questions