Ruby、RVM-使用总结

Ruby、RVM-使用总结

安装或更新Ruby

方法一:使用Homebrew安装Ruby

Homebrew 是什么:

Homebrew是 mac的包管理器,仅需执行相应的命令,就能下载安装需要的软件包,可以省掉自己去下载、解压、拖拽(安装)等繁琐的步骤。

Homebrew 官方文档

安装Homebrew

  1. 在终端执行:

    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    

    执行结果:

    ==> Checking for `sudo` access (which may request your password).
    Password:
    ==> This script will install:
    /opt/homebrew/bin/brew
    /opt/homebrew/share/doc/homebrew
    /opt/homebrew/share/man/man1/brew.1
    /opt/homebrew/share/zsh/site-functions/_brew
    /opt/homebrew/etc/bash_completion.d/brew
    /opt/homebrew
    
    Press RETURN to continue or any other key to abort
    ==> /usr/bin/sudo /usr/sbin/chown -R hsh:admin /opt/homebrew
    ==> Downloading and installing Homebrew...
    HEAD is now at 976f9daa1 Merge pull request #12235 from Homebrew/dependabot/bundler/Library/Homebrew/sorbet-0.5.9226
    fatal: Could not resolve HEAD to a revision
    Warning: /opt/homebrew/bin is not in your PATH.
      Instructions on how to configure your shell for Homebrew
      can be found in the 'Next steps' section below.
    ==> Installation successful!
    
    ==> Homebrew has enabled anonymous aggregate formulae and cask analytics.
    Read the analytics documentation (and how to opt-out) here:
      https://docs.brew.sh/Analytics
    No analytics data has been sent yet (or will be during this `install` run).
    
    ==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
      https://github.com/Homebrew/brew#donations
    
    ==> Next steps:
    - Run these two commands in your terminal to add Homebrew to your PATH:
        echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/hsh/.zprofile
        eval "$(/opt/homebrew/bin/brew shellenv)"
    - Run `brew help` to get started
    - Further documentation: 
        https://docs.brew.sh
    
  2. 可以看到第一步执行后有报错,按照报错提示执行Next steps:在终端执行:

    // 第一步
    echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/hsh/.zprofile
    // 第二步
    eval "$(/opt/homebrew/bin/brew shellenv)"
    

    注:Mac M1 芯片每次重新打开终端时,可能无法找到brew,按照报错提示执行,重新打开终端时,也无法找到,解决办法:可以在终端执行:

    // 用echo 添加上导出语句
    echo 'export PATH="/opt/homebrew/bin:$PATH"' >> $HOME/.zshrc
    
    // 运行source命令,文件生效
    source ~/.zshrc
    
  3. 启动brew,在终端执行:brew help

    执行结果:

    Example usage:
      brew search TEXT|/REGEX/
      brew info [FORMULA|CASK...]
      brew install FORMULA|CASK...
      brew update
      brew upgrade [FORMULA|CASK...]
      brew uninstall FORMULA|CASK...
      brew list [FORMULA|CASK...]
    
    Troubleshooting:
      brew config
      brew doctor
      brew install --verbose --debug FORMULA|CASK
    
    Contributing:
      brew create URL [--no-fetch]
      brew edit [FORMULA|CASK...]
    
    Further help:
      brew commands
      brew help [COMMAND]
      man brew
      https://docs.brew.sh
    
  4. 在终端执行:brew -v:查看最新版本:

    执行结果:

    Homebrew 3.2.16
    Homebrew/homebrew-core (no Git repository)
    

更新Homebrew

  1. 打开终端执行:brew update

    执行结果:

    Already up-to-date.
    

Homebrew常用语法:

// 卸载Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/uninstall.sh)"

// 查询:
brew search 软件名
    
// 安装:
brew install 软件名
    
// 卸载:
brew uninstall 软件名
    
// 更新 Homebrew自己:
brew update 
    
// 查看 Homebrew 配置信息:
brew config 
    
// 查看哪些安装包需要更新
brew outdated
    
// 更新安装包
brew upgrade   # 更新所有的包
brew upgrade $FORMULA   # 更新指定的包
    
// 清理旧版本
brew cleanup   # 清理所有包的旧版本
brew cleanup $FORMULA   # 清理指定包的旧版本
brew cleanup -n   # 查看可清理的旧版本包,不执行实际操作
    
// 锁定不想更新的包
brew pin $FORMULA   # 锁定某个包
brew unpin $FORMULA    # 取消锁定
    
// 查看安装包的相关信息
brew info $FORMULA   # 显示某个包的信息
brew info   # 显示安装了包数量,文件数量,和总占用空间
brew deps --installed --tree   # 查看已安装的包的依赖,树形显示
    
