Quick Tip: Installing Ruby Gems in the user’s Home Directory

By | April 19, 2016

Note: The Operating System in use is Ubuntu Linux (11.04)

My Gem Environment -

$ gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 1.8.11
  - RUBY VERSION: 1.9.2 (2010-08-18 patchlevel 0) [x86_64-linux]
  - INSTALLATION DIRECTORY: /usr/local/lib/ruby/gems/1.9.1
  - RUBY EXECUTABLE: /usr/local/bin/ruby
  - EXECUTABLE DIRECTORY: /usr/local/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-linux
  - GEM PATHS:
     - /usr/local/lib/ruby/gems/1.9.1
     - /home/username/.gem/ruby/1.9.1
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - http://rubygems.org/

After spending quite sometime to find the correct way to install my ruby gems in the HOME directory instead of the system-wide directory (/usr/local/lib/ruby/gems/1.9.1) , i found that the solution was indeed quite simple -

gem install --user-install gem_name

You can even put "gem: --user-install" in your ~/.gemrc for rubygems to do that automatically on `gem install`.

The problem was that, `gem install gem_name` would try to install the gem to /usr/local/lib/ruby/gems/1.9.1 by default. But since it is not writable by the user, the first quick fix that comes to mind is using sudo with the command. Installing a gem with sudo is usually considered as bad practise as it can open path to potential attacks because the gem can then execute some random dangerous code as root.

Finally, you might have to add the /home/path/to/gems/bin to PATH, if it does not already exists. In my case it was /home/username/.gem/ruby/1.9.1/bin. Doing so is really simple. Just add this command to your .bashrc -

export PATH=$PATH:/home/username/.gem/ruby/1.9.1/bin

Thats it!

You might also want to use RVM as it is highly recommended by rubists.

Cheers!

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

6 Comments

Quick Tip: Installing Ruby Gems in the user’s Home Directory
  1. Faruk Adam

    Please can any one help…I have similar issue but I’m very new to this command line stuff…gem is in at /usr/bin/gem, ruby st
    /usr/bin/ruby. Now sass was also installed, however I can check for the versions of gem and ruby but when I do for sass it returns “-jailshell: sass: command not found”

    Any assistance to solve this problem would be much appreciated

    1. Faruk Adam

      RubyGems Environment:
      – RUBYGEMS VERSION: 1.3.7
      – RUBY VERSION: 1.8.7 (2013-06-27 patchlevel 374) [x86_64-linux]
      – INSTALLATION DIRECTORY: /home/username/ruby/gems
      – RUBY EXECUTABLE: /usr/bin/ruby
      – EXECUTABLE DIRECTORY: /home/username/ruby/gems/bin
      – RUBYGEMS PLATFORMS:
      – ruby
      – x86_64-linux
      – GEM PATHS:
      – /home/username/ruby/gems
      – GEM CONFIGURATION:
      – :update_sources => true
      – :verbose => true
      – :benchmark => false
      – :backtrace => false
      – :bulk_threshold => 1000
      – “gempath” => []
      – “gem” => “–remote –gen-rdoc –run-tests”
      – “gemhome” => “/home/username/ruby/gems”
      – “rdoc” => “–inline-source –line-numbers”
      – REMOTE SOURCES:
      http://rubygems.org/

  2. Ilya

    Small correction to my previous post.
    Seems like the example for using -l should be like this:
    ruby -I /home/username/.gem/ruby/1.9.1/gems/gemname/lib yourubyfile.rb

  3. Ilya

    Thank you for your post.
    It helped me to understand that –user-install is an alias to –install-dir /home/username/.gem/ruby/1.9.1.

    By the way, for really local use (without changing your path) you can provide you gem from the command line by -I.
    For example ruby -I /home/username/.gem/ruby/1.9.1/bin yourubyfile.rb

Leave a Reply

Your email address will not be published. Required fields are marked *