一、安裝ruby環(huán)境
1.從ruby官網(wǎng)上下載ruby源碼,https://www.ruby-lang.org/en/downloads/, 解壓
2.cd 到解壓目錄下,執(zhí)行 ? $ ./configure
3.執(zhí)行? $ make
4. 執(zhí)行 ? $ make install
二、通過(guò)rvm升級(jí)Ruby
1.安裝rvm
$ curl -L get.rvm.io| bash -s stable
2.加載文件,測(cè)試是否安裝正常(按照提示操作)
$ source ~/.bashrc
$ source ~/.bash_profile
$ source ~/.profile
$ rvm -v
3.獲取rvm列表 ? ? $ rvm list known

4.安裝ruby2.4? ? ? $ rvm install 2.4

安裝過(guò)程中可能遇到如下錯(cuò)誤:
Error running 'requirements_osx_port_libs_install curl-ca-bundle automake libtool libyaml libffi libksba',showing last 15 lines of /Users/acewill/.rvm/log/1468253599_ruby-2.3.0/package_install_curl-ca-bundle_automake_libtool_libyaml_libffi_libksba.log
原因是需要安裝Homebrew, 參照stack overflow上的問(wèn)題Installing RVM: “Requirements installation failed with status: 1.”
安裝Homebrew, 通過(guò)以下命令:

安裝完Homebrew,重新安裝ruby ? ? ?$ rvm install 2.4
三、設(shè)置ruby的軟件源
1.查看當(dāng)前ruby的源 ? ? $ gem sources -l
2.移除當(dāng)前ruby的源 ? ?$ gem sources --remove?https://rubygems.org/
3.設(shè)置當(dāng)前的ruby源 ? ?$ gem sources -a https://ruby.taobao.org/
如果不能用了,去官網(wǎng)看吧? 實(shí)時(shí)更新的? https://gems.ruby-china.org/
4.再次查看當(dāng)前的ruby源? $ gem sources -l
如果提示為:
*** CURRENT SOURCES ***
表示切換成功
四、設(shè)置gem為最新版本
如果gem版本太老,可以嘗試升級(jí)下gem:? $ gem update --system
升級(jí)成功后會(huì)提示: Latest version currently installed. Aborting.
五、安裝cocopods
1.$ sudo gem install -n /usr/local/bin cocoapods
這里可能會(huì)報(bào)錯(cuò) ?
ERROR: While executing gem ... (Gem::Exception)
Unable to require openssl, install OpenSSL and rebuild ruby (preferred) or use non-HTTPS sources
先看一下$openssl version是不是安裝了 OpenSSL
OpenSSL 0.9.8zh 14 Jan 2016
那就更換源 的地址
2.刪除 https 源 $ gem source -r https://rubygems.org/ to remove
3.添加 http 源 $ gem source -a http://rubygems.org/ to read
4.確保添加正確 ? $ gem source -I
*** CURRENT SOURCES ***
http://rubygems.org/
5.再次安裝cocopods? $ sudo gem install -n /usr/local/bin cocoapods
6. ?$ pod setup ?時(shí)間會(huì)比較久,等著就好了。
7.待安裝成功后,驗(yàn)證一下 ?$ pod --version?
六、使用cocopods
1.接在需要加入cocoapods項(xiàng)目的根目錄新建Podfile文件
$ pod init? //會(huì)生成模板的Podfile文件
$ touch Podfile
2.打開(kāi)Podfile文件,內(nèi)容的格式應(yīng)該如下:
platform :ios, '8.0' #(注明你的開(kāi)發(fā)平臺(tái)以及版本,'8.0'忽略不寫即為最新版本)
pod 'AFNetworking', '~> 2.5.3' #('~> 2.5.3'為版本號(hào),忽略不寫即為最新版本)
3.回到控制臺(tái)執(zhí)行 ?$ pod install
? ?這樣,AFNetworking就已經(jīng)下載完成并且設(shè)置好了編譯參數(shù)和依賴,以后使用的時(shí)候切記如下兩點(diǎn):
1.從此以后需要使用Cocoapods生成的 .xcworkspace文件來(lái)打開(kāi)工程,而不是使用以前的.xcodeproj文件
2.每次更改了Podfile文件,都需要重新執(zhí)行一次 $ pod update命令
如果不知道 cocoaPods 管理的庫(kù)中,是否有你想要的庫(kù),那么你可以通過(guò) ?$ pod search 命令進(jìn)行查找
當(dāng)你執(zhí)行pod install之后,除了 Podfile 外,CocoaPods 還會(huì)生成一個(gè)名為Podfile.lock的文件,Podfile.lock 應(yīng)該加入到版本控制里面,不應(yīng)該把這個(gè)文件加入到.gitignore中。因?yàn)镻odfile.lock會(huì)鎖定當(dāng)前各依賴庫(kù)的版本,之后如果多次執(zhí)行pod install 不會(huì)更改版本,要pod update才會(huì)改Podfile.lock了。這樣多人協(xié)作的時(shí)候,可以防止第三方庫(kù)升級(jí)時(shí)造成大家各自的第三方庫(kù)版本不一致