'You don't have write permissions for the /var/lib/gems/2.3.0 directory
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.
Solution 1:[1]
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 libxml2-dev libxslt1-dev libcurl4-openssl-dev 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
Solution 2:[2]
If you want to use the distribution Ruby instead of rb-env/rvm, you can set up a GEM_HOME
for your current user. Start by creating a directory to store the Ruby gems for your user:
$ mkdir ~/.ruby
Then update your shell to use that directory for GEM_HOME
and to update your PATH
variable to include the Ruby gem bin directory.
$ echo 'export GEM_HOME=~/.ruby/' >> ~/.bashrc
$ echo 'export PATH="$PATH:~/.ruby/bin"' >> ~/.bashrc
$ source ~/.bashrc
(That last line will reload the environment variables in your current shell.)
Now you should be able to install Ruby gems under your user using the gem
command. I was able to get this working with Ruby 2.5.1 under Ubuntu 18.04. If you are using a shell that is not Bash, then you will need to edit the startup script for that shell instead of bashrc
.
Solution 3:[3]
(January 2019) To install Ruby using the Rbenv script, follow these steps:
1. First, update the packages index and install the packages required for the ruby-build tool to build Ruby from source:
sudo apt-get remove ruby
sudo apt update
sudo apt install git curl libssl-dev libreadline-dev zlib1g-dev autoconf bison build-essential libyaml-dev libreadline-dev libncurses5-dev libffi-dev libgdbm-dev
2. Next, run the following curl command to install both rbenv and ruby-build:
curl -sL https://github.com/rbenv/rbenv-installer/raw/main/bin/rbenv-installer | bash -
3. Add $HOME/.rbenv/bin to the system PATH.
If you are using Bash, run:
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
source ~/.bashrc
If you are using Zsh run:
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(rbenv init -)"' >> ~/.zshrc
source ~/.zshrc
4. Install the latest stable version of Ruby and set it as a default version with:
rbenv install 2.5.1
rbenv global 2.5.1
To list all available Ruby versions you can use:
rbenv install -l
5. Verify that Ruby was properly installed by printing out the version number:
ruby -v
# Output
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
SOURCE: How To Install Ruby on Ubuntu 18.04
EDIT: Install rubygems:
sudo apt-get install rubygems
Solution 4:[4]
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.)
Solution 5:[5]
Ubuntu 20.04:
Option 1 - set up a gem installation directory for your user account
For bash (for zsh, we would use .zshrc
of course)
echo '# Install Ruby Gems to ~/gems' >> ~/.bashrc
echo 'export GEM_HOME="$HOME/gems"' >> ~/.bashrc
echo 'export PATH="$HOME/gems/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
Option 2 - use snap
Uninstall the apt-version (ruby-full
) and reinstall it with snap
sudo apt-get remove ruby
sudo snap install ruby --classic
Solution 6:[6]
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.
Solution 7:[7]
I encountered the same error in GitHub Actions. Adding sudo
solved the issue.
sudo gem install bundler
Solution 8:[8]
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.
Solution 9:[9]
Reinstalling Compass worked for me.. It's a magic!
sudo gem install -n /usr/local/bin compass
Solution 10:[10]
Did you try running rbenv rehash
before installing bundler?
I ran into this using WSL2 running Ubuntu 20.04.
I installed rbenv and ruby-build through GitHub and proceeded to install ruby 3.0.1 and set that as the global version.
? ~ rbenv install 3.0.1
Downloading ruby-3.0.1.tar.gz...
-> https://cache.ruby-lang.org/pub/ruby/3.0/ruby-3.0.1.tar.gz
Installing ruby-3.0.1...
Installed ruby-3.0.1 to /home/kayla/.rbenv/versions/3.0.1
? ~ rbenv global 3.0.1
? ~ ruby -v
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux-gnu]
? ~ gem install bundler
Fetching bundler-2.2.20.gem
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /var/lib/gems/2.7.0 directory.
My ruby was installed in the same location:
? ~ which ruby
/usr/bin/ruby
Instead of re-installing rbenv and ruby-build entirely, all I needed to do was rehash before installing bundler:
? ~ rbenv rehash
? ~ gem install bundler
Fetching bundler-2.2.20.gem
Successfully installed bundler-2.2.20
Parsing documentation for bundler-2.2.20
Installing ri documentation for bundler-2.2.20
Done installing documentation for bundler after 3 seconds
1 gem installed
One clue that might've hinted to this earlier, was that the return value for ruby -v
before I rehashed was ruby 2.7.0, not 3.0.1.
Solution 11:[11]
Using the snap version of Ruby solved the problem for me:
Remove the default version:
sudo apt-get remove ruby
Install another one using snap:
sudo snap install ruby --classic
You can now run
gem install bundler
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow