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 -
You might also want to use RVM as it is highly recommended by rubists.