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,这时问题出现了。通过运行“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”运行,程序运行前必须设定好运行版本否则可能造成错误。

Kaijia一直使用GitLab管理代码,以前GitLab还支持Ruby 1.9.3,不过从GitLab 7.0开始,Ruby 1.9.3的支持就被移除了(毕竟运行Ruby 1.9.3的GitLab简直是内存杀手)。现在Kaijia需要使用Ruby 2.0运行GitLab,但是GitLab只支持系统默认的Ruby(即1.9.3)无法选择运行的版本,也无法通过指定运行命令“ruby2.0”设置 Ruby版本;安装指南中也一直是编译安装Ruby 2.1,没有提到如何使用系统提供的Ruby 2.0,因此只能在系统层面上设置Ruby的全局默认版本。

Ubuntu 12.10及以后的版本提供一个ruby-switch小脚本实现全局Ruby版本的切换,同时也可以用update-alternatives命令来切 换版本,但自从ruby1.9.1包直接写死默认路径后update-alternatives就无法使用了,ruby-switch的原作者也申请从 Debian unstable源中移除了脚本,Ubuntu也同样将此包移除。因此可以说之前可用的方式全都失效了。

既然软性方法没有了,那就只能使用(不可复原的)硬方法了。硬方法即是强制替换/usr/bin/目录下与Ruby相关的符号链接到Ruby 2.0对应的版本,这样即可实现默认版本的切换:

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的默认版本就已经切换到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

 

重新执行报错:

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

我们相应的装2.0-dev就行了

sudo apt-get install ruby2.0-dev

sudo apt – get install ruby2 . 0 – dev

 

 

重新执行报错:

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

 

执行如下命令

sudo apt-get install zlib1g zlib1g-dev

sudo apt-get install libxslt1-dev libxml2-dev

 

最后还是资源被墙,果断翻墙解决

 

gem install nokogiri 报错checking if the C compiler accepts问题

 

用sudo apt-get install libgmp-dev 解决