Install Latest Stable Version of Ruby Using rbenv

rbenv install -l | awk -F '.' '
   /^[[:space:]]*[0-9]+\.[0-9]+\.[0-9]+[[:space:]]*$/ {
      if ( ($1 * 100 + $2) * 100 + $3 > Max ) { 
         Max = ($1 * 100 + $2) * 100 + $3
         Version=$0
         }
      }
   END { print Version }'
  • Take the biggest version (sorted order or not)

If list is sorted a simpler sed (posix version) is enough

rbenv install -l | sed -n '/^[[:space:]]*[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}[[:space:]]*$/ h;${g;p;}'

Simple solution (directly installs latest stable version):

rbenv install $(rbenv install -l | grep -v - | tail -1)

Explanation:

rbenv install -l | grep -v - | tail -1

Filters out all versions that contain a hyphen -, which is all non-MRI versions and prerelease MRI versions. Then selects the last one, guaranteed to be the highest because ruby-build output is already sorted by version number ascending.