首页 > 代码库 > Ubuntu下安装 Ruby, Rails 运行环境

Ubuntu下安装 Ruby, Rails 运行环境

说明:

在安装之前我做了:1、更新软件源,详细指导在这里;

         2、更新系统:sudo apt-get update && sudo apt-get upgrade;

         3、安装fcitx,安装sogou输入法;

         4、打字不方便,禁用触摸板:sh TouchPad.sh off

接着就开始安装了!这里说明一下,后面的可能有几步可能是多余的,怕出错,就没有说去更正简化。

参考资料来自:边晓宇@CSDN 和 ruby-china.org如何快速正确的安装 Ruby, Rails 运行环境

 

  操作系统Ubuntu 14.04.1

 

一、安装后面需要使用curl,用dpkg -s curl命令检查一下系统中有没有安装:

zhaomh@ZHAO:~$  dpkg -s curldpkg-query: package curl is not installed and no information is available使用 dpkg --info (= dpkg-deb --info) 来检测打包好的文件,还可以通过 dpkg --contents (= dpkg-deb --contents) 来列出它们的内容。

提示没有安装,然后安装:

zhaomh@ZHAO:~$ sudo dpkg -s curldpkg-query: package curl is not installed and no information is available使用 dpkg --info (= dpkg-deb --info) 来检测打包好的文件,还可以通过 dpkg --contents (= dpkg-deb --contents) 来列出它们的内容。zhaomh@ZHAO:~$ sudo apt-get install curl正在读取软件包列表... 完成正在分析软件包的依赖关系树       正在读取状态信息... 完成       下列软件包是自动安装的并且现在不需要了:  linux-headers-generic linux-image-genericUse apt-get autoremove to remove them.下列【新】软件包将被安装:  curl升级了 0 个软件包,新安装了 1 个软件包,要卸载 0 个软件包,有 0 个软件包未被升级。需要下载 123 kB 的软件包。解压缩后会消耗掉 313 kB 的额外空间。获取:1 http://mirrors.ustc.edu.cn/ubuntu/ trusty-security/main curl amd64 7.35.0-1ubuntu2.2 [123 kB]下载 123 kB,耗时 0秒 (411 kB/s)Selecting previously unselected package curl.(正在读取数据库 ... 系统当前共安装有 197612 个文件和目录。)Preparing to unpack .../curl_7.35.0-1ubuntu2.2_amd64.deb ...Unpacking curl (7.35.0-1ubuntu2.2) ...Processing triggers for man-db (2.6.7.1-1ubuntu1) ...正在设置 curl (7.35.0-1ubuntu2.2) ...

测试一下:

zhaomh@ZHAO:~$ dpkg -s curlPackage: curlStatus: install ok installedPriority: optionalSection: webInstalled-Size: 306Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>Architecture: amd64Multi-Arch: foreignVersion: 7.35.0-1ubuntu2.2Depends: libc6 (>= 2.17), libcurl3 (= 7.35.0-1ubuntu2.2), zlib1g (>= 1:1.1.4)Description: command line tool for transferring data with URL syntax curl is a command line tool for transferring data with URL syntax, supporting DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, TELNET and TFTP. . curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, kerberos...), file transfer resume, proxy tunneling and a busload of other useful tricks.Homepage: http://curl.haxx.seOriginal-Maintainer: Alessandro Ghedini <ghedo@debian.org>

二、安装rvm

用rvm官方推荐的方式安装:curl -L get.rvm.io | bash -s stable

 

zhaomh@ZHAO:~$ curl -L get.rvm.io | bash -s stable  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current                                 Dload  Upload   Total   Spent    Left  Speed100   184  100   184    0     0     71      0  0:00:02  0:00:02 --:--:--    71100 22686  100 22686    0     0   6852      0  0:00:03  0:00:03 --:--:--     0Downloading https://github.com/wayneeseguin/rvm/archive/1.26.1.tar.gzDownloading https://github.com/wayneeseguin/rvm/releases/download/1.26.1/1.26.1.tar.gz.ascgpg: 已创建目录‘/home/zhaomh/.gnupg’gpg: 新的配置文件‘/home/zhaomh/.gnupg/gpg.conf’已建立gpg: 警告:在‘/home/zhaomh/.gnupg/gpg.conf’里的选项于此次运行期间未被使用gpg: 钥匙环‘/home/zhaomh/.gnupg/pubring.gpg’已建立gpg: 于 2014年11月12日 星期三 05时23分59秒 CST 创建的签名,使用 RSA,钥匙号 BF04FF17gpg: 无法检查签名:找不到公钥Warning, RVM 1.26.0 introduces signed releases and automated check of signatures when GPG software found.Assuming you trust Michal Papis import the mpapis public key (downloading the signatures).GPG signature verification failed for /home/zhaomh/.rvm/archives/rvm-1.26.1.tgz - https://github.com/wayneeseguin/rvm/releases/download/1.26.1/1.26.1.tar.gz.asc!try downloading the signatures:    gpg --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3or if it fails:    command curl -sSL https://rvm.io/mpapis.asc | gpg --import -the key can be compared with:    https://rvm.io/mpapis.asc    https://keybase.io/mpapis

 

