Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Install Latest Stable Version of Ruby Using rbenv

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.


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;}'

One should first update the ruby-build to get the latest version of ruby while install using rbenv.. Follow the below steps:

  • brew reinstall --HEAD ruby-build ( if rbenv is already installed brew may through some error then to move ahead and simply
    • brew unlink ruby-build and
    • brew install --HEAD ruby-build )
  • brew upgrade
  • and then you could use one of the above approaches suggested above to install automatically the latest version or simply rbenv install <required latest version>

working in macOS 10.13.6


After quite a bit of trial-and-error I figured out a way to grab the latest stable version from this list. This isn't perfect as it just grabs the correct pattern and the last version of it, but it should get the job done. It will work as long as the versions are in order.

This will produce 2.2.2

rbenv install -l | grep -P "^  [[:digit:]]\.[[:digit:]]\.[[:digit:]]$" | tail -1

We can plug that result into rbenv install like this:

rbenv install $(rbenv install -l | grep -P "^  [[:digit:]]\.[[:digit:]]\.[[:digit:]]$" | tail -1)