177

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.
NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
derek
  • 9,358
  • 11
  • 53
  • 94
  • You can try to install RVM and install a newer version of ruby like 2.3.3 – Anton Skovorodko Mar 07 '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
  • 1
    The RVM option, and imho even better&simple [install as a pure “user-install”](https://stackoverflow.com/a/18294746/444255) (as opposed to a global, rights-demanding one) might be a much better thing to, than tinkering with rights! – Frank N Nov 09 '17 at 16:41
  • 16
    I wish this stream explained why this OP had this problem and why they should abandon a version of Ruby in favor of both Ruby and a multiple installation manager (RVM or RBEVN). I'm unable to tell for sure whether the advice here is based solely on personal preference. I hope there's more behind this advice. – BaldEagle Jun 24 '18 at 14:48
  • 5
    `sudo gem install bundler` ? – user2023370 Nov 03 '20 at 15:33
  • Sometimes this issue is due to shell user. Below command worked for me. `/bin/bash --login` – Kshitij Apr 08 '22 at 19:40
  • Coming back to this question 6 years later, and working with ruby v 2.7 (installed with apt), I ran into this same error while installing minitest gem, however despite that, a .gem directory got created under $HOME and it had minitest in it... and it works. What sorcery is this – dhanshreeA Jun 09 '22 at 15:47

13 Answers13

244

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
vvvvv
  • 25,404
  • 19
  • 49
  • 81
derek
  • 9,358
  • 11
  • 53
  • 94
  • 12
    Instruction how to install ruby (on rails) is here https://gorails.com/setup/ubuntu/16.04 – Andrzej Rehmann Aug 12 '16 at 07:26
  • 9
    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
  • 7
    I agree with @AntonSkovorodko , it'd be nice if the answer could justify the uninstall of Ruby – blong Apr 13 '17 at 19:30
  • 4
    Just be careful when uninstalling Ruby in Cinnamon environment (happened in Linux Mint 18.2), after reboot I could not run system in graphic mode. When this happens to you, go to to CLI via `Ctrl+Alt+F1` and run following commands (uninstalls and installs Cinnamon again): `sudo apt purge cinnamon && sudo apt install cinnamon` – Kout Aug 07 '17 at 18:28
  • 5
    as this answer ages, you'll probably want to know what the latest available are: `rbenv install -l` for a list all available versions then `rbenv install 2.5.1` for the 2.5.1 version or whatever you'd like. – Paul Sturm Jul 09 '18 at 19:32
  • apt-get is not found in my Mac. I don't know what can I do now – Kathiresan Murugan Jul 12 '19 at 07:14
  • 2
    `E: Package 'python-software-properties' has no installation candidate` – Aaron Franke Sep 26 '19 at 13:59
  • 1
    For those getting `Package 'python-software-properties' has no installation candidate`, you can install it by running: `sudo apt-get install software-properties-common` – Simon Ernesto Cardenas Zarate Jan 23 '20 at 04:22
  • 1
    Following onto what @PaulSturm stated, we're up to 2.7 now. `rbenv install 2.7.1` – johnsampson May 22 '20 at 02:26
  • thank you so much, I have been struggling with different ways to install for half an hour – FAYA Aug 15 '20 at 18:20
  • On Ubuntu 18 the flow of these comands seems to have changed somewhat. sudo apt remove ruby-all-dev sudo apt install rbenv rbenv init add line specified in rbenv init to bash rc with nano ~/.bashrc paste output from previous command at the bottom. source ~/.bashrc mkdir -p "$(rbenv root)"/plugins git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build curl -fsSL https://github.com/rbenv/rbenv-installer/raw/main/bin/rbenv-doctor | bash rbenv install 3.0.2 – Greggory Wiley Jul 21 '21 at 15:05
  • 1
    You could add the export PATHs to `~/.profile` to accommodate other shells, or if you're using zsh, like me change `~/.bashrc` to either `~/.zshrc` or `~/.zprofile`. – zoltankundi Mar 09 '22 at 13:20
  • These days I would recommend asdf over language specific toolings: https://github.com/asdf-vm/asdf-ruby Otherwise you end up installing rbenv for Ruby, NVM/Volta for JavaScript, pyenv for Python etc. etc. with a specific workflow for each – Eric Burel Sep 08 '22 at 05:54
  • fish users should check this answer https://stackoverflow.com/a/73835179/12911698 – Tarik Waleed Sep 24 '22 at 06:31
  • got error at `rbenv install 2.3.1` `make: *** [uncommon.mk:442: rdoc] Error 1` – Dr.X Mar 23 '23 at 13:55
120

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.

Community
  • 1
  • 1
James
  • 1,748
  • 1
  • 11
  • 14
  • 4
    I did this, then got an error about "can't find header files for ruby", then I read this: https://stackoverflow.com/a/4502672/2245874 , and didn't follow all of it but did do sudo apt install ruby-gems, and then it worked for me. – robm May 31 '18 at 23:20
  • 2
    I believe @robm meant ruby-dev not ruby-gems – Altreus May 10 '19 at 18:01
  • 4
    This is probably more appropriate for ruby users that don't need to develop against multiple versions. – danielcooperxyz Jul 30 '19 at 09:15
  • 1
    You decribed a really good Linux way to fix my ruby install without any additional tools, just good old bash :) – Denis Trofimov Jan 11 '20 at 21:04
  • Thanks a lot! Definitely a clear and laconic solution! But instead of using `~/.bashrc` file, I’d suggest writing to the `~/.profile` one that will allow using not only Bash-specific scripts and terminals (that is especially actual if you have to use a `/bin/sh` script). The `~/.bashrc` is exactly for tuning Bash, not all the shell or environment. – oneastok Sep 02 '21 at 15:03
  • fish users should check this answer stackoverflow.com/a/73835179/12911698 – Tarik Waleed Sep 24 '22 at 06:32
