Mac OS X 下使用 Ruby Gem 的两个坑

2016-12-08  本文已影响1563人  小阳哥_

最近需要在自己MacBookPro上用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

它其实就是修改了你的 ~/.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 ***

activesupport (4.2.5)
bigdecimal (default: 1.2.0)
CFPropertyList (2.3.3, 2.2.8)
claide (1.0.1, 1.0.0, 0.9.1)
cocoapods (1.1.1, 0.39.0)
cocoapods-core (1.1.1, 1.0.1, 0.39.0)
cocoapods-deintegrate (1.0.1, 1.0.0)
cocoapods-downloader (1.1.2, 1.1.0, 0.9.3)
cocoapods-plugins (1.0.0, 0.4.2)
cocoapods-search (1.0.0, 0.1.0)
cocoapods-stats (1.0.0, 0.6.2)
cocoapods-trunk (1.1.1, 1.0.0, 0.6.4)
cocoapods-try (1.1.0, 0.5.1)
colored (1.2)
concurrent-ruby (1.0.2)
escape (0.0.4)
fourflusher (2.0.1, 0.3.2)
fuzzy_match (2.0.4)
gh_inspector (1.0.2)
i18n (0.7.0)
io-console (default: 0.4.2)
json (default: 1.7.7)
libxml-ruby (2.6.0)
minitest (5.8.3, default: 4.3.2)
molinillo (0.5.4, 0.4.5, 0.4.0)
nanaimo (0.2.2)
nap (1.0.0)
netrc (0.7.8)
nokogiri (1.5.6)
psych (default: 2.0.0)
rake (default: 0.9.6)
rdoc (default: 4.0.0)
rubygems-update (2.6.7)
sqlite3 (1.3.7)
test-unit (default: 2.0.0.0)
thread_safe (0.3.5)
tzinfo (1.2.2)
xcodeproj (1.4.1, 1.2.0, 0.28.2)
$  /usr/bin/gem list --local

*** LOCAL GEMS ***

activesupport (4.2.5)
bigdecimal (default: 1.2.0)
CFPropertyList (2.3.3, 2.2.8)
claide (1.0.1, 1.0.0, 0.9.1)
cocoapods (1.1.1, 0.39.0)
cocoapods-core (1.1.1, 1.0.1, 0.39.0)
cocoapods-deintegrate (1.0.1, 1.0.0)
cocoapods-downloader (1.1.2, 1.1.0, 0.9.3)
cocoapods-plugins (1.0.0, 0.4.2)
cocoapods-search (1.0.0, 0.1.0)
cocoapods-stats (1.0.0, 0.6.2)
cocoapods-trunk (1.1.1, 1.0.0, 0.6.4)
cocoapods-try (1.1.0, 0.5.1)
colored (1.2)
concurrent-ruby (1.0.2)
escape (0.0.4)
fourflusher (2.0.1, 0.3.2)
fuzzy_match (2.0.4)
gh_inspector (1.0.2)
i18n (0.7.0)
io-console (default: 0.4.2)
json (default: 1.7.7)
libxml-ruby (2.6.0)
minitest (5.8.3, default: 4.3.2)
molinillo (0.5.4, 0.4.5, 0.4.0)
nanaimo (0.2.2)
nap (1.0.0)
netrc (0.7.8)
nokogiri (1.5.6)
psych (default: 2.0.0)
rake (default: 0.9.6)
rdoc (default: 4.0.0)
rubygems-update (2.6.7)
sqlite3 (1.3.7)
test-unit (default: 2.0.0.0)
thread_safe (0.3.5)
tzinfo (1.2.2)
xcodeproj (1.4.1, 1.2.0, 0.28.2)

如有不足之处,请指正,谢谢.

上一篇下一篇

猜你喜欢

热点阅读