さくらVPSにRubyとRuby on Railsをインストールする

さくらVPSにRubyとRuby on Railsをインストールする

先日借りたさくらVPSでRubyを使おうと思ったらデフォルトでインストールされていないようなのでRuby環境を構築することにする。ついでにRuby on Railsもインストールすることにする。

まずRubyをインストール

$ sudo su -
# yum install ruby
Installed:
  ruby.x86_64 0:1.8.5-5.el5_4.8

Dependency Installed:
  ruby-libs.x86_64 0:1.8.5-5.el5_4.8

Complete!

次にRubyGemsをインストールする。 ただし、Ruby1.8.5だと、Gemsは1.3.5でないとインストールできない。1.3.6以降を使いたい場合は、Ruby1.8.6以上をインストールする。

# wget http://rubyforge.org/frs/download.php/60718/rubygems-1.3.5.tgz
# tar xvfz rubygems-1.3.5.tgz
# cd rubygems-1.3.5
# ruby setup.rb
RubyGems 1.3.5 installed
./lib/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- rdoc/rdoc (LoadError)
        from ./lib/rubygems/custom_require.rb:31:in `require'
        from ./lib/rubygems/commands/setup_command.rb:352:in `run_rdoc'
        from ./lib/rubygems/commands/setup_command.rb:247:in `install_rdoc'
        from ./lib/rubygems/commands/setup_command.rb:120:in `execute'
        from ./lib/rubygems/command.rb:257:in `invoke'
        from ./lib/rubygems/command_manager.rb:132:in `process_args'
        from ./lib/rubygems/command_manager.rb:102:in `run'
        from ./lib/rubygems/gem_runner.rb:58:in `run'
        from setup.rb:35

rdocがないって言われたので、rdocをインストールして、再びGemsインストールを実行

# yum install rdoc
Installed:
  ruby-rdoc.x86_64 0:1.8.5-5.el5_4.8

Dependency Installed:
  ruby-irb.x86_64 0:1.8.5-5.el5_4.8

Complete!

# ruby setup.rb
RubyGems 1.3.5 installed

=== 1.3.5 / 2009-07-21

Bug fixes:

* Fix use of prerelease gems.
* Gem.bin_path no longer escapes path with spaces. Bug #25935 and #26458.

Deprecation Notices:

* Bulk index update is no longer supported (the code currently remains, but not
  the tests)
* Gem::manage_gems was removed in 1.3.3.
* Time::today was removed in 1.3.3.

------------------------------------------------------------------------------

RubyGems installed the following executables:
        /usr/bin/gem

これでRubyとRubyGemsのインストールができた。

次にRuby on Rails をインストールする。

# gem install rails
ERROR:  Error installing rails:
        activesupport requires Ruby version >= 1.8.7

Ruby1.8.7 以上いれろって怒られた。 さすがにめんどくさくなってきたけどもうちょっと頑張ることにする。

仕方がないので、Ruby1.8.7をインストールする。 最新版のソースを落としてビルドする。

# wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p249.tar.bz2
# tar jvxf ruby-1.8.7-p249.tar.bz2
# cd ruby-1.8.7-p249
# ./configure
# make && make install
# make clean

Ruby1.8.7がインストールできたので、Ruby1.8.5を削除する。

# yum remove ruby
Removed:
  ruby.x86_64 0:1.8.5-5.el5_4.8

Dependency Removed:
  ruby-irb.x86_64 0:1.8.5-5.el5_4.8            ruby-rdoc.x86_64 0:1.8.5-5.el5_4.8

Complete!

Rubyのバージョンを確認する。

# ruby -v
ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux]

gemsも最新版をインストールする。 最新版URLはこちらで確認

# wget http://rubyforge.org/frs/download.php/70696/rubygems-1.3.7.tgz
# tar xvfz rubygems-1.3.7.tgz
# cd rubygems-1.3.7
# ruby setup.rb
# gem -v
1.3.7

これで、Ruby1.8.7、RubyGems1.3.7がインストールできた。 と思いきや、リストを参照したらエラーがでた。

# gem list
ERROR:  Loading command: list (LoadError)
    no such file to load -- zlib
ERROR:  While executing gem ... (NameError)
    uninitialized constant Gem::Commands::ListCommand

zlibないって言われた。でもzlib入ってるし。 ググったら同じ問題にぶつかった人がいたのでこちらを参考にzlib-devel入れて再コンパイルしてみた。そしたらすんなり通りやがりましたよ。

# cd
# yum install zlib-devel
Installed:
  zlib-devel.i386 0:1.2.3-3                    zlib-devel.x86_64 0:1.2.3-3

Complete!

