how to install gems without sudo

Viewed 44885

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
7 Answers

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"

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"

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"

Related