Lubuntu 18.04虚拟机中ruby、gem、rbenv命令找不到求助
Hey there, I feel your pain—spending a whole day wrestling with environment config is so draining. Let's get your rbenv-managed Ruby back up and running quickly.
First, let's diagnose the issue: Your PATH includes ~/.rbenv/bin, but rbenv needs one more critical piece to work properly: shims. The shims directory (~/.rbenv/shims) is where rbenv puts links to your installed Ruby version's commands (like ruby, gem, bundle), and right now that directory isn't in your PATH. That's why your system can't find those commands and is suggesting you install the system Ruby instead.
Here's how to fix this:
1. Temporary Fix (Test in Current Terminal)
First, let's get your commands working right now without restarting anything. Run these two commands in your terminal:
export PATH="$HOME/.rbenv/bin:$PATH" eval "$(rbenv init -)"
Now test if ruby works:
ruby -v
You should see ruby 2.4.0p0 (or similar) if this fixes it. If it does, the problem is that rbenv's initialization isn't being loaded automatically when you open a terminal.
2. Permanent Fix (Save for Future Terminals)
We need to add the rbenv initialization to your shell config file. Lubuntu uses bash by default, so we'll edit ~/.bashrc:
- Open the file in your editor:
vi ~/.bashrc - Scroll to the bottom and add these two lines:
export PATH="$HOME/.rbenv/bin:$PATH" eval "$(rbenv init -)" - Save and exit (in vi: press
Esc, type:wq, then press Enter). - Make the changes take effect immediately in your current terminal:
source ~/.bashrc
3. Verify Your Setup
Run these commands to make sure everything is set correctly:
- Check your Ruby version:
ruby -v(should show 2.4.0) - Confirm rbenv's global version:
rbenv global(should output2.4.0) - Test bundler:
bundle --version
Why This Happened
When you install rbenv, the final step is adding those initialization lines to your shell config. It's easy to miss this step, especially when you're tired and rushing through setup. The eval "$(rbenv init -)" command tells rbenv to inject the shims directory into your PATH, which lets your system find the rbenv-managed Ruby commands instead of looking for the system Ruby.
This isn't a permission issue—your user has access to the rbenv directories, as you can see them. It's just a missing shell configuration step.
内容的提问来源于stack exchange,提问作者user2229608

