95
qichunren@zhaobak:~> gem install hpricot
ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions into the /opt/ruby-enterprise-1.8.7/lib/ruby/gems/1.8 directory.

Current login user is qichunren, and qichunre user have write permission with .gem dir.I would like to know why gem not install files into my home .gem dir first? Why my gem common first want to install files into /opt/ruby-enterprise-1.8.7/lib/ruby/gems/1.8

user229044
  • 232,980
  • 40
  • 330
  • 338
qichunren
  • 4,405
  • 7
  • 37
  • 48

5 Answers5

195

Try setting GEM_HOME and GEM_PATH to ~/.gem,

For the current terminal session, just type:

export GEM_HOME=~/.gem
export GEM_PATH=~/.gem

If you want these to be set whenever you open a terminal, add the above commands to your ~/.bashrc file.

For a more comprehensive solution to setting up a custom ruby environment, see this tutorial from Site5KB, which describes using a .gemrc file.

David LeBauer
  • 31,011
  • 31
  • 115
  • 189
user229044
  • 232,980
  • 40
  • 330
  • 338
  • 13
    This is the actual right answer. Don't go wielding sudo installing things if you don't have to. – Jesse O'Brien Jun 08 '13 at 13:23
  • The link in the answer seems to have changed. The first step from this guide accomplished the same thing for me though: http://kb.site5.com/ruby-on-rails/how-to-setup-a-custom-rubygems-environment – Mike S Jan 28 '14 at 20:43
  • @MikeSlutsky Thanks for pointing that out. In the future, if you find a broken link (or a link that has changed and is no longer relevant), feel free to submit an "edit" suggestion if you think you have a decent replacement. – user229044 Jan 28 '14 at 20:47
  • @meagar I updated link given by @Mike-Slutsky, but noticed that it is documentation for `.gemrc` configuration. For ruby gem novices, this is daunting, when I understand your solution to be the much simpler ones I added. The solution works for me, but as a ruby novice, please confirm that my changes are in line with your intent. – David LeBauer Sep 12 '15 at 23:29
  • This is really helpful. Don't simply use sudo. Thanks. – Jerry Liu Nov 17 '15 at 03:06
  • 2
    Remember to add your GEM_PATH to the global PATH. This is what I do: `PATH=$HOME/.gems/bin:$HOME/bin:$PATH GEM_HOME=$HOME/.gems GEM_PATH=$HOME/.gems:/var/lib/gems/2.1.0:/usr/lib/ruby/gems/2.1.0 export PATH GEM_HOME GEM_PATH` – Spone Jan 25 '16 at 19:05
54

For a systemwide Ruby install, become root. For example:

$ sudo gem install hpricot

However, the modern approach in many circumstances, including in development, is to use a tool that lets you easily install and use Ruby as a normal user. This lets you avoid having to become root. There are a few such tools, and the one I use is RVM.

# install rvm into your ~
$ \curl -sSL https://get.rvm.io | bash -s stable

# install latest version of ruby into your ~
$ rvm install ruby

# installs a gem into your ~
$ gem install $SOME_GEM_NAME
yfeldblum
  • 65,165
  • 12
  • 129
  • 169
  • 41
    Do not use sudo because you will install them as root and not have access to them when you're using your normal user. Use RVM and gemsets. – rxgx Apr 08 '11 at 07:44
  • 4
    e.g. `rvm gem install hpricot` – AJP Aug 05 '12 at 23:17
  • 5
    You might want to delete this answer. You'll keep the points, and save people a lot of headache. – Olhovsky May 14 '14 at 11:51
13

I was getting this error on my shared server through 1and1 hosting. my solution was adding the --user-install option, which just installs it for your logged in user (which is all you need in a shared server environment) example; installing sass

gem install sass --user-install
Max
  • 454
  • 4
  • 10
4

If you're using rbenv and this is happening, you need to add the following to your .bash_profile:

export RBENV_ROOT="$HOME/.rbenv"

if [ -d $RBENV_ROOT ]; then
  export PATH="$RBENV_ROOT/bin:$PATH"
  eval "$(rbenv init -)"
fi
Kevin Qi
  • 3,220
  • 2
  • 22
  • 24
0

re-install ruby resolve my problem.

brew install ruby
jackyshan
  • 29
  • 3