Ruby-on-rails 'sudo gem install' 或 'gem install' 和 gem 位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2119064/
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
'sudo gem install' or 'gem install' and gem locations
提问by raphael_turtle
Running 'sudo gem list --local' and 'gem list --local' give me differing results. My gem path is set to my home folder and only contains the gems from 'gem list --local'.
运行 ' sudo gem list --local' 和 ' gem list --local' 给我不同的结果。我的 gem 路径设置为我的主文件夹,并且只包含来自“ gem list --local”的 gem 。
It's probably not good to have gems installed in different directories on my computer, so should I have the gem path set differently, and should I always use sudowhen installing something?
在我的计算机上的不同目录中安装 gems 可能不太好,所以我应该设置不同的 gem 路径,并且sudo在安装某些东西时我应该总是使用吗?
my ~/.profile
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"
~/.bash_profile is empty.
~/.bash_profile 为空。
回答by jonnii
Contrary to all the other posts I suggest NOT using sudowhen installing gems.
与所有其他帖子相反,我建议sudo在安装 gems 时不要使用。
Instead I recommend you install RVMand start a happy life with portable gem homes and different version of Ruby all living under one roof.
相反,我建议您安装RVM并开始一个幸福的生活,便携式 gem 之家和不同版本的 Ruby 都生活在一个屋檐下。
For the uninitiated, from the documentation:
对于初学者,来自文档:
RVM is a command line tool which allows us to easily install, manage and work with multiple ruby environments and sets of gems.
RVM 是一个命令行工具,它允许我们轻松安装、管理和使用多个 ruby 环境和一组 gem。
The reason why installing gems with sudois worse than just gem installis because it installs the gems for ALL USERSas root. This might be fine if you're the only person using the machine, but if you're not it can cause weirdness.
安装 gems 的原因sudo不仅仅是gem install因为它将所有用户的 gems 安装为root. 如果您是唯一使用这台机器的人,这可能没问题,但如果您不是,则可能会导致怪异。
If you decide you want to blow away all your gems and start again it's much easier, and safer, to do so as a non-root user.
如果您决定要吹掉所有宝石并重新开始,那么以非 root 用户身份这样做会更容易、更安全。
If you decide you want to use RVMthen using sudowill cause all kinds of weirdness because each Ruby version you install through RVMhas its own GEM_HOME.
如果您决定要使用,RVM那么使用sudo会导致各种奇怪的情况,因为您安装的每个 Ruby 版本RVM都有自己的 GEM_HOME。
Also, it's nice if you can make your development environment as close to your production environment as possible, and in production you'll most likely install gems as a non-root user.
此外,如果您能让您的开发环境尽可能接近您的生产环境,那就太好了,并且在生产环境中,您很可能会以非 root 用户身份安装 gems。
回答by Nils
You can also install gems in your local environment (without sudo) with
您还可以在本地环境中安装 gems(没有sudo)
gem install --user-install <gemname>
I recommend that so you don't mess with your system-level configuration even if it's a single-user computer.
我建议这样做,这样即使是单用户计算机,也不会弄乱系统级配置。
You can check where the gems go by looking at gempaths with gem environment. In my case it's "~/.gem/ruby/1.8".
您可以通过查看 gempath 来检查 gem 的去向gem environment。就我而言,它是“~/.gem/ruby/1.8”。
If you need some binaries from local installs added to your path, you can add something to your bashrc like:
如果您需要将本地安装的一些二进制文件添加到您的路径中,您可以向您的 bashrc 添加一些内容,例如:
if which ruby >/dev/null && which gem >/dev/null; then
PATH="$(ruby -r rubygems -e 'puts Gem.user_dir')/bin:$PATH"
fi
回答by allyraza
Better yet, put --user-installin your ~/.gemrc file so you don't have to type it every time
更好的是,放入--user-install你的 ~/.gemrc 文件,这样你就不必每次都输入它
gem: --user-install
回答by David Salamon
In case you
如果你
- installed ruby gems with sudo
- want to install gems without sudo
- don't want to install rvm/rbenv
- 使用 sudo 安装 ruby gems
- 想在没有 sudo 的情况下安装 gems
- 不想安装 rvm/rbenv
add the following to your .bash_profile:
将以下内容添加到您的.bash_profile:
export GEM_HOME=/Users/?your_user?/.gem
export PATH="$GEM_HOME/bin:$PATH"
Open a new tab in Terminal OR source ~/.bash_profileand you're good to go!
在终端 OR 中打开一个新选项卡,source ~/.bash_profile您就可以开始了!
回答by Todd Vierling
Related (for bundler users), if you want a lighter alternative to RVM which will put everything in a user-specific well known directory, I recommend using:
相关(对于 bundler 用户),如果你想要一个更轻的 RVM 替代品,它将把所有东西都放在一个用户特定的众所周知的目录中,我建议使用:
bundle install --path $HOME/.gem
if you want to install gems to the same place that
如果您想将 gems 安装到相同的位置
gem install --user-install GEMNAME
will install them, .gem/ruby/RUBYVERSIONin your homedir. (See the other comment on this question about --user-install.)
将.gem/ruby/RUBYVERSION在您的 homedir 中安装它们。(请参阅有关此问题的其他评论--user-install。)
This will make the gems visible to gem list, uninstallable via gem uninstall, etc. without needing sudoaccess. Runnable scripts installed by gem or bundler can be put into your path by adding
这将使 gems 可见gem list,可通过 卸载gem uninstall,无需sudo访问。可以通过添加将 gem 或 bundler 安装的可运行脚本放入您的路径中
$HOME/.gem/ruby/RUBYVERSION/bin
to your $PATH. gemitself tells you about this if it isn't set when you do gem install --user-install.
到您的$PATH. gem如果您在执行时未设置它,它本身会告诉您这一点gem install --user-install。
回答by Tasos Koutoumanos
sudo gem install --no-user-install <gem-name>
will install your gem globally, i.e. it will be available to all user's contexts.
将全局安装您的 gem ,即它将对所有用户的上下文可用。
回答by Chadi
You can install gems into a specific folder (example vendor/) in your Rails app using :
您可以使用以下命令将 gems 安装到 Rails 应用程序中的特定文件夹(例如 vendor/)中:
bundle install --path vendor

