使用rbenv升级Ruby至2.6.5后安装Rails 6.0.2.1路径异常求助
Hey there, let's troubleshoot why your Rails 6.0.2.1 install isn't being recognized even though you've installed it via gem. The core issue here is path priority conflicts between your system/brew-installed Ruby and rbenv-managed Ruby, plus your Rails gem ended up in the wrong gem directory.
Let's Break Down the Problem
From your environment info:
$PATHhas/usr/local/opt/ruby/binbefore~/.rbenv/shims— so your system is using the brew-installed Ruby instead of the rbenv-managed 2.6.5which rubypoints to/usr/local/opt/ruby/bin/ruby(not rbenv's shim), which means yourgem install railscommand installed Rails to brew's Ruby gem directory, not rbenv's 2.6.5 directory- That's why
ls $(rbenv prefix)/bindoesn't showrails— it's not in rbenv's bin folder
Step-by-Step Fixes
1. Fix Your PATH Priority
First, we need to make sure rbenv's shims directory takes precedence over other Ruby paths. Edit your shell configuration file (this depends on your shell: ~/.bash_profile for Bash, ~/.zshrc for Zsh):
- Look for lines that add
/usr/local/opt/ruby/binto your PATH - Move the rbenv initialization code before those lines. The rbenv init line looks like this:
eval "$(rbenv init -)" - Save the file, then restart your terminal or run
source ~/.zshrc(orsource ~/.bash_profile) to apply changes.
2. Ensure rbenv's Ruby 2.6.5 is Active
Set rbenv to use your 2.6.5 version globally (or locally for your project):
- For global use:
rbenv global 2.6.5 - For a specific project directory:
cd your-project-folder rbenv local 2.6.5
Now verify you're using rbenv's Ruby:
which ruby # Should output: /Users/paulinedussart/.rbenv/shims/ruby
3. Uninstall and Reinstall Rails to the Correct Directory
First, remove the Rails that was installed to brew's Ruby:
gem uninstall rails -v 6.0.2.1
Then install Rails using rbenv's Ruby:
gem install rails -v 6.0.2.1 rbenv rehash
4. Clean Up Conflicting Ruby Version Managers
I noticed ~/.rvm/bin in your PATH — rvm and rbenv don't play well together. If you're not using rvm anymore, comment out any rvm-related lines in your shell config file (like [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"), then restart your terminal.
Verify the Fix
Run these commands to confirm everything works:
which rails # Should output: /Users/paulinedussart/.rbenv/shims/rails rails --version # Should show: Rails 6.0.2.1
内容的提问来源于stack exchange,提问作者Pauline DUSSART

