Search code examples
rubyrubygemsrvmsudochown

Setting up RVM for gem installs in future for correct permissions and adding PATH correctly


I have scoured the internet for answers. I feel I have not followed the right way to do things and just want to set up my rvm environment in the proper way. This all stemmed from me having to use sudo gem install sass to install SASS which I didn't want to do.

Now when I do gem install sass I get:

ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /usr/bin directory.

Steps I took:

sudo chown -R dhruv: /Library/Ruby/Gems/2.0.0
\curl -sSL https://get.rvm.io | bash -s stable --ruby
brew install gnupg gnupg2
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
sudo chown -R dhruv:staff ~/.rvm
sudo chown -R dhruv:staff ~/.gem
sudo chown -R dhruv:staff /Library/Ruby/Gems/2.0.0

Note that which rvm doesn't return anything, even though running the curl command above gave me:

Upgrading the RVM installation in /Users/dhruv/.rvm/
    RVM PATH line found in /Users/dhruv/.mkshrc /Users/dhruv/.profile /Users/dhruv/.bashrc /Users/dhruv/.zshrc.
    RVM sourcing line found in /Users/dhruv/.profile /Users/dhruv/.bash_profile /Users/dhruv/.zlogin.
Upgrade of RVM in /Users/dhruv/.rvm/ is complete.

# dhruvghulati,
#
#   Thank you for using RVM!
#   We sincerely hope that RVM helps to make your life easier and more enjoyable!!!
#
# ~Wayne, Michal & team.

I want to get things to a state where I can install gems easily without sudo. How do I do this?

Also, how do I prevent these PATH errors I am seeing for rvm?

Also I am worried about when I did sudo chown -R dhruv:staff /Library/Ruby/Gems/2.0.0, the internet seems to say this is a terrible evil and I will have to revert back? Can I revert this to maybe chmod to the original user this should be owned by?


Here is the output of rvm info:

Warning! PATH is not properly set up, '/Users/dhruv/.rvm/gems/ruby-2.2.1/bin' is not at first place,
         usually this is caused by shell initialization files - check them for 'PATH=...' entries,
         it might also help to re-add RVM to your dotfiles: 'rvm get stable --auto-dotfiles',
         to fix temporarily in this shell session run: 'rvm use ruby-2.2.1'.

ruby-2.2.1:

  system:
    uname:       "Darwin Dhruvs-MacBook-Pro-2.local 14.5.0 Darwin Kernel Version 14.5.0: Tue Sep  1 21:23:09 PDT 2015; root:xnu-2782.50.1~1/RELEASE_X86_64 x86_64"
    system:      "osx/10.10/x86_64"
    bash:        "/bin/bash => GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin14)"
    zsh:         "/bin/zsh => zsh 5.0.5 (x86_64-apple-darwin14.0)"

  rvm:
    version:      "rvm 1.26.11 (latest) by Wayne E. Seguin <wayneeseguin@gmail.com>, Michal Papis <mpapis@gmail.com> [https://rvm.io/]"
    updated:      "1 day 8 minutes 30 seconds ago"
    path:         "/Users/dhruv/.rvm"

  ruby:
    interpreter:  "ruby"
    version:      "2.2.1p85"
    date:         "2015-02-26"
    platform:     "x86_64-darwin14"
    patchlevel:   "2015-02-26 revision 49769"
    full_version: "ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-darwin14]"

  homes:
    gem:          "/Users/dhruv/.rvm/gems/ruby-2.2.1"
    ruby:         "/Users/dhruv/.rvm/rubies/ruby-2.2.1"

  binaries:
    ruby:         "/Users/dhruv/.rvm/rubies/ruby-2.2.1/bin/ruby"
    irb:          "/Users/dhruv/.rvm/rubies/ruby-2.2.1/bin/irb"
    gem:          "/Users/dhruv/.rvm/rubies/ruby-2.2.1/bin/gem"
    rake:         "/Users/dhruv/.rvm/rubies/ruby-2.2.1/bin/rake"

  environment:
    PATH:         "/Users/dhruv/anaconda/bin:/Users/dhruv/anaconda/bin:/Users/dhruv/.rvm/gems/ruby-2.2.1/bin:/Users/dhruv/.rvm/gems/ruby-2.2.1@global/bin:/Users/dhruv/.rvm/rubies/ruby-2.2.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/Users/dhruv/.gem/ruby/2.0.0/bin:/Users/dhruv/scripts:/Users/dhruv/.rvm/bin:/Users/dhruv/.rvm/bin:/usr/local/lib:/Users/dhruv/.gem/ruby/2.0.0/bin:/Users/dhruv/scripts:/Users/dhruv/.rvm/bin:/Users/dhruv/.rvm/bin:/usr/local/lib"
    GEM_HOME:     "/Users/dhruv/.rvm/gems/ruby-2.2.1"
    GEM_PATH:     "/Users/dhruv/.rvm/gems/ruby-2.2.1:/Users/dhruv/.rvm/gems/ruby-2.2.1@global"
    MY_RUBY_HOME: "/Users/dhruv/.rvm/rubies/ruby-2.2.1"
    IRBRC:        "/Users/dhruv/.rvm/rubies/ruby-2.2.1/.irbrc"
    RUBYOPT:      ""
    gemset:       ""