// 列出已安装包
brew list
    
// 删除
brew rm $FORMULA   # 删除某个包
brew uninstall --force $FORMULA   # 删除所有版本

注:Homebrew主要由四个部分组成:brewhomebrew-corehomebrew-caskhomebrew-bottles,它们对应的功能如下:

组成 功能
Homebrew 源代码仓库
homebrew-core Homebrew 核心源
homebrew-cask 提供macos应用和大型二进制文件的安装
homebrew-bottles 预编译二进制软件包

安装或更新Ruby

  1. 先更新Homebrewbrew update;Mac自身的ruby版本是2.6.3p62

  2. 安装rubybrew install ruby

    执行结果:

    ==> Downloading https://ghcr.io/v2/homebrew/core/libyaml/manifests/0.2.5
    ######################################################################## 100.0%
    ==> Downloading https://ghcr.io/v2/homebrew/core/libyaml/blobs/sha256:fe1082f347
    ==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sh
    ######################################################################## 100.0%
    ==> Downloading https://ghcr.io/v2/homebrew/core/ca-certificates/manifests/2021-
    ######################################################################## 100.0%
    ==> Downloading https://ghcr.io/v2/homebrew/core/ca-certificates/blobs/sha256:47
    ==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sh
    ######################################################################## 100.0%
    ==> Downloading https://ghcr.io/v2/homebrew/core/openssl/1.1/manifests/1.1.1l_1
    ######################################################################## 100.0%
    ==> Downloading https://ghcr.io/v2/homebrew/core/openssl/1.1/blobs/sha256:e3d855
    ==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sh
    ######################################################################## 100.0%
    ==> Downloading https://ghcr.io/v2/homebrew/core/readline/manifests/8.1.1
    ######################################################################## 100.0%
    ==> Downloading https://ghcr.io/v2/homebrew/core/readline/blobs/sha256:bcb228b99
    ==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sh
    ######################################################################## 100.0%
    ==> Downloading https://ghcr.io/v2/homebrew/core/ruby/manifests/3.0.2_1
    ######################################################################## 100.0%
    ==> Downloading https://ghcr.io/v2/homebrew/core/ruby/blobs/sha256:86f9be3f7ac26
    ==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sh
    ######################################################################## 100.0%
    ==> Installing dependencies for ruby: libyaml, ca-certificates, openssl@1.1 and readline
    ==> Installing ruby dependency: libyaml
    ==> Pouring libyaml--0.2.5.arm64_big_sur.bottle.tar.gz
    🍺  /opt/homebrew/Cellar/libyaml/0.2.5: 10 files, 369.9KB
    ==> Installing ruby dependency: ca-certificates
    ==> Pouring ca-certificates--2021-09-30.all.bottle.1.tar.gz
    ==> Regenerating CA certificate bundle from keychain, this may take a while...
    🍺  /opt/homebrew/Cellar/ca-certificates/2021-09-30: 3 files, 203.5KB
    ==> Installing ruby dependency: openssl@1.1
    ==> Pouring openssl@1.1--1.1.1l_1.arm64_big_sur.bottle.tar.gz
    🍺  /opt/homebrew/Cellar/openssl@1.1/1.1.1l_1: 8,073 files, 18MB
    ==> Installing ruby dependency: readline
    ==> Pouring readline--8.1.1.arm64_big_sur.bottle.tar.gz
    🍺  /opt/homebrew/Cellar/readline/8.1.1: 48 files, 1.7MB
    ==> Installing ruby
    ==> Pouring ruby--3.0.2_1.arm64_big_sur.bottle.tar.gz
    ==> Caveats
    By default, binaries installed by gem will be placed into:
      /opt/homebrew/lib/ruby/gems/3.0.0/bin
    
    You may want to add this to your PATH.
    
    ruby is keg-only, which means it was not symlinked into /opt/homebrew,
    because macOS already provides this software and installing another version in
    parallel can cause all kinds of trouble.
    
    If you need to have ruby first in your PATH, run:
      echo 'export PATH="/opt/homebrew/opt/ruby/bin:$PATH"' >> ~/.zshrc
    
    For compilers to find ruby you may need to set:
      export LDFLAGS="-L/opt/homebrew/opt/ruby/lib"
      export CPPFLAGS="-I/opt/homebrew/opt/ruby/include"
    
    ==> Summary
    🍺  /opt/homebrew/Cellar/ruby/3.0.2_1: 16,390 files, 40.4MB
    ==> Caveats
    ==> ruby
    By default, binaries installed by gem will be placed into:
      /opt/homebrew/lib/ruby/gems/3.0.0/bin
    
    You may want to add this to your PATH.
    
    ruby is keg-only, which means it was not symlinked into /opt/homebrew,
    because macOS already provides this software and installing another version in
    parallel can cause all kinds of trouble.
    
    If you need to have ruby first in your PATH, run:
      echo 'export PATH="/opt/homebrew/opt/ruby/bin:$PATH"' >> ~/.zshrc
    
    For compilers to find ruby you may need to set:
      export LDFLAGS="-L/opt/homebrew/opt/ruby/lib"
      export CPPFLAGS="-I/opt/homebrew/opt/ruby/include"
    

    安装完成后,执行ruby -v还是显示原来的版本,这是因为环境变量没有配置。

  3. 将ruby环境变量配置到~/.zshrc中:echo 'export PATH="/opt/homebrew/opt/ruby/bin:$PATH"' >> ~/.zshrc

  4. 使其立即生效:source ~/.zshrc

  5. 现在执行:ruby -v:显示已是新版本:

    ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [arm64-darwin20]
    

