I have ruby installed on my ubuntu 16.04.

$which ruby  

/usr/bin/ruby

$ruby -v 

ruby 2.3.0p0 (2015-12-25) [x86_64-linux-gnu]

$gem install bundler 

ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /var/lib/gems/2.3.0 directory.

Any help will be greatly appreciated!

share|improve this question
    
You can try to install RVM and install a newer version of ruby like 2.3.3 – Anton Skovorodko Mar 7 '17 at 15:04
    
For comment 3: the package "python-software-properties" is not available you should replace for software-properties-common – Francisco Pedraza Jul 21 '17 at 16:47
    
The RVM option, and imho even better&simple install as a pure “user-install” (as opposed to a global, rights-demanding one) might be a much better thing to, than tinkering with rights! – Frank Nocke Nov 9 '17 at 16:41
up vote 61 down vote accepted

You first need to uninstall the ruby installed by Ubuntu with something like sudo apt-get remove ruby.

Then reinstall ruby using rbenv and ruby-build according to their docs:

cd $HOME
sudo apt-get update 
sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev

git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL

git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL

rbenv install 2.3.1
rbenv global 2.3.1
ruby -v

The last step is to install Bundler:

gem install bundler
rbenv rehash

Then enjoy!

Derek

share|improve this answer
2  
Instruction how to install ruby (on rails) is here gorails.com/setup/ubuntu/16.04 – Hoto Aug 12 '16 at 7:26
    
I'm on ubuntu after following above steps not helps... – Raja Simon Oct 26 '16 at 6:11
1  
I'm kind of reticent to do the uninstall, since it requires uninstalling dependent programs too (vim-gnome for example). I guess I would prefer to know why in the first place it needs to write in /var/lib/... for a user install... Did I do a sudo gem install when not needed? – PlasmaBinturong Nov 15 '16 at 21:30
2  
Why uninstall is needed? – Anton Skovorodko Mar 7 '17 at 15:04
    
I agree with @AntonSkovorodko , it'd be nice if the answer could justify the uninstall of Ruby – blong Apr 13 '17 at 19:30

Try using chown -R on the var/lib/gems directory, assigning ownership to the user [rubyusername] in this example, the user that will be installing and developing with gems.

 # chown -R rubyusername:rubyusername /var/lib/gems 

This recursively changes everything under the gems directory. For extra security on multi-user systems, you can also create a group, rather than chowning the individual rubyusername, and add users to that group.

share|improve this answer
2  
It can solve that problem, but another issue pops up: ERROR: While executing gem ... (Gem::FilePermissionError) You don't have write permissions for the /usr/local/bin directory ; so I guess something else is wrong instead of the permission. – derek Jun 16 '16 at 3:28

Building on derek's answer above, it is generally not recommended to use the system provided Ruby instance for your own development work, as system tools might depend on the particular version or location of the Ruby install. Similar to this answer for Mac OSX, you will want to follow derek's instructions on using something like rbenv (RVM is a similar alternative) to install your own Ruby instance.

However, there is no need to uninstall the system version of Ruby, the rbenv installation instructions provide a mechanism to make sure that the instance of Ruby available in your shell is the rbenv instance, not the system instance. This is the

echo 'eval "$(rbenv init -)"' >> ~/.bashrc

line in derek's answer.

share|improve this answer

Rather than changing owners, which might lock out other local users, or –some day– your own ruby server/deployment-things... running under a different user...

I would rather simply extend rights of that particular folder to... well, everybody:

cd /var/lib
sudo chmod -R a+w gems/

(I did encounter your error as well. So this is fairly verified.)

share|improve this answer

Try sudo chmod 777 -R /var/lib/gems. It will give read and write permissions. It is not much advised. But solves the problem in an easier way.

share|improve this answer

protected by Community Oct 5 '17 at 12:34

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.