安装失败,然后按照提示看,前面的都看不懂,只认识一个单词是干嘛的,然后:

curl -sSL https://rvm.io/mpapis.asc | gpg --import 

接着输入上面的安装RVM的命令:

zhaomh@ZHAO:~$ curl -L get.rvm.io | bash -s stable  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current                                 Dload  Upload   Total   Spent    Left  Speed100   184  100   184    0     0     76      0  0:00:02  0:00:02 --:--:--    76100 22686  100 22686    0     0   6767      0  0:00:03  0:00:03 --:--:--  307kDownloading https://github.com/wayneeseguin/rvm/archive/1.26.1.tar.gzDownloading https://github.com/wayneeseguin/rvm/releases/download/1.26.1/1.26.1.tar.gz.ascgpg: 于 2014年11月12日 星期三 05时23分59秒 CST 创建的签名,使用 RSA,钥匙号 BF04FF17gpg: 完好的签名,来自于“Michal Papis (RVM signing) <mpapis@gmail.com>”gpg: 警告:这把密钥未经受信任的签名认证!gpg:          没有证据表明这个签名属于它所声称的持有者。主钥指纹: 409B 6B17 96C2 7546 2A17  0311 3804 BB82 D39D C0E3     子钥指纹: 62C9 E5F4 DA30 0D94 AC36  166B E206 C29F BF04 FF17GPG verified /home/zhaomh/.rvm/archives/rvm-1.26.1.tgzInstalling RVM to /home/zhaomh/.rvm/    Adding rvm PATH line to /home/zhaomh/.profile /home/zhaomh/.bashrc /home/zhaomh/.zshrc.    RVM sourcing line found in /home/zhaomh/.bashrc.    RVM sourcing line not found for Zsh, rerun this command with --auto-dotfiles flag to fix it.Installation of RVM in /home/zhaomh/.rvm/ is almost complete:  * To start using RVM you need to run `source /home/zhaomh/.rvm/scripts/rvm`    in all your open shell windows, in rare cases you need to reopen all shell windows.# zhaomh,##   Thank you for using RVM!#   We sincerely hope that RVM helps to make your life easier and more enjoyable!!!## ~Wayne, Michal & team.In case of problems: http://rvm.io/help and https://twitter.com/rvm_io

查看版本:

zhaomh@ZHAO:~$ echo [[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" >>~/.bashrczhaomh@ZHAO:~$ source ~/.bashrczhaomh@ZHAO:~$ rvm -vrvm 1.26.1 (latest) by Wayne E. Seguin <wayneeseguin@gmail.com>, Michal Papis <mpapis@gmail.com> [https://rvm.io/]zhaomh@ZHAO:~$ 

三、安装Ruby

1、首先先将源定位到淘宝的Ruby镜像里

sed -i s!cache.ruby-lang.org/pub/ruby!ruby.taobao.org/mirrors/ruby! $rvm_path/config/db

2、先解决依赖,安装依赖程序,至于解释,边晓宇@CSDN博客里是这么解释的:

通过rvm requirements命令,可以察看安装各版本时候的前提条件。其中这句是需要关注的。这是安装依赖的第三方包,没有这个安装不成功岂不是很悲剧?
rvm requirements && rvm pkg install readline

3、安装特定版本的ruby

zhaomh@ZHAO:~$ rvm list known  # MRI Rubies[ruby-]1.8.6[-p420][ruby-]1.8.7[-head] # security released on head[ruby-]1.9.1[-p431][ruby-]1.9.2[-p330][ruby-]1.9.3[-p550][ruby-]2.0.0-p576[ruby-]2.0.0[-p594][ruby-]2.1.3[ruby-]2.1[.4][ruby-]2.1-headruby-head# for forks use: rvm install ruby-head-<name> --url https://github.com/github/ruby.git --branch 2.1# JRubyjruby-1.6.8jruby[-1.7.16.1]jruby-head# Rubiniusrbx-1.3.3rbx-2.0.0rbx-2.1.1rbx[-2.2.10]rbx-head# Opalopal# Minimalistic ruby implementation - ISO 30170:2012mruby[-head]# Ruby Enterprise Editionree-1.8.6ree[-1.8.7][-2012.02]# GoRubygoruby# Topaztopaz# MagLevmaglev[-head]maglev-1.0.0# Mac OS X Snow Leopard Or Newermacruby-0.10macruby-0.11macruby[-0.12]macruby-nightlymacruby-head# IronRubyironruby[-1.1.3]ironruby-headzhaomh@ZHAO:~$ rvm install 2.1.3Searching for binary rubies, this might take some time.Found remote file https://rvm.io/binaries/ubuntu/14.04/x86_64/ruby-2.1.3.tar.bz2Checking requirements for ubuntu.Requirements installation successful.ruby-2.1.3 - #configureruby-2.1.3 - #download** Resuming transfer from byte position 4845568  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current                                 Dload  Upload   Total   Spent    Left  Speed100 18.3M  100 18.3M    0     0   142k      0  0:02:11  0:02:11 --:--:--  161kruby-2.1.3 - #validate archiveruby-2.1.3 - #extractruby-2.1.3 - #validate binaryruby-2.1.3 - #setupruby-2.1.3 - #gemset created /home/zhaomh/.rvm/gems/ruby-2.1.3@globalruby-2.1.3 - #importing gemset /home/zhaomh/.rvm/gemsets/global.gems..........................................ruby-2.1.3 - #generating global wrappers........ruby-2.1.3 - #gemset created /home/zhaomh/.rvm/gems/ruby-2.1.3ruby-2.1.3 - #importing gemsetfile /home/zhaomh/.rvm/gemsets/default.gems evaluated to empty gem listruby-2.1.3 - #generating default wrappers........zhaomh@ZHAO:~$ ruby -vruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-linux]zhaomh@ZHAO:~$ rvm use 2.1.2 --defaultruby-2.1.2 is not installed.To install do: rvm install ruby-2.1.2zhaomh@ZHAO:~$ rvm use 2.1.3 --defaultUsing /home/zhaomh/.rvm/gems/ruby-2.1.3zhaomh@ZHAO:~$ rvm -vrvm 1.26.1 (latest) by Wayne E. Seguin <wayneeseguin@gmail.com>, Michal Papis <mpapis@gmail.com> [https://rvm.io/]zhaomh@ZHAO:~$ ruby -vruby 2.1.3p242 (2014-09-19 revision 47630) [x86_64-linux]

如果想UNDO上面的版本默认设置可以

zhaomh@ZHAO:~$rvm reset

或者

zhaomh@ZHAO:~$ rvm --help | grep default


抓取与default相关的设置。

四、安装rails

1、查看gem,并更换gem源为淘宝镜像

zhaomh@ZHAO:~$ gem -v2.2.2zhaomh@ZHAO:~$ gem source*** CURRENT SOURCES ***https://rubygems.org/zhaomh@ZHAO:~$ gem source -r https://rubygems.org/https://rubygems.org/ removed from sourceszhaomh@ZHAO:~$ gem source -a https://ruby.taobao.orghttps://ruby.taobao.org added to sourceszhaomh@ZHAO:~$ gem source*** CURRENT SOURCES ***https://ruby.taobao.org

2、安装Rails

zhaomh@ZHAO:~$ gem install railsFetching: thread_safe-0.3.4.gem (100%)Successfully installed thread_safe-0.3.4Fetching: minitest-5.4.3.gem (100%)Successfully installed minitest-5.4.3......(省略)Parsing documentation for rails-4.1.7Installing ri documentation for rails-4.1.7Parsing documentation for railties-4.1.7Installing ri documentation for railties-4.1.7Parsing documentation for sprockets-3.0.0.beta.3Installing ri documentation for sprockets-3.0.0.beta.3Parsing documentation for sprockets-rails-2.2.0Installing ri documentation for sprockets-rails-2.2.0Parsing documentation for thor-0.19.1Installing ri documentation for thor-0.19.1Parsing documentation for thread_safe-0.3.4Installing ri documentation for thread_safe-0.3.4Parsing documentation for tzinfo-1.2.2Installing ri documentation for tzinfo-1.2.2Done installing documentation for actionmailer, actionpack, actionview, activemodel, activerecord, activesupport, arel, builder, erubis, i18n, mail, mime-types, minitest, rack, rack-test, rails, railties, sprockets, sprockets-rails, thor, thread_safe, tzinfo after 504 seconds22 gems installed

假如想安装特定的版本的话:比如我想要安装Rails-4.1.6版本的话(我安装时4.1.7刚出来)

gem install rails --version=4.1.6

 

五、其他扩展

1、gem命令是管理rubyGem包的,如果有需要可以查看帮助怎么使用;

2、RVM(ruby version manager)是用来管理ruby版本的工具,但是可以借助RVM来切换ruby 的版本(参考来源)

rvm use  2.1.2

并且绑定特定的Rail的版本(这要注意版本支不支持rails)

rvm use2.1.2@rails4.1.6

这个话,最好参考RVM的命令帮助,因为不排除版本原因会更换命令形式。

但是绑定之后可能再次单独调用Rail会找不到,比如

  rails -v

命令。

 

Ubuntu下安装 Ruby, Rails 运行环境