Ruby、RVM-使用总结

2021-10-15  本文已影响0人  AngeloD

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/
    
上一篇下一篇

猜你喜欢

热点阅读