ruby 如何在没有 sudo 的情况下安装 gems

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11643629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:19:23  来源:igfitidea点击:

how to install gems without sudo

rubygem

提问by murtaza52

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

在我所有的 gem 安装中,我必须执行 sudo 吗?所以

sudo gem install rails

须藤宝石安装导轨

will work, while only

会起作用,而只有

gem install rails

宝石安装导轨

will not work. How do I remedy it ?

不管用。我该如何补救?

I have rvm installed -

我已经安装了 rvm -

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 -

但是,当我使用 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).

我已经使用curl -L get.rvm.io | bash -s stable --auto(没有 sudo)重新安装了 rvm 。

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

但是,当我尝试安装 gem 时,它仍然给我以下错误 -

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

回答by Pedro Nascimento

Use chownon the whole .rvmand .gemdirectories back to your user. You probably used sudobefore and it screwed up permissions.

使用chown整个.rvm.gem目录返回给您的用户。您可能sudo以前使用过,但它搞砸了权限。

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

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

当然,将用户名更改为您的用户名,将组更改为您的组

回答by Joshua Cheek

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.

当你在没有 sudo 的情况下安装它们时,Ruby 不知道它们被安装到哪里。我不记得它默认安装它们的位置,可能是 ~/.gems 之类的地方。无论如何,您可以通过设置 GEM_HOME 环境变量来告诉 Ruby 安装它们的位置。

$ # 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)

如果可行,那么您可以将它放在 ~/.bashrc 中(根据您的系统,可以放入许多可能的文件)

回答by eMarine

For Mac users, this works for me...

对于 Mac 用户,这对我有用...

  1. Add GEM_HOME to your .bash_profile
  1. 将 GEM_HOME 添加到您的 .bash_profile

For example, nano ~/.bash_profileand add export GEM_HOME=/Users/Michael/.gemwhere the path is to your own Home folder

例如,nano ~/.bash_profileGEM_HOME=/Users/Michael/.gem在路径为您自己的主文件夹的位置添加导出

  1. Add the gem executables to your system path
  1. 将 gem 可执行文件添加到您的系统路径

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

同样在.bash_profile, 添加export PATH="$GEM_HOME/bin:$PATH"

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

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

回答by Sergio Tulentsev

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

您应该安装Ruby 版本管理器。它将安装的 rubies 和 gems 存储在您的主目录中,因此现在您不必使用sudo来安装 gems。除此之外,它还有很多功能。:)

回答by djangodude

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

我通过以下方式解决了我的问题:(我有 ubuntu 17.10)

Open terminal and type:

打开终端并输入:

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

Then add ruby's bin path in .bashrc file

然后在.bashrc文件中添加ruby的bin路径

export PATH="$PATH:$HOME/.gem/ruby/x.x.x/bin"

回答by Hola Soy Edu Feliz Navidad

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

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

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

不要忘记设置前面提到的路径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:

您必须考虑的是更改 PATH 顺序,例如export PATH="$HOME/.gem/bin:$PATH". 例如,如果您尝试将 rails 安装到 OS X 系统上的本地目录中,但已经内置了一个可执行文件,则可能会发生这种情况。在这种情况下,如果您不更改路径顺序,您还将收到以下输出:

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.

此系统上当前未安装 Rails。要获取最新版本,只需键入:

$ sudo gem 安装导轨

然后,您可以重新运行“rails”命令。

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

并设置变量,GEM_HOMEexport GEM_HOME="$HOME/.gems"

回答by leo.tan

MacOS

苹果系统

brew install ruby 

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

~/.zshrc为 zsh编辑 (对于 Bash 可能类似于~/.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"