0

The following Ruby gem installation without sudo used to work back in the day:

xcode-select --install
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
export PATH=/usr/local/bin:$PATH
launchctl setenv PATH “/usr/local/bin:$PATH”
brew install ruby
gem install jekyll

But now on the last step, I get:

ERROR: While executing gem ... (Gem::FilePermissionError) You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory.

I have tried this solution - https://gist.github.com/DirtyF/5d2bde5c682101b7b5d90708ad333bf3. And, it sort of worked, but now my $PATH is messed up. echo $PATH returns - /Users/username/.rbenv/shims:/Users/username/.rbenv/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin.

I have fully removed rbenv and cleared .bash_profile. Now my $PATH looks like so /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin what gets me back to the point one:

ERROR: While executing gem ... (Gem::FilePermissionError) You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory.

Can anyone share a correct up-to-date way to install Ruby gem without using sudo and messing up the $PATH?

macOS 10.14.1

ruby -v
ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin18]

gem env    
RubyGems Environment:
      - RUBYGEMS VERSION: 2.5.2.3
      - RUBY VERSION: 2.3.7 (2018-03-28 patchlevel 456) [universal.x86_64-darwin18]
      - INSTALLATION DIRECTORY: /Library/Ruby/Gems/2.3.0
      - USER INSTALLATION DIRECTORY: /Users/username/.gem/ruby/2.3.0
      - RUBY EXECUTABLE: /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/ruby
      - EXECUTABLE DIRECTORY: /usr/local/bin
      - SPEC CACHE DIRECTORY: /Users/username/.gem/specs
      - SYSTEM CONFIGURATION DIRECTORY: /Library/Ruby/Site
      - RUBYGEMS PLATFORMS:
        - ruby
        - universal-darwin-18
      - GEM PATHS:
         - /Library/Ruby/Gems/2.3.0
         - /Users/username/.gem/ruby/2.3.0
         - /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/gems/2.3.0
      - GEM CONFIGURATION:
         - :update_sources => true
         - :verbose => true
         - :backtrace => false
         - :bulk_threshold => 1000
      - REMOTE SOURCES:
         - https://rubygems.org/
      - SHELL PATH:
         - /usr/local/bin
         - /usr/bin
         - /bin
         - /usr/sbin
         - /sbin
Alex Ljamin
  • 737
  • 8
  • 31
  • what version of MacOS are you on? Are you still trying to use system ruby? I would still recommend using RVM as you should not have any permissions issues with that. – lacostenycoder Nov 11 '18 at 11:57
  • Can you also paste the output off `gem env`? The error is because whatever you're doing is trying to modify system library directories/files which you can't do without `sudo`. But installing gems with `sudo` is not recommended. Also this thread my give some insight https://github.com/rbenv/rbenv/issues/1122 – lacostenycoder Nov 11 '18 at 11:59

4 Answers4

3

What worked for me, after having the same issues and the steps you described in your answer did not resolve the problem, in order to be able to install ruby gems (jekyll, in this case) was to append the two lines mentioned in the jekyll installation guide to my shell config file using the terminal:

echo 'export GEM_HOME=$HOME/gems' >>~/.bash_profile
echo 'export PATH=$HOME/gems/bin:$PATH' >>~/.bash_profile

just be sure to use two >> instead of one, as mentioned in this thread.

Disclaimer: This worked for me, but I am new to using the CLI, so I have no idea if the lines above might have any security implications…

bruno
  • 369
  • 2
  • 15
0

Don't install ruby with homebrew if you are going to use a ruby version manager, i.e. rbevn.

I would at this point recommend brew uninstall ruby

Then use rbenv if you already have it working to install your ruby version.

But if you are confused and not sure what you're doing, you might wanna consider using RVM instead of RBENV as the install/setup is easier.

If so see How do you uninstall rbenv on OSX?

Then see https://rvm.io/rvm/install

Looking at your gist echo "export PATH=$HOME/gems/bin:$PATH" >> ~/.bashrc

You may need to edit your ~/.bashrc and remove that export line

You will probably want to close and reopen your terminal before doing any of the above. Anything that makes changes to ~/.bashrc won't take effect until you restart your terminal

lacostenycoder
  • 10,623
  • 4
  • 31
  • 48
0

Thanks for the helpful link lacostenycoder. I have managed to get Ruby gems installed without sudo the following way:

xcode-select --install
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install rbenv
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
rbenv install 2.5.1
rbenv global 2.5.1
gem install jekyll
Alex Ljamin
  • 737
  • 8
  • 31
0

The key to safely install any Ruby gem on a Mac is to have a proper Ruby environment configured. Installing Ruby with Homebrew is one way to do it, although using a Ruby manager is better. As mentioned by others, you also have to make sure that the PATH was set properly, and to restart your terminal (or open a new tab, or source the shell file) for the changes in your shell file to take effect.

At a high level, setting a working Ruby development environment on a Mac 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