Rubu包管理
Ruby的软件包单元为RubyGem
- Gem由.gemspec文件描述
- Gem的构建过程由Rakefile描述
- Rake是Gem的构建工具,它与Make类似,用以完成自动化测试和代码生成
- Bundle则是Ruby的包管理工具,用来跟踪和下载正确版本的Gem
gem安装报错
gem install rails
报错如下:
ERROR: Could not find a valid gem 'rails' (>= 0), here is why:Unable to download data from https://rubygems.org/ - Errno::ETIMEDOUT: Connection timed out - connect(2) for "s3.amazonaws.com" port 443 (https://api.rubygems.org/latest_specs.4.8.gz)
在stackoverflow上找到了答案:
大概意思是将https替换成http:
sudo gem sources -r https://rubygems.org/
sudo gem sources -a http://rubygems.org/
windows下如果出现这个错误,也是执行以上两句,去掉sudo即可。
另: 附上gem源有关的常用命令
- 查看当前使用的源地址:
gem sources
删除默认的源地址:
gem sources -r xxx.a.b.c/
注:默认的url地址后必须有”/”,否则可能删不掉。
添加源地址:
gem sources -a xxx.a.b.c/
注:国内使用淘bbbb宝的源比较稳定,且安装或更新网速都比较快。
gem sources -a http://ruby.taobao.org/
注:Mac上执行sudo gem install rails
报错
报错如下:
Building native extensions. This could take a while... ERROR: Error installing rails: ERROR: Failed to build gem native extension.
在stackoverflow上找到了答案:
原因 是缺少Command Line Developer Tools
解决方案:(推荐第一种,第三种可能不奏效)
xcode-select --install
- 去官网下载安装Apple XCode命令行工具
if you still have the problem even after Upgrading Xcode. Its may be because :
The Apple LLVM compiler in Xcode 5.1 treats unrecognized command-line options as errors. This issue has been seen when building both Python native extensions and Ruby Gems, where some invalid compiler options are currently specified.
To workaround this issue, set the ARCHFLAGS environment variable to downgrade the error to a warning.
ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future gem install GemName
Personally I have got this problem while installing json gem , I did : ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future gem install json
And the problem was solved.
删除命令行工具
如果因任何原因,你需要卸载命令行开发工具 ,找到/Library/Developer/
,并删除该目录下的CommandLineTools
文件夹。
其实我更愿意将/Library/Developer/
的Developer
文件夹一同删除。因为没装命令行工具的时候,/Library/
目录下没有Developer
这个文件夹;装了命令行工具才出现的。
报Dev-Kit的错
经过上面一步解决了gem源的问题之后,还可能出现下面错误:
ERROR: Error installing rails: The 'websocket-driver' native gem requires installed build tools. Please update your PATH to include build tools or download the DevKit from 'http://rubyinstaller.org/downloads' and follow the instructions at 'http://github.com/oneclick/rubyinstaller/wiki/Development-Kit'
出错的原因是安装XXXXX的时候,需要build tools,但系统中没有。
在stackoverflow上找到了答案:
大致意思如下:
到
http://rubyinstaller.org/downloads/
去下载dev kit – DevKit-mingw64-64-4.7.2-20130224-1432-sfx
按照
http://github.com/oneclick/rubyinstaller/wiki/Development-Kit/
安装dev kit
安装步骤如下:
- 如果原来系统中已经安装了旧版的dev kit, 则删除它
- 下载上面提到的dev kit
- 解压下载下来的文件到指定的目录,如
c:/devkit
。(注意:目录不能有空格) - 在安装目录下运行
ruby dk.rb
,然后按照提示分别运行ruby dk.rb init
和ruby dk.rb install
来增强ruby - 可以运行
gem install rdiscount –-platform=ruby
来测试是否成功
按照安装步骤,完成了DevKit的安装,非常简单。
invalid gem: package is corrupt的错误
安装ruport时报了个错如下:
ERROR: Error installing ruport: invalid gem: package is corrupt, exception while verifying: undefined me thod `size' for nil:NilClass (NoMethodError) in D:/Ruby-2.2.5/Ruby22-x64/lib/rub y/gems/2.2.0/cache/pdf-writer-1.1.8.gem
在stackoverflow上找到了答案,解决办法如下:
将%RUBY_HOME%/lib/ruby/gems/2.2.0/cache 文件夹中对应的gem删掉即可(这里是pdf-writer-1.1.8.gem),再重新执行命令。
Ruby的gem包管理
配置好Gemfile:
gemfile中可以定义项目中必须要安装的包以及相应的版本号,这样就可以避免不同的安装包以及版本之间的差异带来的问题。
例如:
source "http://rubygems.org"
gem 'rake'
gem 'foobar'
gem 'foobar', '0.0.18'
windows环境需要安装devkit
按前面的步骤配置好了devkit之后,执行gem install bundle
安装bundle包;切换到Gemfile所在的目录,执行bundle install
,Gemfile中指定的包以及相关的依赖包都会被安装。
导出当前机器上的gem包到某个文件
gem list > ~/gem_list.txt
Gemfile有了变动,更新
bundle update
不安装某个gem包对应的文档
gem install package-name --no-ri --nordoc