Install Latest Stable Version of Ruby Using rbenv

2020-05-15 15:47发布

问题:

I want to install the latest stable version of Ruby available with rbenv. This feature won't be happening in rbenv itself.

When I run the command rbenv install -l in my shell, I get a long list of available versions. The list has all types of entries. The following is a partial list to demonstrate the format and diversity:

$ rbenv install -l
Available versions:
  2.0.0-p643
  2.0.0-p645
  2.1.0-dev
  2.1.0-preview1
  2.1.0-preview2
  2.1.0-rc1
  2.1.4
  2.1.5
  2.1.6
  2.2.0-dev
  2.2.0-preview1
  2.2.0-preview2
  2.2.0-rc1
  2.2.0
  2.2.1
  2.2.2
  2.3.0-dev
  jruby-1.7.19
  jruby-1.7.20
  jruby-9.0.0.0-dev
  jruby-9.0.0.0+graal-dev
  jruby-9.0.0.0.pre1
  jruby-9.0.0.0.pre2
  maglev-1.0.0
  maglev-1.1.0-dev
  maglev-2.0.0-dev
  mruby-dev
  mruby-1.0.0
  mruby-1.1.0
  rbx-2.5.2
  rbx-2.5.3
  ree-1.8.7-2011.12
  ree-1.8.7-2012.01
  ree-1.8.7-2012.02
  topaz-dev

My goal is to automate the command rbenv install VERSION in a shell script where VERSION is the highest x.x.x release. In other words, I need to automatically substitute the highest entry on the list that starts with a number and does not end with -something into VERSION. From this list, I need 2.2.2.

What can I put in my shell script to automatically pick the highest x.x.x version in the command rbenv install x.x.x?

Edit: Since Ruby is not yet installed, the solution has to be in Bash and not Ruby.

Edit 2: I want the MRI (mainstream) version of Ruby.

回答1:

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


回答2:

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.



回答3:

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



回答4:

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)


回答5:

Mine is similar to Anonymous's answer (but shorter because I'm using \d).

rbenv install -l|  grep -P "\s2.*(\.|\d)\d$" | tail -1

I wanted to specify the latest jruby, which is why I used a "2", so I can replace the 2 with "jruby":

rbenv install -l|  grep -P "\sjruby.*(\.|\d)\d$"|tail -1

You can replace jruby with other strings to get different types of ruby, e.g. rbx, mruby.



回答6:

I would much rather do it in Ruby than in bash.

versions = `rbenv install -l`
versions.split("\s").select{|v| v.match( /^\d*\.\d*\.\d*$/ )}.last