# cd ruby-1.8.7-p249
# cd ext/
# ruby extconf.rb --with-zlib-include=/usr/include -with-zlib-lib=/usr/lib
checking for deflateReset() in -lz... yes
checking for zlib.h... yes
checking for kind of operating system... Unix
creating Makefile

# make && make install
# gem list

*** LOCAL GEMS ***

でーけたー!これでやっとRailsをインストールできるはず。

# gem install rails
Successfully installed activesupport-3.0.1
Successfully installed builder-2.1.2
Successfully installed i18n-0.4.2
Successfully installed activemodel-3.0.1
Successfully installed rack-1.2.1
Successfully installed rack-test-0.5.6
Successfully installed rack-mount-0.6.13
Successfully installed tzinfo-0.3.23
Successfully installed abstract-1.0.0
Successfully installed erubis-2.6.6
Successfully installed actionpack-3.0.1
Successfully installed arel-1.0.1
Successfully installed activerecord-3.0.1
Successfully installed activeresource-3.0.1
Successfully installed mime-types-1.16
Successfully installed polyglot-0.3.1
Successfully installed treetop-1.4.8
Successfully installed mail-2.2.9
Successfully installed actionmailer-3.0.1
Successfully installed rake-0.8.7
Successfully installed thor-0.14.4
Successfully installed railties-3.0.1
Successfully installed bundler-1.0.3
Successfully installed rails-3.0.1
24 gems installed
Installing ri documentation for activesupport-3.0.1...
Installing ri documentation for builder-2.1.2...
ERROR:  While generating documentation for builder-2.1.2
... MESSAGE:   Unhandled special: Special: type=17, text="<!-- HI -->"
... RDOC args: --ri --op /usr/local/lib/ruby/gems/1.8/doc/builder-2.1.2/ri --title Builder -- Easy XML Building --main README --line-numbers --quiet lib CHANGES Rakefile README doc/releases/builder-1.2.4.rdoc doc/releases/builder-2.0.0.rdoc doc/releases/builder-2.1.1.rdoc --title builder-2.1.2 Documentation
(continuing with the rest of the installation)
Installing ri documentation for i18n-0.4.2...
Installing ri documentation for activemodel-3.0.1...
Installing ri documentation for rack-1.2.1...
Installing ri documentation for rack-test-0.5.6...
Installing ri documentation for rack-mount-0.6.13...
Installing ri documentation for tzinfo-0.3.23...
Installing ri documentation for abstract-1.0.0...
Installing ri documentation for erubis-2.6.6...
Installing ri documentation for actionpack-3.0.1...
Installing ri documentation for arel-1.0.1...
Installing ri documentation for activerecord-3.0.1...
Installing ri documentation for activeresource-3.0.1...
Installing ri documentation for mime-types-1.16...
Installing ri documentation for polyglot-0.3.1...
Installing ri documentation for treetop-1.4.8...
Installing ri documentation for mail-2.2.9...
Installing ri documentation for actionmailer-3.0.1...
Installing ri documentation for rake-0.8.7...
Installing ri documentation for thor-0.14.4...
Installing ri documentation for railties-3.0.1...
Installing ri documentation for bundler-1.0.3...
Installing ri documentation for rails-3.0.1...
File not found: lib

なんか途中エラーでた。なんじゃこりゃ。 とりあえず確認してみる。

# rails -v
Rails 3.0.1

Rails3.x系になってから、createコマンドがRails2系と違うみたいで、下記ではcreateできなかった。

$ rails testapp
Usage:
  rails new APP_PATH [options]

ので、Rails3.0にのっとってcreateしてみる。

$ rails new testapp
      create
      create  README
      create  Rakefile
      create  config.ru
      create  .gitignore
      create  Gemfile
      create  app
      create  app/controllers/application_controller.rb
      create  app/helpers/application_helper.rb
      create  app/mailers
      create  app/models
      create  app/views/layouts/application.html.erb
      create  config
      create  config/routes.rb
      create  config/application.rb
      create  config/environment.rb
      create  config/environments
      create  config/environments/development.rb
      create  config/environments/production.rb
      create  config/environments/test.rb
      create  config/initializers
      create  config/initializers/backtrace_silencers.rb
      create  config/initializers/inflections.rb
      create  config/initializers/mime_types.rb
      create  config/initializers/secret_token.rb
      create  config/initializers/session_store.rb
      create  config/locales
      create  config/locales/en.yml
      create  config/boot.rb
      create  config/database.yml
      create  db
      create  db/seeds.rb
      create  doc
      create  doc/README_FOR_APP
      create  lib
      create  lib/tasks
      create  lib/tasks/.gitkeep
      create  log
      create  log/server.log
      create  log/production.log
      create  log/development.log
      create  log/test.log
      create  public
      create  public/404.html
      create  public/422.html
      create  public/500.html
      create  public/favicon.ico
      create  public/index.html
      create  public/robots.txt
      create  public/images
      create  public/images/rails.png
      create  public/stylesheets
      create  public/stylesheets/.gitkeep
      create  public/javascripts
      create  public/javascripts/application.js
      create  public/javascripts/controls.js
      create  public/javascripts/dragdrop.js
      create  public/javascripts/effects.js
      create  public/javascripts/prototype.js
      create  public/javascripts/rails.js
      create  script
      create  script/rails
      create  test
      create  test/fixtures
      create  test/functional
      create  test/integration
      create  test/performance/browsing_test.rb
      create  test/test_helper.rb
      create  test/unit
      create  tmp
      create  tmp/sessions
      create  tmp/sockets
      create  tmp/cache
      create  tmp/pids
      create  vendor/plugins
      create  vendor/plugins/.gitkeep

