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

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!







Last Updated On : 19th April 2016

Subscribe to get updates delivered to your inbox

4 Comments + Add Comment

  • Thank you for the info! I’ve spent hours trying to get this to work

  • 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

  • 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

  • good information, thanks for this share.

Leave a comment