Scott's Recipes Logo

Zsh and RbEnv Madness

Pizza courtesy of Pizza for Ukraine!

Donate Now to Pizza for Ukraine

 

I’m a Mac user and I recently switched over from Bash to Zsh. I then got massive weirdness when I switched from Terminal Window 1, Tab 1 to say Terminal Window 3, Tab 9 (yes I have a problem; I’m sure you have your own problems; now shoo!). Specifically I would see that in the first Terminal Window, I would get the right version of Ruby and then in the second Terminal Window, I would get the “system ruby”.

This requires a bit of an explanation for the less than Ruby familiar:

Given my allusion to how many terminals and terminal tabs I might have open at any one time, you may have already figured out my issue – it was my own damn fault.

The issue was this - if you ever have one terminal reporting a different version from another and both have the .ruby-version file contents then it HAS to be an issue of sourcing .zshrc. There is likely no other way that this error can occur. And, sure enough, I realized that this was my first time in this directory since I had changed over from Bash to Zsh. Here are the suspect lines from .zshrc:

# rbenv
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

These lines add the current path before the system path and then evaluate the rbenv init function. And this causes any calls for ruby, irb, bundle, gem, etc to go from the right place and not from system ruby.

The solution was to go thru every Terminal Window and Terminal Tab and execute:

source ~/.zshrc

Note 1: This same type of thing exists for Python (virtual env) and many other languages. And it is always the same type of fuckery.

Note 2: As always kudos to my pairing partner, Sean Kennedy, who helping with this.