how to install gems without sudo

后端 未结 7 494
萌比男神i
萌比男神i 2020-12-05 00:29

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

sudo gem install rails

will work, while only

gem install

相关标签:
7条回答
  • 2020-12-05 00:30

    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"

    0 讨论(0)
  • 2020-12-05 00:31

    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

    0 讨论(0)
  • 2020-12-05 00:31

    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"
    
    0 讨论(0)
  • 2020-12-05 00:36

    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)

    0 讨论(0)
  • 2020-12-05 00:37

    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"
    
    0 讨论(0)
  • 2020-12-05 00:46

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

    0 讨论(0)
提交回复
热议问题