35

On all my gem installs I have to do sudo ? So

sudo gem install rails

will work, while only

gem install rails

will not work. How do I remedy it ?

I have rvm installed -

murtaza@murtaza-dev:~$ which rvm
/home/murtaza/.rvm/bin/rvm

murtaza@murtaza-dev:~$ which gem
/home/murtaza/.rvm/rubies/ruby-1.9.3-p194/bin/gem

However I am also getting this warning when I do any operations with gem -

murtaza@murtaza-dev:~$ gem
/home/murtaza/.rvm/rubies/ruby-1.9.3-p194/bin/gem:4: warning: Insecure world writable dir /opt in PATH, mode 040777

EDIT

I have reinstalled rvm using curl -L get.rvm.io | bash -s stable --auto (without sudo).

However still when I try installing gem it gives me the following error -

murtaza@murtaza-dev:~$ gem install rails
/home/murtaza/.rvm/rubies/ruby-1.9.3-p194/bin/gem:4: warning: Insecure world writable dir /opt in PATH, mode 040777
ERROR:  While executing gem ... (Errno::EACCES)
    Permission denied - /home/murtaza/.gem/specs

EDIT

I did chown on the whole thing but still getting the error -

murtaza@murtaza-dev:~$ sudo chown murtaza.murtaza -R ~/.rvm/*


 murtaza@murtaza-dev:~$ gem install rails
    /home/murtaza/.rvm/rubies/ruby-1.9.3-p194/bin/gem:4: warning: Insecure world writable dir /opt in PATH, mode 040777
    ERROR:  While executing gem ... (Errno::EACCES)
        Permission denied - /home/murtaza/.gem/specs
murtaza52
  • 45,047
  • 27
  • 78
  • 118
  • For the warning, let's see if [this answer](http://stackoverflow.com/questions/2796421/warning-insecure-world-writable-dir-when-i-run-a-ruby-or-gem-command) will help. – Sergio Tulentsev Jul 25 '12 at 06:11
  • Your `chown` looks odd to me. Shouldn't it be `murtaza:murtaza`? – Sergio Tulentsev Jul 25 '12 at 07:03
  • Done but still the error. However I checked the ~/.gem dir, and it doesnt have a specs file in the first place. What should I do ? – murtaza52 Jul 25 '12 at 07:05
  • I'm running out of ideas. I never had this problem. – Sergio Tulentsev Jul 25 '12 at 07:06
  • 2
    please, dont install gems with sudo, USE RVM !!! or any other ruby version mannager – Rodrigo Zurek Jul 25 '12 at 06:07
  • 1
    possible duplicate of ['sudo gem install' or 'gem install' and gem locations](http://stackoverflow.com/questions/2119064/sudo-gem-install-or-gem-install-and-gem-locations) – Nakilon Sep 22 '15 at 12:07
  • chmod not chown. It's telling you that you have a world writable directory. Chown is the change the USER. 'chmod 755 /some/directory' will silence it. – figtrap Nov 10 '16 at 00:56
  • The [answer by @leo.tan](https://stackoverflow.com/a/55273128/2632107) is just about perfect for not requiring elevated permissions, though if ya wanted an example where `sudo` could get involved check a related [answer](https://stackoverflow.com/a/56157908/2632107) for setting up shared Jekyll servers... also that **`Insecure world writable dir /opt in PATH, mode 040777`** error is __bad__ and I'm pretty sure you've got something else messed-up on your system now. – S0AndS0 May 16 '19 at 02:57

7 Answers7

32

Use chown on the whole .rvm and .gem directories back to your user. You probably used sudo before and it screwed up permissions.

sudo chown -R username:group ~/.rvm
sudo chown -R username:group ~/.gem

Of course, change username to your username and group to your group

Pedro Nascimento
  • 11,336
  • 4
  • 30
  • 63
19

When you install them without sudo, Ruby doesn't know where they get installed to. I can't remember where it installs them by default, probably somewhere like ~/.gems or something. Anyway, you can tell Ruby that's where they're installed by setting the GEM_HOME environment variable.

$ # assuming your gems are stored in ~/.gems
$ GEM_HOME="$HOME/.gems" ruby my_program.rb

If that works, then you might put it in your ~/.bashrc (there are a number of possible files this could go in, depending on your system)

Joshua Cheek
  • 26,664
  • 15
  • 67
  • 81
  • 3
    Going to leave this answer because it might be helpful for someone else, but since you edited it to say you're using rvm, it doesn't make sense any more (rvm will manage this variable for you). – Joshua Cheek Jul 25 '12 at 06:17
  • 3
    `GEM_HOME` is also the way if you don't want to use `rvm` when you use system installed Ruby in ephemeral machines e.g. in Docker container. – gertas Oct 18 '14 at 22:20
11

For Mac users, this works for me...

  1. Add GEM_HOME to your .bash_profile

For example, nano ~/.bash_profile and add export GEM_HOME=/Users/Michael/.gem where the path is to your own Home folder

  1. Add the gem executables to your system path

Also in .bash_profile, add export PATH="$GEM_HOME/bin:$PATH"

Source: http://michaelehead.com/2016/02/06/installing-gems-without-sudo.html

eMarine
  • 1,021
  • 1
  • 12
  • 23
6

You should install Ruby Version Manager. It stores installed rubies and gems in your home dir, so now you don't have to use sudo to install gems. It has a lot more features besides this. :)

Sergio Tulentsev
  • 210,238
  • 40
  • 347
  • 343
2

I resolved my issue in the following way: (I have ubuntu 17.10)

Open terminal and type:

echo "gem: --user-install" >> ~/.gemrc

Then add ruby's bin path in .bashrc file

export PATH="$PATH:$HOME/.gem/ruby/x.x.x/bin"
kuntam robo
  • 1,831
  • 1
  • 9
  • 12
2

MacOS

brew install ruby 

edit ~/.zshrc for zsh (for Bash could be something like ~/.bash_aliases)

# By default, binaries installed by gem will be placed into:
# /usr/local/lib/ruby/gems/2.6.0/bin
export GEM_HOME="/usr/local/lib/ruby/gems/"
export PATH="$GEM_HOME/bin:$PATH"
S0AndS0
  • 746
  • 1
  • 6
  • 18
leo.tan
  • 786
  • 6
  • 10
1

try gem install package --install-dir $HOME/.gem.

Don't forget setting the path as it was mentioned before export PATH="$PATH:$HOME/.gem/bin".

Something you have to consider is changing the PATH order like export PATH="$HOME/.gem/bin:$PATH". It might happen for example if you are trying to install rails into your local directory on a OS X system but there is an executable alredy built in. In this case if you don't change the path order you will also receive the following output:

Rails is not currently installed on this system. To get the latest version, simply type:

$ sudo gem install rails

You can then rerun your "rails" command.

And setting the variable GEM_HOME like export GEM_HOME="$HOME/.gems"

Community
  • 1
  • 1