bash 使用 rbenv 安装最新的稳定版 Ruby

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30179484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:58:50  来源:igfitidea点击:

Install Latest Stable Version of Ruby Using rbenv

rubybashshellsedrbenv

提问by mislav

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

我想安装rbenv可用的最新稳定版 Ruby 。此功能不会在 rbenv 本身中发生

When I run the command rbenv install -lin 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在 shell 中运行命令时,我得到一长串可用版本。该列表包含所有类型的条目。以下是展示格式和多样性的部分列表:

$ 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 VERSIONin a shell script where VERSIONis the highest x.x.xrelease. In other words, I need to automatically substitute the highest entry on the list that starts with a number and does not end with -somethinginto VERSION. From this list, I need 2.2.2.

我的目标是在最高版本rbenv install VERSION的 shell 脚本中自动执行命令。换句话说,我需要自动替换列表中以数字开头且不以into结尾的最高条目。从这个列表中,我需要.VERSIONx.x.x-somethingVERSION2.2.2

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

我可以在我的 shell 脚本中放入什么来自动选择x.x.x命令中的最高版本rbenv install x.x.x

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

编辑:由于尚未安装 Ruby,因此解决方案必须使用 Bash 而不是 Ruby。

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

编辑 2:我想要 Ruby 的 MRI(主流)版本。

采纳答案by NeronLeVelu

rbenv install -l | awk -F '.' '
   /^[[:space:]]*[0-9]+\.[0-9]+\.[0-9]+[[:space:]]*$/ {
      if ( ( * 100 + ) * 100 +  > Max ) { 
         Max = ( * 100 + ) * 100 + 
         Version=
rbenv install -l | sed -n '/^[[:space:]]*[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}[[:space:]]*$/ h;${g;p;}'
} } END { print Version }'
  • Take the biggest version (sorted order or not)
  • 取最大的版本(排序与否)

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

如果对列表进行排序,则更简单的 sed(posix 版本)就足够了

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

回答by mislav

Simple solution(directly installs latest stable version):

简单的解决办法(直接安装最新的稳定版):

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

Explanation:

解释:

rbenv install -l | grep -P "^  [[:digit:]]\.[[:digit:]]\.[[:digit:]]$" | 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.

过滤掉所有包含连字符的版本-,即所有非 MRI 版本和预发布 MRI 版本。然后选择最后一个,保证是最高的,因为 ruby​​-build 输出已经按版本号升序排序。

回答by Sathish

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

在使用 rbenv 安装时,应该首先更新 ruby​​-build 以获取最新版本的 ruby​​。请按照以下步骤操作:

  • brew reinstall --HEAD ruby-build( if rbenv is already installed brew may through some error then to move ahead and simply
    • brew unlink ruby-buildand
    • 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>
  • brew reinstall --HEAD ruby-build(如果 rbenv 已经安装了 brew 可能会通过一些错误然后继续前进并简单地
    • brew unlink ruby-build
    • brew install --HEAD ruby-build)
  • brew upgrade
  • 然后您可以使用上面建议的上述方法之一自动安装最新版本或简单地 rbenv install <required latest version>

working in macOS 10.13.6

在 macOS 10.13.6 中工作

回答by Sathish

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

这将产生 2.2.2

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

We can plug that result into rbenv installlike this:

我们可以将结果插入rbenv install如下:

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

回答by kristianp

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

我的类似于 Anonymous 的答案(但更短,因为我使用的是 \d)。

rbenv install -l|  grep -P "\sjruby.*(\.|\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":

我想指定最新的 jruby,这就是我使用“2”的原因,所以我可以用“jruby”替换 2:

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

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

您可以将 jruby 替换为其他字符串以获得不同类型的 ruby​​,例如 rbx、mruby。

回答by B Seven

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

我更愿意在 Ruby 中做这件事而不是在 bash 中。

##代码##