Ruby、RVM-使用總結(jié)
安裝或更新Ruby
方法一:使用Homebrew
安裝Ruby
Homebrew 是什么:
Homebrew是 mac的包管理器躺孝,僅需執(zhí)行相應(yīng)的命令,就能下載安裝需要的軟件包浑娜,可以省掉自己去下載佑力、解壓、拖拽(安裝)等繁瑣的步驟筋遭。
安裝Homebrew
-
在終端執(zhí)行:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
執(zhí)行結(jié)果:
==> 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
-
可以看到第一步執(zhí)行后有報(bào)錯(cuò)打颤,按照?qǐng)?bào)錯(cuò)提示執(zhí)行
Next steps
:在終端執(zhí)行:// 第一步 echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/hsh/.zprofile // 第二步 eval "$(/opt/homebrew/bin/brew shellenv)"
注:Mac M1 芯片每次重新打開(kāi)終端時(shí),可能無(wú)法找到
brew
宛畦,按照?qǐng)?bào)錯(cuò)提示執(zhí)行瘸洛,重新打開(kāi)終端時(shí)揍移,也無(wú)法找到次和,解決辦法:可以在終端執(zhí)行:// 用echo 添加上導(dǎo)出語(yǔ)句 echo 'export PATH="/opt/homebrew/bin:$PATH"' >> $HOME/.zshrc // 運(yùn)行source命令,文件生效 source ~/.zshrc
-
啟動(dòng)
brew
那伐,在終端執(zhí)行:brew help
:執(zhí)行結(jié)果:
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
-
在終端執(zhí)行:
brew -v
:查看最新版本:執(zhí)行結(jié)果:
Homebrew 3.2.16 Homebrew/homebrew-core (no Git repository)
更新Homebrew
-
打開(kāi)終端執(zhí)行:
brew update
:執(zhí)行結(jié)果:
Already up-to-date.
Homebrew
常用語(yǔ)法:
// 卸載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 # 查看可清理的舊版本包踏施,不執(zhí)行實(shí)際操作
// 鎖定不想更新的包
brew pin $FORMULA # 鎖定某個(gè)包
brew unpin $FORMULA # 取消鎖定
// 查看安裝包的相關(guān)信息
brew info $FORMULA # 顯示某個(gè)包的信息
brew info # 顯示安裝了包數(shù)量,文件數(shù)量罕邀,和總占用空間
brew deps --installed --tree # 查看已安裝的包的依賴畅形,樹(shù)形顯示
// 列出已安裝包
brew list
// 刪除
brew rm $FORMULA # 刪除某個(gè)包
brew uninstall --force $FORMULA # 刪除所有版本
注:Homebrew
主要由四個(gè)部分組成:brew
、homebrew-core
诉探、homebrew-cask
日熬、homebrew-bottles
,它們對(duì)應(yīng)的功能如下:
組成 | 功能 |
---|---|
Homebrew | 源代碼倉(cāng)庫(kù) |
homebrew-core | Homebrew 核心源 |
homebrew-cask | 提供macos應(yīng)用和大型二進(jìn)制文件的安裝 |
homebrew-bottles | 預(yù)編譯二進(jìn)制軟件包 |
安裝或更新Ruby
先更新
Homebrew
:brew update
肾胯;Mac自身的ruby
版本是2.6.3p62
竖席;-
安裝
ruby
:brew install ruby
:執(zhí)行結(jié)果:
==> 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"
安裝完成后,執(zhí)行
ruby -v
還是顯示原來(lái)的版本敬肚,這是因?yàn)榄h(huán)境變量沒(méi)有配置毕荐。 將ruby環(huán)境變量配置到
~/.zshrc
中:echo 'export PATH="/opt/homebrew/opt/ruby/bin:$PATH"' >> ~/.zshrc
;使其立即生效:
source ~/.zshrc
艳馒;-
現(xiàn)在執(zhí)行:
ruby -v
:顯示已是新版本:ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [arm64-darwin20]
注意:Homebrew
使用憎亚、報(bào)錯(cuò)等問(wèn)題:參考這里
方法二:使用RVM
安裝或更新Ruby
安裝RVM
安裝:
curl -L get.rvm.io | bash -s stable
;執(zhí)行:
source ~/.bashrc
;執(zhí)行:
source ~/.bash_profile
第美;-
查看
rvm
版本:rvm -v
:執(zhí)行結(jié)果:
rvm 1.29.12 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io]
更新RVM
-
更新RVM:
rvm get stable
:執(zhí)行結(jié)果:
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
查看當(dāng)前的
ruby
版本:rvm list
蝶锋;-
列出
ruby
可安裝的版本信息:rvm list known
:執(zhí)行結(jié)果:
# 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 .....
安裝
ruby
:rvm install 3.0.0
;注意:如果你電腦沒(méi)有安裝Xcode和Command Line Tools for Xcode以及Homebrew 會(huì)自動(dòng)下載安裝,建議提前安裝這三者.設(shè)為默認(rèn):
rvm use 3.0.0 --default
什往;
注意:
1)use后面放rvm list安裝的版本牲览,能夠直接放2.5.1,也能夠放rvm list出來(lái)的全名
2)--default設(shè)置默認(rèn)版本恶守,若是不加這個(gè)字段第献,則只是在當(dāng)前控制臺(tái)臨時(shí)設(shè)置版本
更換Ruby
源
-
更換
ruby
源:在終端執(zhí)行如下:sudo gem update --system gem sources --remove https://rubygems.org/ gem sources --add https://gems.ruby-china.com/
-
驗(yàn)證你的
Ruby
鏡像是并且僅是ruby-china
:gem sources -l
:執(zhí)行結(jié)果:
*** CURRENT SOURCES *** https://gems.ruby-china.com/