Ruby gem permission denied /var/lib/gems using Ubuntu

gemrubyUbuntu

I'm trying to install Rails using Gem and did the following from the terminal:

sudo apt-get install ruby-full build-essential
sudo apt-get install rubygems
export PATH=/var/lib/gems/1.8/bin:$PATH
gem install rails

Then I get the following message:

ERROR: While executing gem … (Errno:EACCES)
Permission denied – /var/lib/gems

I've followed the instruction from Ubuntu Help Guide & I'm using Oracle VM VirtualBox to run Ubuntu under Windows 7 and have RubyGems v1.8.15.

Best Answer

You need to prefix the gem command with sudo because /var/lib/gems is owned by root. You could also take a look at RVM which allows really easy installation and management of gems and Ruby versions. Best part, it's all in your home dir!

EDIT: per @AndrewMarshall's comment bellow, rbenv is an alternative to RVM.