注意:Homebrew使用、报错等问题:参考这里

方法二:使用RVM安装或更新Ruby

安装RVM

  1. 安装:curl -L get.rvm.io | bash -s stable

  2. 执行:source ~/.bashrc

  3. 执行:source ~/.bash_profile

  4. 查看rvm版本:rvm -v

    执行结果:

    rvm 1.29.12 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io]
    

更新RVM

  1. 更新RVM:rvm get stable

    执行结果:

    Downloading https://get.rvm.io
    No GPG software exists to validate rvm-installer, skipping.
    Downloading https://github.com/rvm/rvm/archive/1.29.12.tar.gz
    Downloading https://github.com/rvm/rvm/releases/download/1.29.12/1.29.12.tar.gz.asc
    Found PGP signature at: 'https://github.com/rvm/rvm/releases/download/1.29.12/1.29.12.tar.gz.asc',
    but no GPG software exists to validate it, skipping.
    Upgrading the RVM installation in /Users/hsh/.rvm/
        RVM PATH line found in /Users/hsh/.mkshrc /Users/hsh/.profile /Users/hsh/.bashrc /Users/hsh/.zshrc.
        RVM sourcing line found in /Users/hsh/.profile /Users/hsh/.bash_profile /Users/hsh/.zlogin.
    Upgrade of RVM in /Users/hsh/.rvm/ is complete.
    /Users/hsh/.zshrc:2:export PATH export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin 
    
      * WARNING: Above files contains PATH= with no $PATH inside, this can break RVM,
        for details check https://github.com/rvm/rvm/issues/1351#issuecomment-10939525
        to avoid this warning prepend $PATH
    
    
    Thanks for installing RVM 🙏
    Please consider donating to our open collective to help us maintain RVM.
    
    👉  Donate: https://opencollective.com/rvm/donate
    
    
    RVM reloaded!
    

安装或更新Ruby

  1. 查看当前的ruby版本:rvm list

  2. 列出ruby可安装的版本信息:rvm list known

    执行结果:

    # MRI Rubies
    [ruby-]1.8.6[-p420]
    [ruby-]1.8.7[-head] # security released on head
    [ruby-]1.9.1[-p431]
    [ruby-]1.9.2[-p330]
    [ruby-]1.9.3[-p551]
    [ruby-]2.0.0[-p648]
    [ruby-]2.1[.10]
    [ruby-]2.2[.10]
    [ruby-]2.3[.8]
    [ruby-]2.4[.10]
    [ruby-]2.5[.8]
    [ruby-]2.6[.6]
    [ruby-]2.7[.2]
    [ruby-]3[.0.0]
    ruby-head
    
    # for forks use: rvm install ruby-head-<name> --url https://github.com/github/ruby.git --branch 2.2
    
    .....
    
  3. 安装rubyrvm install 3.0.0;注意:如果你电脑没有安装Xcode和Command Line Tools for Xcode以及Homebrew 会自动下载安装,建议提前安装这三者.

  4. 设为默认:rvm use 3.0.0 --default

注意:

1)use后面放rvm list安装的版本,能够直接放2.5.1,也能够放rvm list出来的全名
2)--default设置默认版本,若是不加这个字段,则只是在当前控制台临时设置版本

更换Ruby

  1. 更换ruby源:在终端执行如下:

    sudo gem update --system
    gem sources --remove https://rubygems.org/
    gem sources --add https://gems.ruby-china.com/
    
  2. 验证你的Ruby镜像是并且仅是ruby-chinagem sources -l

    执行结果:

    *** CURRENT SOURCES ***
    
    https://gems.ruby-china.com/
    
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,126评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,254评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,445评论 0 341
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,185评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,178评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,970评论 1 284
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,276评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,927评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,400评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,883评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,997评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,646评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,213评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,204评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,423评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,423评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,722评论 2 345

推荐阅读更多精彩内容