Search code examples
rubyubuntu-20.04rbenv

rbenv says installed version is not installed


I just installed ruby 3.0.4 using rbenv install 3.0.4, which worked, but rbenv still says:

$ rbenv versions
rbenv: version `3.0' is not installed (set by /home/dombek/code/.ruby-version)
  system
  3.0.4

Shouldn't it find 3.0.4 when 3.0 is requested? How can I fix that?


Solution

  • Shouldn't it find 3.0.4 when 3.0 is requested?

    Nope. This is expected behaviour. rbenv expects the exact version specified in the .ruby-version file to be installed and set for use.

    How can I fix that?

    Depends what you mean by fix. The obvious answer here is to just specify the correct version:

    echo '3.0.4' > /home/dombek/code/.ruby-version
    

    Alternatively, you can set 3.0.4 globally or locally using rbenv:

    rbenv global 3.0.4
    # or
    rbenv local 3.0.4
    

    After changing the version, you should rehash rbenv to apply changes:

    rbenv rehash
    

    Finally, I'm not sure how practical this is, but you could theoretically write a bash script that lets you specify a ruby version like "3.0". Something like:

    set_nearest_rbenv_version() {
      desired_version=$1.*
      nearest_version=$(rbenv versions --bare | grep -E "^$desired_version" | tail -1)
      if [ -n "$nearest_version" ]; then
        rbenv global $nearest_version
        echo "Set to nearest version: $nearest_version"
      else
        echo "No matching versions found."
      fi
    }
    
    set_nearest_rbenv_version 3.0
    

    ^ untested.