mac及linux下搭建ruby+rails环境

mac通常会自带ruby一个版本的ruby环境, 比如我的是2.0.0。 如果项目需要的版本巧好和自己的默认版本一样,就不用折腾安装其他版本了。 如果不一样, 这里介绍使用rvm来安装管理ruby版本

安装 rvm(参照官网http://www.rvm.io/)

第一步可能不是必须的

第一步

 gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB 
第二步
 curl -sSL https://get.rvm.io | bash -s stable 

成功之后通常会有个提示

 Downloading https://github.com/rvm/rvm/archive/1.29.3.tar.gz Downloading https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc Found PGP signature at: 'https://github.com/rvm/rvm/releases/download/1.29.3/1.29.3.tar.gz.asc', but no GPG software exists to validate it, skipping. Installing RVM to /Users/echen1/.rvm/   Adding rvm PATH line to /Users/echen1/.profile /Users/echen1/.mkshrc /Users/echen1/.bashrc /Users/echen1/.zshrc.   Adding rvm loading line to /Users/echen1/.profile /Users/echen1/.bash_profile /Users/echen1/.zlogin. Installation of RVM in /Users/echen1/.rvm/ is almost complete:  * To start using RVM you need to run `source /Users/echen1/.rvm/scripts/rvm`   in all your open shell windows, in rare cases you need to reopen all shell windows.  * WARNING: '~/.profile' file found. To load it into your shell, add the following line to '/Users/echen1/.bash_profile': 

如上面安装rvm成功之后, 再倒数第二行有一个WARNING, 这时候如果直接运行rvm 可能会出现command not found 解决方案: 运行一下命令

 source ~/.profile 

此时再运行: 类似一下结果表示安装成功

 rvm -v 

image.png 切换源

原生的源在国外,通常比较慢,所以可以切换到淘宝源(参考:https://ruby.taobao.org/)

For mac

 sed -i .bak -E 's!https?://cache.ruby-lang.org/pub/ruby!https://ruby.taobao.org/mirrors/ruby!' $rvm_path/config/db 
For Linux
 sed -i -E 's!https?://cache.ruby-lang.org/pub/ruby!https://ruby.taobao.org/mirrors/ruby!' $rvm_path/config/db 
rvm使用介绍 列出已知的ruby版本
 rvm list known 
安装一个ruby版本
 rvm install 1.9.3 

这里安装了最新的1.9.3, rvm list known列表里面的都可以拿来安装。

使用一个ruby版本

 rvm use 1.9.3 
设置为默认版本
 rvm use 1.9.3 --default  
查询已经安装的ruby
 rvm list 
切换 gem源
 gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/ 
 gem sources -l 

image.png

如图表示切换成功

安装rails 方法1(直接安装)

 gem install rails 
方法2

如果已有rails 项目则在项目运行 bundle install