在Fedora上安装Ruby on Rails

Posted on Jan 20, 2013

Practically all the software in the world is either broken or very difficult to use. So users dread software. They’ve been trained that whenever they try to install something, or even fill out a form online, it’s not going to work. I dread installing stuff, and I have a Ph.D. in computer science. Paul Graham, Founders at Work

这几年一直在用AS3.0做开发,已经很久没有接触Linux了。博客上挂着Fedora的倒计时牌子,每次看到新版本倒计时一个接一个地陆续发布,这个周末重新装上了Fedora,学习一些其他的Web开发知识。虽说Ruby on Rails这几年不是很流行了,而且近期Twitter改用java引起了不小的震动,有些人认为RoR过时了,Ruby日薄西山了。我倒是觉得技术人员与其关注这些,不如去专注学习技术。

ror_logo

安装步骤

Fedora更新非常快,我用的是目前最新的Fedora 18。虽然按照文档和教程一步一步安装的,但还是遇到了很多问题。安装的方法有很多种,这个和Ruby语言的特性也很相似 😉 。考虑到环境配置和开发工作,使用RVM安装比较好,RVM可以管理多个Ruby版本和RubyGem,在开发时可以根据需要切换版本,配置环境也很方便。使用yum安装ruby,管理比较麻烦,不适合开发工作。

安装git和cURL

可以考虑先更新所有包

yum update

安装git

yum install git-core

安装curl

yum install curl

安装RVM

RVM,即Ruby Version Manager,可以管理多个版本Ruby,RubyGem,Rails,对开发来说非常有用。

curl -L get.rvm.io | bash -s stable source ~/.rvm/scripts/rvm

安装完毕后检查RVM的软件包依赖

rvm requirements

输出信息如下:

Requirements for redhat/18./i386

NOTE: ‘ruby’ represents Matz’s Ruby Interpreter (MRI) (1.8.X, 1.9.X) This is the original / standard Ruby Language Interpreter ‘ree’ represents Ruby Enterprise Edition ‘rbx’ represents Rubinius

Additional Dependencies:

For update-system:

For rvm:

sudo yum install bash curl git patch

For ruby:

sudo yum install gcc-c++ patch readline readline-devel zlib zlib-devel libyaml-devel libffi-devel openssl-devel make bzip2 autoconf automake libtool bison iconv-devel

For jruby:

sudo yum install java

安装其它组件

这些软件是在运行时可能要用到的,根据需要安装。

Javascript运行时

访问ExecJS安装可支持的JavaScript运行时,推荐安装Node.js。 这里有点麻烦,可以先尝试通过yum查找,名称不一定是nodejs,如果有就安装

yum search nodejs yum install nodejs

如果没有,就去找合适的RPM包,或者直接下载源代码编译安装; 安装node.js

cd node sudo ./configure sudo make sudo make install

喝杯茶等待安装完毕,检查版本

[anthony@localhost ~]$ node -v v0.9.8-pre

SQLite等组件

yum install libxslt-devel libxml2-devel sqlite-devel

安装Ruby

安装了RVM之后,所有Ruby相关的都可以用它来安装配置了。

安装Ruby

rvm install 1.9.3

如果报错,需要添加参数

rvm install 1.9.3 –with-openssl-dir=$HOME/.rvm/

使用ruby 1.9.3作为默认版本

rvm use 1.9.3 –default

检查ruby版本

ruby -v

更新gems

gem update

安装Rails

安装rails

gem install rails

检查rails版本

rails -v

Rails测试 安装完了,接下来可以用rails生成个helloWorld了。

mkdir rails_projects cd rails_projects rails new first_app

开始创建,喝杯茶……

create create README.rdoc create Rakefile create config.ru create .gitignore create Gemfile create app create app/assets/images/rails.png create app/assets/javascripts/application.js create app/assets/stylesheets/application.css create app/controllers/application_controller.rb create app/helpers/application_helper.rb create app/views/layouts/application.html.erb create app/mailers/.gitkeep create app/models/.gitkeep create config create config/routes.rb create config/application.rb create config/environment.rb create config/environments . . . create vendor/plugins create vendor/plugins/.gitkeep run bundle install Fetching gem metadata from https://rubygems.org/……….. Fetching gem metadata from https://rubygems.org/.. Using rake (10.0.3) Using i18n (0.6.1) Using multi_json (1.5.0) Using activesupport (3.2.11) Using builder (3.0.4) Using activemodel (3.2.11) Using erubis (2.7.0) Using journey (1.0.4) Using rack (1.4.4) Using rails (3.2.11) Using sass (3.2.5) Using sass-rails (3.2.6) Using sqlite3 (1.3.7) Using uglifier (1.3.0) Your bundle is complete! Use bundle show [gemname] to see where a bundled gem is installed.

创建完毕,启动服务器

cd first_app rails server

好了,打开浏览器,登录 http://localhost:3000看看 🙂

总结

万事开头难,第一次安装时经常遇到各种问题:ruby安装失败,gem update报错,rails生成报错,启动报错等等。不过总算完成了,接下来的时间里就要去边做边学了。

另外,我也开始尝试使用Markdown来写文档了,这篇总结就是由Markdown格式转换的。喝水不忘挖井人,最后缅怀一下Aaron Swartz

Aaron Swartz


参考资料

  1. http://ruby.railstutorial.org/ruby-on-rails-tutorial-book
  2. http://blog.sudobits.com/2012/05/02/how-to-install-ruby-on-rails-in-ubuntu-12-04-lts/
  3. http://www.evansdiy.com/tips/the+way+to+install+nodejs
  4. https://github.com/joyent/node#readme