Ruby安裝的那些坑

安裝參考http://guides.rubyonrails.org/getting_started.html
安裝Ruby

sudo apt-get install ruby2.0

ruby -v 顯示1.9.3
修改軟鏈接切換到2.0
Ubuntu從14.04開始在主軟件庫中提供Ruby 2.0,同時提供的還有1.9.3(軟件包名稱是ruby1.9.1)。不過由于Ruby 2.0在垃圾回收方面的顯著改進,大量程序推薦使用Ruby 2.0,這時問題出現(xiàn)了。通過運行“ruby -v”可知:

ruby -v
ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-linux]

Ubuntu 14.04中默認的Ruby版本是1.9.3,而Ruby 2.0則需要用命令“ruby2.0”運行,程序運行前必須設(shè)定好運行版本否則可能造成錯誤。

Kaijia一直使用GitLab管理代碼,以前GitLab還支持Ruby 1.9.3,不過從GitLab 7.0開始,Ruby 1.9.3的支持就被移除了(畢竟運行Ruby 1.9.3的GitLab簡直是內(nèi)存殺手)?,F(xiàn)在Kaijia需要使用Ruby 2.0運行GitLab,但是GitLab只支持系統(tǒng)默認的Ruby(即1.9.3)無法選擇運行的版本,也無法通過指定運行命令“ruby2.0”設(shè)置 Ruby版本;安裝指南中也一直是編譯安裝Ruby 2.1,沒有提到如何使用系統(tǒng)提供的Ruby 2.0,因此只能在系統(tǒng)層面上設(shè)置Ruby的全局默認版本。

Ubuntu 12.10及以后的版本提供一個ruby-switch小腳本實現(xiàn)全局Ruby版本的切換,同時也可以用update-alternatives命令來切 換版本,但自從ruby1.9.1包直接寫死默認路徑后update-alternatives就無法使用了,ruby-switch的原作者也申請從 Debian unstable源中移除了腳本,Ubuntu也同樣將此包移除。因此可以說之前可用的方式全都失效了。

既然軟性方法沒有了,那就只能使用(不可復原的)硬方法了。硬方法即是強制替換/usr/bin/目錄下與Ruby相關(guān)的符號鏈接到Ruby 2.0對應(yīng)的版本,這樣即可實現(xiàn)默認版本的切換:

cd /usr/bin/
ln -sf ruby2.0 ruby
ln -sf gem2.0 gem
ln -sf erb2.0 erb
ln -sf irb2.0 irb
ln -sf rake2.0 rake
ln -sf rdoc2.0 rdoc
ln -sf testrb2.0 testrb

替換完成之后還可以更新一下gem:

gem update –system
gem pristine –all

這時Ruby的默認版本就已經(jīng)切換到2.0了。當然雖然這樣做比較暴力,但也是可以還原的,如果想切換會Ruby 1.9.3,只需要運行(請注意在Ubuntu 14.04中,Ruby 1.9.3的名稱為ruby1.9.1):

cd /usr/bin/
ln -sf ruby1.9.1 ruby
ln -sf gem1.9.1 gem
ln -sf erb1.9.1 erb
ln -sf irb1.9.1 irb
ln -sf rake1.9.1 rake
ln -sf rdoc1.9.1 rdoc
ln -sf testrb1.9.1 testrb

同樣的,再更新一下gem之后Ruby就又切換回1.9.3了。

sudo gem install rails

報錯 不用懷疑這是被墻了,解決辦法換鏡像

ERROR:  Could not find a valid gem ‘rails’ (>= 0), here is why:
Unable to download data from https://rubygems.org/ – Errno::ECONNRESET: Connection reset by peer – SSL_connect (https://rubygems.org/latest_specs.4.8.gz)

更新gem的軟件源

sudo gem sources –r https://rubygems.org
sudo gem sources -a http://rubygems.org

重新執(zhí)行報錯:

mkmf.rb can’t find header files for ruby at /usr/lib/ruby/include/ruby.h

搜了一下Stackoverflow得到提示:

mkmf is part of the ruby1.9.1-dev package. This package contains the header files needed for extension libraries for Ruby 1.9.1. You need to install the ruby1.9.1-dev package by doing:

sudo apt-get install ruby1.9.1-dev
sudo apt – get install ruby1 . 9.1 – dev

Then you can install Rails as per normal
我們相應(yīng)的裝2.0-dev就行了

sudo apt-get install ruby2.0-dev
sudo apt – get install ruby2 . 0 – dev

重新執(zhí)行報錯:

Building native extensions.  This could take a while…
ERROR:  Error installing rails:
ERROR: Failed to build gem native extension.
/usr/bin/ruby2.0 extconf.rb
checking if the C compiler accepts … yes
Building nokogiri using packaged libraries.
Using mini_portile version 2.0.0
checking for gzdopen() in -lz… no
zlib is missing; necessary for building libxml2
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers.  Check the mkmf.log file for more details.  You may need configuration options.
Provided configuration options:
–with-opt-dir
–without-opt-dir
–with-opt-include
–without-opt-include=${opt-dir}/include
–with-opt-lib
–without-opt-lib=${opt-dir}/lib
–with-make-prog
–without-make-prog
–srcdir=.
–curdir
–ruby=/usr/bin/ruby2.0
–help
–clean
–use-system-libraries
–enable-static
–disable-static
–with-zlib-dir
–without-zlib-dir
–with-zlib-include
–without-zlib-include=${zlib-dir}/include
–with-zlib-lib
–without-zlib-lib=${zlib-dir}/lib
–enable-cross-build
–disable-cross-build
Gem files will remain installed in /var/lib/gems/2.0.0/gems/nokogiri-1.6.7.2 for inspection.
Results logged to /var/lib/gems/2.0.0/gems/nokogiri-1.6.7.2/ext/nokogiri/gem_make.out

執(zhí)行如下命令

sudo apt-get install zlib1g zlib1g-dev
sudo apt-get install libxslt1-dev libxml2-dev

最后還是資源被墻,果斷科學上網(wǎng)解決

gem install nokogiri 報錯checking if the C compiler accepts問題

用sudo apt-get install libgmp-dev 解決

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容