让人蛋疼的Mac OS 下安装 Ruby Gem

/ 1评 / 0

最近需要在自己的 mbp 上用 gem 装俩东西, 然后平了俩坑. 记一下, 作为以后遇到同样问题的记录.

首先是 Connection Reseted 的问题. 症状很简单, 用 gem install 后, 效果如下:

gem install xxx
ERROR:  While executing gem ... (Gem::RemoteFetcher::FetchError)
    Errno::ECONNRESET: Connection reset by peer - SSL_connect (https://api.rubygems.org/quick/Marshal.4.8/xxx.gemspec.rz)

原因很简单, 在中国 api.rubygems.org 是被墙的, 也不知是什么仇什么怨.

最简单的方法是用淘宝镜像.

淘宝镜像地址是: https://ruby.taobao.org/

淘宝镜像首页说明写得很详细.

若是直接用 gem 手工安装的话, 用如下命令即可搞定:

$ gem sources --add https://ruby.taobao.org/ --remove https://rubygems.org/
$ gem sources -l
*** CURRENT SOURCES ***
https://ruby.taobao.org
# 请确保只有 ruby.taobao.org
$ gem install xxx

 

又或者
$ gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/
$ gem sources -l
https://gems.ruby-china.org
# 确保只有 gems.ruby-china.org

它其实就是修改了下你的 ~/.gemrc 文件

若用 bundle , 则可以使用命令

$ bundle config mirror.https://rubygems.org https://ruby.taobao.org

在 OS X 下, 若继续 gem install, 那么很容易遇到如下错误

gem install xxx
1 gem installed
gem install xxx
ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.

原因即字面意思, 就是 gem 要往某个神奇的目录写文件但是你的权限不够. 因为你使用的是 Apple 家自带的 ruby, 在尝试往 Apple 自家的库中塞东西, 默认那个位置是给 root 的.

有两个差劲的方法可以解决这个问题. 一个是前面加 sudo. 这时候记得先用 sudo 重走第一步换个源. 另一种是修改 /Library/Ruby/Gems/2.0.0 这个目录的用户, 即

sudo chown -R your_name:your_name /Library/Ruby/Gems/2.0.0

不过个人最建议的是用 homebrew 再装一个 ruby, 这样 local 归 local, system 归 system. 反正也不大.

brew install ruby

然后再 gem 下

$ gem install tmbundle-manager
Fetching: thor-0.19.1.gem (100%)
Successfully installed thor-0.19.1
Fetching: tmbundle-manager-0.1.2.gem (100%)
Successfully installed tmbundle-manager-0.1.2
Parsing documentation for thor-0.19.1
Installing ri documentation for thor-0.19.1
Parsing documentation for tmbundle-manager-0.1.2
Installing ri documentation for tmbundle-manager-0.1.2
Done installing documentation for thor, tmbundle-manager after 1 seconds
2 gems installed

默认情况下, brew 的各种 gems 会装到 /usr/local/lib/ruby/gems/2.3.0 这个位置. 2.3.0 是版本号, 也许在其它位置. 而不是原来的 /Library/Ruby/Gems/2.0.0 这个位置.

完了我们检查下 gem, 发现自带的和系统的完全就用两套库了. 非常好

$ gem list --local
*** LOCAL GEMS ***
bigdecimal (1.2.8)
did_you_mean (1.0.0)
io-console (0.4.5)
json (1.8.3)
minitest (5.8.3)
net-telnet (0.1.1)
power_assert (0.2.6)
psych (2.0.17)
rake (10.4.2)
rdoc (4.2.1)
test-unit (3.1.5)
thor (0.19.1)
tmbundle-manager (0.1.2)
$ /usr/bin/gemlist --local
*** LOCAL GEMS ***
activesupport (3.2.19)
bigdecimal (1.2.0)
CFPropertyList (2.2.8)
colored (1.2)
i18n (0.6.11)
io-console (0.4.2)
json (1.7.7)
libxml-ruby (2.6.0)
minitest (4.3.2)
multi_json (1.10.1)
nokogiri (1.5.6)
psych (2.0.0)
rake (0.9.6)
rdoc (4.0.0)
rubygems-update (2.4.2)
sqlite3 (1.3.7)
test-unit (2.0.0.0)
xcodeproj (0.19.4)

 

sudo gem <span class="hljs-operator"><span class="hljs-keyword">install</span> cocoapods</span>

发表评论

邮箱地址不会被公开。 必填项已用*标注