rbenv 不改变 ruby 版本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10940736/
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
rbenv not changing ruby version
提问by ajorgensen
I installed rbenv according to the github directions. I am running OSX but I have tried this on a Ubuntu 12.04 VM and got the same results. The following is what i get in my terminal when I try to change ruby versions:
我根据github的说明安装了rbenv。我正在运行 OSX,但我已经在 Ubuntu 12.04 VM 上尝试过,并得到了相同的结果。以下是我尝试更改 ruby 版本时在终端中得到的信息:
rbenv versions
* 1.9.3-p0 (set by /Users/user/.rbenv/version)
1.9.3-p125
rbenv global
1.9.3-p0
rbenv rehash
ruby -v
ruby 1.8.7 (2011-12-28 patchlevel 357) [universal-darwin11.0]
which ruby
/usr/bin/ruby
Anyone have any ideas as to why rbenv isn't switching the ruby version like it thinks it is? Also there is no .rbenv file in the local directory that would be causing the ruby version to default to 1.8.7
任何人都知道为什么 rbenv 没有像它认为的那样切换 ruby 版本?本地目录中也没有 .rbenv 文件会导致 ruby 版本默认为 1.8.7
rbenv local
rbenv: no local version configured for this directory
回答by defvol
Check that PATH contains $HOME/.rbenv/shims and $HOME/.rbenv/bin
检查 PATH 是否包含 $HOME/.rbenv/shims 和 $HOME/.rbenv/bin
$ env | grep PATH
Also check that you have the following in your ~/.bash_profile if using bash or ~/.zshenv if using zsh
如果使用 bash 或 ~/.zshenv 如果使用 zsh,还要检查您的 ~/.bash_profile 中是否包含以下内容
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
NOTE: Make sure it's the last setting in your ~/.bash_profile . I ran into an issue where I installed a program that updated my .bash_profile and reset PATH.
注意:确保它是 ~/.bash_profile 中的最后一个设置。我遇到了一个问题,我安装了一个更新我的 .bash_profile 并重置 PATH 的程序。
Finally, make sure your $HOMEfolder doesn't have a .ruby-versionfile that you may have created by accident if you were to have done $ rbenv local <ruby-version>in your $HOMEfolder. Doing $ rbenv global <ruby-version>' modifies the$HOME/.rbenv/versionfile, and the existence of a.ruby-versionfile in the$HOMEfolder would override the version set by$HOME/.rbenv/version`.
确保最后,让你的$HOME文件夹没有一个.ruby-version您可能意外创建文件,如果你已经做了$ rbenv local <ruby-version>你的$HOME文件夹中。执行$ rbenv global <ruby-version>' modifies the$HOME/.rbenv/version file, and the existence of a.ruby-version file in the$HOME folder would override the version set by$HOME/.rbenv/version`。
From the docs:
从文档:
Choosing the Ruby Version When you execute a shim, rbenv determines which Ruby version to use by reading it from the following sources, in this order:
The RBENV_VERSION environment variable, if specified. You can use the rbenv shell command to set this environment variable in your current shell session.
The first .ruby-version file found by searching the directory of the script you are executing and each of its parent directories until reaching the root of your filesystem.
The first .ruby-version file found by searching the current working directory and each of its parent directories until reaching the root of your filesystem. You can modify the .ruby-version file in the current working directory with the rbenv local command.
The global ~/.rbenv/version file. You can modify this file using the rbenv global command. If the global version file is not present, rbenv assumes you want to use the "system" Ruby—i.e. whatever version would be run if rbenv weren't in your path.
选择 Ruby 版本当您执行 shim 时,rbenv 通过从以下来源读取它来确定要使用的 Ruby 版本,按此顺序:
RBENV_VERSION 环境变量(如果指定)。您可以使用 rbenv shell 命令在当前 shell 会话中设置此环境变量。
通过搜索您正在执行的脚本的目录及其每个父目录直到到达文件系统的根目录,找到第一个 .ruby-version 文件。
通过搜索当前工作目录及其每个父目录直到到达文件系统的根目录找到的第一个 .ruby-version 文件。您可以使用 rbenv local 命令修改当前工作目录中的 .ruby-version 文件。
全局 ~/.rbenv/version 文件。您可以使用 rbenv 全局命令修改此文件。如果全局版本文件不存在,rbenv 假定您要使用“系统”Ruby——即如果 rbenv 不在您的路径中,将运行任何版本。
回答by mjmdavis
I fixed this by adding the following to my ~/.bash_profile:
我通过将以下内容添加到我的 ~/.bash_profile 来解决这个问题:
#PATH for rbenv
export PATH="$HOME/.rbenv/shims:$PATH"
This is what is documented at https://github.com/sstephenson/rbenv.
这是https://github.com/sstephenson/rbenv 中记录的内容。
From what I can tell there is no ~/.rbenv/bin directory, which was mentioned in the post by @rodowi.
据我所知,没有@rodowi 在帖子中提到的 ~/.rbenv/bin 目录。
回答by alphadogg
This may be an old question, but Google led me here and, for posterity sake, thought I'd share.
这可能是一个老问题,但谷歌把我带到这里,为了后代,我想我会分享。
My problem persisted after many of the recommended solutions above. Like the OP, I installed rbenv and then a ruby version, but my laptop defaulted to system. What I had overlooked was that when I ran:
在上述许多推荐的解决方案之后,我的问题仍然存在。像OP一样,我安装了rbenv,然后安装了ruby版本,但我的笔记本电脑默认为系统。我忽略的是,当我跑的时候:
[~/.rbenv] $ rbenv versions
* system (set by /Users/alphadogg/.rbenv/version)
2.0.0-p247
IOW, it was still defaulting to system. A quick
IOW,它仍然默认为系统。一个快速
[~/.rbenv] $ rbenv local 2.0.0-p247
switched it to the new version.
将其切换到新版本。
回答by Sky Notify
First step is to find out which ruby is being called:which ruby
第一步是找出正在调用哪个 ruby:which ruby
Your system says:/usr/bin/ruby
你的系统说:/usr/bin/ruby
This is NOT the shim used by rbenv, which (on MacOS) should look like:/Users/<username>/.rbenv/shims/ruby
这不是 rbenv 使用的垫片,它(在 MacOS 上)应该如下所示:/Users/<username>/.rbenv/shims/ruby
The shim is actually a script that acts like a redirect to the version of ruby you set.rbenv global 1.9.3
rbenv local --unset
rbenv shell --unset
I recommend that for trouble shooting you unset the project specific "local" version, and the shell specific "shell" version and just test using the "global" version setting which is determined in a plain text file in ~/.rbenv/verion which will just be the version number "1.9.3" in your case. You can do "ls -laG" in the root of your project folder (not the home folder) to make sure there is no longer a ".ruby-version" file there.
shim 实际上是一个脚本,它的作用类似于重定向到您设置的 ruby 版本。rbenv global 1.9.3
rbenv local --unset
rbenv shell --unset
我建议您在排除故障时取消设置特定于项目的“本地”版本和特定于 shell 的“shell”版本,然后使用在 ~/.rbenv/verion 中的纯文本文件中确定的“全局”版本设置进行测试在您的情况下,将只是版本号“1.9.3”。您可以在项目文件夹(不是主文件夹)的根目录中执行“ls -laG”以确保那里不再有“.ruby-version”文件。
You can use "rbenv versions" to identify which version rbenv is set to use (and the location and name of the file that is setting that).
您可以使用“rbenv 版本”来确定 rbenv 设置为使用哪个版本(以及设置该版本的文件的位置和名称)。
rbenv versions
rbenv versions
NONE OF THAT MATTERS: until you set the path correctly.
无关紧要:直到您正确设置路径。
Use this to make sure your *MacOS will obey you:eval "$(rbenv init -)"
使用它来确保你的 *MacOS 会服从你:eval "$(rbenv init -)"
Followed by:which ruby
其次是:which ruby
To make sure it looks like: /Users//.rbenv/shims/ruby
确保它看起来像:/Users//.rbenv/shims/ruby
Then add the line to your profile so it runs each time you open a new terminal window:~/.bash_profile
eval "$(rbenv init -)"
然后将该行添加到您的配置文件中,以便每次打开新的终端窗口时它都会运行:~/.bash_profile
eval "$(rbenv init -)"
There are other ways to modify the path, feel free to substitute any of them instead of running the rbenv init.
还有其他修改路径的方法,可以随意替换其中任何一个,而不是运行 rbenv init。
NOTE: reinstall Rails with:gem install rails
注意:使用以下命令重新安装 Rails:gem install rails
If you were trying to run Ruby on Rails, then you need to have this all working first, then install the rails gem again. A previous install of Rails will use a hard coded path to the wrong ruby and several other things will be in the wrong place, so just install the gem again.
如果您尝试运行 Ruby on Rails,那么您需要先让这一切正常运行,然后再次安装 rails gem。以前安装的 Rails 将使用硬编码路径到错误的 ruby 并且其他一些东西将位于错误的位置,因此只需再次安装 gem。
P. S. If your MacOS won't obey you (*mentioned above) then you may have to find another way to modify your path, but that's unlikely to be a problem because "Macs just work" ;)
PS 如果您的 MacOS 不服从您(* 上面提到的),那么您可能必须找到另一种方法来修改您的路径,但这不太可能成为问题,因为“Mac 可以正常工作”;)
回答by Neal
I just found this same problem. What I did was uninstall rbenv (via homebrew) and reinstall it. I also added
我刚刚发现了同样的问题。我所做的是卸载rbenv(通过自制软件)并重新安装它。我还加了
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
into ~/.bash_profile when I reinstalled rbenv. Works perfectly now.
当我重新安装 rbenv 时进入 ~/.bash_profile 。现在完美运行。
回答by Peter Piper
In my case changing the ~/.zshenvdid not work. I had to make the changes inside ~/.zshrc.
在我的情况下,更改~/.zshenv不起作用。我不得不在里面进行更改~/.zshrc。
I just added:
我刚刚补充说:
# Include rbenv for ZSH
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
at the top of ~/.zshrc, restarted the shell and logged out.
在顶部~/.zshrc,重新启动外壳并注销。
Check if it worked:
检查它是否有效:
? ~ rbenv install 2.4.0
? ~ rbenv global 2.4.0
? ~ rbenv global
2.4.0
? ~ ruby -v
ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-darwin16]
回答by Singhak
回答by drjorgepolanco
If you are using bash, go to
如果您使用的是bash,请转到
~/.bash_profile
and add the following line (if it's not already there)
并添加以下行(如果它还没有)
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
If you are using Zsh, go to
如果您使用的是 Zsh,请转到
~/.zshrc
and add the same line of code, at the end of the .zshrc file.
并在 .zshrc 文件的末尾添加相同的代码行。
Then simply restart your terminal and it should be fine now.
然后只需重新启动您的终端,现在应该没问题了。
回答by Rootical V.
As for me the easiest way to use rbenvalong with zshis adding rbenvto plugins section in .zshrcconfig. In my case it looks similar to:
至于我用最简单的方式rbenv沿zsh被添加rbenv到插件部分.zshrc的配置。就我而言,它看起来类似于:
# Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/plugins/*)
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(git bower rails ruby rbenv gulp npm node nvm sublime)
After that there're no problems when installing, switching, using ruby versions with help of rbenv.
之后在安装、切换、使用 ruby 版本时都没有问题rbenv。
Mind to restart your terminal session after made changes.
请注意在进行更改后重新启动终端会话。
回答by AmitF
run:
跑:
rbenv init
After I ran that, when i set my local rbenv version:
运行之后,当我设置本地 rbenv 版本时:
rbenv local 2.4.0
then my ruby -vand my rbenv localversions coincided.
然后我ruby -v和我的rbenv local版本重合。
Note: You might also want to exit the directory you're in and then go back into it, i've noticed that was necessary for me in order to get things to work.
注意:你可能还想退出你所在的目录,然后再回到它,我注意到这对我来说是必要的,以便让事情正常工作。