testappを動かしてみる

$ cd testapp/
$ rails server
Could not find gem 'sqlite3-ruby (>= 0, runtime)' in any of the gem sources.
Try running `bundle install`.

“sqlite3-ruby” が入ってないとのこと。 “sqlite3-ruby” を入れる。

$ cd testapp/
$ vi Gemfile
gem 'sqlite3-ruby', :require => 'sqlite3'

$ sudo gem install sqlite3-ruby
Building native extensions.  This could take a while...
ERROR:  Error installing sqlite3-ruby:
        ERROR: Failed to build gem native extension.

/usr/local/bin/ruby extconf.rb
checking for sqlite3.h... yes
checking for sqlite3_libversion_number() in -lsqlite3... yes
checking for rb_proc_arity()... no
checking for sqlite3_initialize()... no
sqlite3-ruby only supports sqlite3 versions 3.6.16+, please upgrade!
*** 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.

Gem files will remain installed in /usr/local/lib/ruby/gems/1.8/gems/sqlite3-ruby-1.3.2 for inspection.
Results logged to /usr/local/lib/ruby/gems/1.8/gems/sqlite3-ruby-1.3.2/ext/sqlite3/gem_make.out

“sqlite3-ruby only supports sqlite3 versions 3.6.16+, please upgrade!” って新しいのいれろってことかしら。

sqlite3のバージョン確認

$ rpm -q sqlite
sqlite-3.3.6-5

古い。そういえばsqlite-develも入っていなかったので、インストールする。

# yum install sqlite-devel
Installed:
  sqlite-devel.i386 0:3.3.6-5                sqlite-devel.x86_64 0:3.3.6-5

Complete!

sqlite3をソースからインストールする。

$ cd /usr/local/src
$ sudo wget http://www.sqlite.org/sqlite-amalgamation-3.7.3.tar.gz
$ sudo tar xvzf sqlite-3.7.3.tar.gz 
$ cd sqlite-3.7.3
$ sudo ./configure
$ sudo make
$ sudo make install

再びsqlite3-ruby をインストール

$ sudo gem install sqlite3-ruby
Building native extensions.  This could take a while...
Successfully installed sqlite3-ruby-1.3.2
1 gem installed
Installing ri documentation for sqlite3-ruby-1.3.2...

No definition for libversion

Enclosing class/module 'mSqlite3' for class Statement not known
Installing RDoc documentation for sqlite3-ruby-1.3.2...

No definition for libversion

Enclosing class/module 'mSqlite3' for class Statement not known

sqlite3-ruby を確認

$ gem list


*** LOCAL GEMS ***


abstract (1.0.0)
actionmailer (3.0.1)
actionpack (3.0.1)
activemodel (3.0.1)
activerecord (3.0.1)
activeresource (3.0.1)
activesupport (3.0.1)
arel (1.0.1)
bcrypt-ruby (2.1.2)
builder (2.1.2)
bundler (1.0.3)
erubis (2.6.6)
i18n (0.4.2)
mail (2.2.9)
mime-types (1.16)
polyglot (0.3.1)
rack (1.2.1)
rack-mount (0.6.13)
rack-test (0.5.6)
rails (3.0.1)
railties (3.0.1)
rake (0.8.7)
sqlite3-ruby (1.3.2)
thor (0.14.4)
treetop (1.4.8)
tzinfo (0.3.23)

はいったー! railsサーバーを立ち上げてみる。

$ cd
$ cd dev/rails/testapp/
$ rails server
=> Booting WEBrick
=> Rails 3.0.1 application starting in development on http://0.0.0.0:3000

http://localhost:3000/ にアクセス。

やったー!うごいたー!