48

(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
OFmendez
  • 15
  • 6
Achraf JEDAY
  • 1,936
  • 5
  • 24
  • 33
  • 2
    this is great! nice and succinct. I followed it exactly (except the `.zshrc` changes that I did by hand) and it worked perfectly. In the same way you added how to install `rubygems`, I'd suggest maybe adding how to later update the installed ruby version (I'm guessing you just run the `rbenv install` and `rbenv global` commands again and maybe `rbenv uninstall` to prune outdated versions) – gMale Mar 07 '19 at 16:27
  • 1
    This is amazing! Works perfectly on Ubuntu 18.04 WSL. – robe007 Oct 01 '19 at 23:07
  • 1
    This is more elegant answer. – rashedcs Nov 11 '19 at 22:16
  • Why should I install `rubygems` package separately. Isn't this shipped out with `ruby`? – Muhammed Bera Koç Mar 08 '21 at 19:05
29

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
schnatterer
  • 7,525
  • 7
  • 61
  • 80
27

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.)

Frank N
  • 9,625
  • 4
  • 80
  • 110
  • 4
    I also needed to address /usr/local/bin folder, but this solution did work: "ERROR: While executing gem ... (Gem::FilePermissionError) You don't have write permissions for the /usr/local/bin directory." – eb80 Jul 19 '18 at 15:18
10

I encountered the same error in GitHub Actions. Adding sudo solved the issue.

sudo gem install bundler
Kols
  • 3,641
  • 2
  • 34
  • 42
9

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.

minnow
  • 123
  • 5
  • 3
    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 03:28
  • I get other errors later ERROR: Failed to build gem native extension. – Brad Apr 11 '21 at 21:40
4

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.

Sean Ray
  • 967
  • 2
  • 9
  • 16
1

Reinstalling Compass worked for me.. It's a magic!

sudo gem install -n /usr/local/bin compass
Haje
  • 415
  • 3
  • 5
1

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

Mostafa Wael
  • 2,750
  • 1
  • 21
  • 23
0

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.

0

For Fish Shell users:

To set the environment variable

in your ~/.config/fish/config.fish put :

set -gx PATH $HOME/.rbenv/bin $PATH
set -gx PATH $HOME/.rbenv/plugins/ruby-build/bin $PATH

Those are the equivalent fish commands for:

echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc

Init the rbevn function

also in your ~/.config/fish/config.fish put:

rbenv init - | source

this is the fish equivalent to

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

Source your configs

source ~/.config/fish/config.fish
Tarik Waleed
  • 97
  • 1
  • 8
-1

You should install brew

curl -fsSL -o install.sh https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh
/bin/bash install.sh
brew install chruby ruby-install
brew install cocoapods
brew upgrade cocoapods

Xcode Command Line Tools is needed, if it is not installed, run this command: sudo xcode-select --install.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
manoj kashyap
  • 143
  • 2
  • 9