The command gem install --user-install sass gives me:

WARNING:  You don't have /Users/dhruv/.gem/ruby/2.2.0/bin in your PATH,
      gem executables will not run.
Successfully installed sass-3.4.21
Parsing documentation for sass-3.4.21
Done installing documentation for sass after 4 seconds
1 gem installed

.bash_profile:

export PATH=$PATH:~/.gem/ruby/2.0.0/bin
export SBT_OPTS="-Xmx2G -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=2G -Xss2M  -Duser.timezone=GMT"
export PATH=$PATH:~/scripts
source ~/.profile
source ~/.bashrc
export PATH=$PATH:/usr/local/lib
# added by Anaconda3 2.3.0 installer
export PATH="/Users/dhruv/anaconda/bin:$PATH"
export MONGO_PATH=/usr/local

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

.bashrc:

export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"

.profile:

export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

Solution

  • First, stop using --user-install. You shouldn't need it with RVM as that's one of its primary purposes, keeping the gems and Ruby in your home directory.

    Your path is not well constructed:

    "/Users/dhruv/anaconda/bin:/Users/dhruv/anaconda/bin:/Users/dhruv/.rvm/gems/ruby-2.2.1/bin:/Users/dhruv/.rvm/gems/ruby-2.2.1@global/bin:/Users/dhruv/.rvm/rubies/ruby-2.2.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/Users/dhruv/.gem/ruby/2.0.0/bin:/Users/dhruv/scripts:/Users/dhruv/.rvm/bin:/Users/dhruv/.rvm/bin:/usr/local/lib:/Users/dhruv/.gem/ruby/2.0.0/bin:/Users/dhruv/scripts:/Users/dhruv/.rvm/bin:/Users/dhruv/.rvm/bin:/usr/local/lib"
    

    Which breaks down to:

    /Users/dhruv/anaconda/bin
    /Users/dhruv/anaconda/bin
    /Users/dhruv/.rvm/gems/ruby-2.2.1/bin
    /Users/dhruv/.rvm/gems/ruby-2.2.1@global/bin
    /Users/dhruv/.rvm/rubies/ruby-2.2.1/bin
    /usr/local/bin
    /usr/bin
    /bin
    /usr/sbin
    /sbin
    /Library/TeX/texbin
    /Users/dhruv/.gem/ruby/2.0.0/bin
    /Users/dhruv/scripts
    /Users/dhruv/.rvm/bin
    /Users/dhruv/.rvm/bin
    /usr/local/lib
    /Users/dhruv/.gem/ruby/2.0.0/bin
    /Users/dhruv/scripts
    /Users/dhruv/.rvm/bin
    /Users/dhruv/.rvm/bin
    /usr/local/lib
    

    You need to have RVM first in the path but you're getting in its way.

    It should look something like:

    /Users/dhruv/.rvm/gems/ruby-2.2.1/bin
    /Users/dhruv/.rvm/gems/ruby-2.2.1@global/bin
    /Users/dhruv/.rvm/rubies/ruby-2.2.1/bin
    /Users/dhruv/scripts
    /Users/dhruv/anaconda/bin
    /usr/local/bin
    /usr/bin
    /bin
    /usr/sbin
    /sbin
    /Library/TeX/texbin
    

    I'd rewrite ~/.bash_profile to:

    source ~/.bashrc
    
    export MONGO_PATH=/usr/local
    export SBT_OPTS="-Xmx2G -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=2G -Xss2M  -Duser.timezone=GMT"
    
    export PATH=~/scripts:~/anaconda/bin:$PATH
    
    # RVM needs to initialize last so it can adjust the path correctly...
    [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
    

    I'd rewrite ~/.bashrc to:

    # [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
    

    I'd rewrite ~/.profile to:

    # export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for scripting
    # [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
    

    That should clean up your path, and probably will allow RVM to figure things out better.

    You might need to do minor tweaks but keep it simple.

    I'd recommend reading about how the shell uses PATH to find things and how to define PATH. Also, it'd be good to read about using ~/.bashrc, ~/.profile and ~/.bash_profile, especially on Mac OS. man bash at the command-line says, among other things:

    When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.

    [...]

    When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc.

    When you install RVM using their default installation, it will create the ~/.rvm directory with the correct permissions. You won't have to do a thing after that to have RVM and Ruby store gems in that directory. Scripts will be able to find gems you've installed without you doing anything special with gem install. If it gets weird or hard something is wrong.