Ruby1.9.3をインストールしたときのいろいろなエラー

EC2で新しく環境構築して、Ruby1.9.3をソースからコンパイルしてgemをアップデートしようとしたら以下のエラーが出た。

[root@hoge ~]# gem update --system
/usr/local/lib/ruby/1.9.1/yaml.rb:56:in `<top (required)>':
It seems your ruby installation is missing psych (for YAML output).
To eliminate this warning, please install libyaml and reinstall your ruby.
ERROR:  Loading command: update (LoadError)
    cannot load such file -- zlib
ERROR:  While executing gem ... (NameError)
    uninitialized constant Gem::Commands::UpdateCommand

とりあえず、yumで以下を入れる。

yum install zlib-devel

再度Rubyコンパイルをして確認

[root@hoge ~]# gem update --system
/usr/local/lib/ruby/1.9.1/yaml.rb:56:in `<top (required)>':
It seems your ruby installation is missing psych (for YAML output).
To eliminate this warning, please install libyaml and reinstall your ruby.

エラーは出なくなったけど、警告がまだ消えない。
以下からソースファイルを持ってきてコンパイル
http://pyyaml.org/wiki/LibYAML
http://pyyaml.org/download/libyaml/yaml-0.1.4.tar.gz


んで、再度Rubyコンパイルしたらうまくいきました。
が、passengerをインストールしようとしたらこんなエラーが、、

                                                                                      • -
Checking for required software... * GNU C++ compiler... found at /usr/bin/g++ * Curl development headers with SSL support... found * OpenSSL development headers... found * Zlib development headers... found * Ruby development headers... found * OpenSSL support for Ruby... not found * RubyGems... found * Rake... found at /usr/local/bin/rake * rack... found * Apache 2... found at /usr/sbin/httpd * Apache 2 development headers... found at /usr/sbin/apxs * Apache Portable Runtime (APR) development headers... found at /usr/bin/apr-1-config * Apache Portable Runtime Utility (APU) development headers... found at /usr/bin/apu-1-config Some required software is not installed. But don't worry, this installer will tell you how to install them. Press Enter to continue, or Ctrl-C to abort.
                                                                                      • -

OpenSSL...ぬぬ、、以下を入れる。

yum -y install httpd-devel
yum -y install openssl-devel

再度Rubyコンパイルで無事完了

結論

Ruby1.9.3を素のAmazon Linuxにソースからインストールする場合は、まず以下を実行するべし

yum -y install gcc
yum -y install make
yum -y install gcc-c++
yum -y install zlib-devel
yum -y install httpd-devel
yum -y install openssl-devel
yum -y install curl-devel

これできっと幸せになれるはず

[追伸]
Rubyコンパイルを何度もやり直したのだが、最初はmicroインスタンスだったんだけど、時間かかりすぎるので、以降はxlargeにして作業してました


参考URL
http://d.hatena.ne.jp/donbulinux/20111111/1321004527


LionにしてからXCodeのアップデートするの忘れてた

rvmで新しくgemset作って、最新のrailsを入れようとしたら以下のようなエラーが出た。

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

        /Users/yamazaki/.rvm/rubies/ruby-1.9.2-p180/bin/ruby extconf.rb
checking for re.h... *** 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.

Provided configuration options:
	--with-opt-dir
	--without-opt-dir
	--with-opt-include
	--without-opt-include=${opt-dir}/include
	--with-opt-lib
	--without-opt-lib=${opt-dir}/lib
	--with-make-prog
	--without-make-prog
	--srcdir=.
	--curdir
	--ruby=/Users/yamazaki/.rvm/rubies/ruby-1.9.2-p180/bin/ruby
/Users/yamazaki/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/mkmf.rb:368:in `try_do': The complier failed to generate an executable file. (RuntimeError)
You have to install development tools first.
	from /Users/yamazaki/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/mkmf.rb:452:in `try_cpp'
	from /Users/yamazaki/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/mkmf.rb:834:in `block in have_header'
	from /Users/yamazaki/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/mkmf.rb:693:in `block in checking_for'
	from /Users/yamazaki/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/mkmf.rb:280:in `block (2 levels) in postpone'
	from /Users/yamazaki/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/mkmf.rb:254:in `open'
	from /Users/yamazaki/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/mkmf.rb:280:in `block in postpone'
	from /Users/yamazaki/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/mkmf.rb:254:in `open'
	from /Users/yamazaki/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/mkmf.rb:276:in `postpone'
	from /Users/yamazaki/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/mkmf.rb:692:in `checking_for'
	from /Users/yamazaki/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/mkmf.rb:833:in `have_header'
	from extconf.rb:14:in `
' Gem files will remain installed in /Users/yamazaki/.rvm/gems/ruby-1.9.2-p180@oshiri/gems/json-1.6.1 for inspection. Results logged to /Users/yamazaki/.rvm/gems/ruby-1.9.2-p180@oshiri/gems/json-1.6.1/ext/json/ext/parser/gem_make.out

なんかコンパイルでエラーになってるようだけど、、と思ったらgccが入ってない。。あれれ??
そういえばMacをLionにアップデートしてからXcodeのアップデートしてないなということでAppStoreからXcodeをダウンロードしてアップデート。無事にインストールできました^^

case expressions must be constant expressions

久しぶりにEclipseを立ち上げたら表題のような意味不明なエラーが出た。
で、ググったらADT 14からライブラリプロジェクトに限って、Rがfinalでなくなったのでcase文が使えなくなったぽい。
http://tools.android.com/tips/non-constant-fields


if文に変更したら無事解決。

Ctrl-1 (or Cmd-1 on Mac):で簡単にif-elseに変換できるウルトラ技があったことを初めて知ったw


で、上記が解決したら以下のようなエラーが。。次から次へとまったく。。
Error generating final archive: Debug Certificate expired


以下のサイトを参考にして、debug.keystoreを削除してproject cleanしたら直りました
http://d.hatena.ne.jp/hypercrab/20100619/1276925032
macの場合は~/.android/以下にあります

railsでsitemapを自動生成するライブラリ

サイトを運営していると、SEO対策としてsitemapを作ることがあると思います。
[Google ウェブマスターヘルプ]
https://www.google.com/support/webmasters/bin/answer.py?hl=ja&answer=183668&ctx=cb&src=cb&cbid=-35ar1gb5e7oa&cbrank=0


これ手動で作成するのは面倒なので自動化したいです。ツールはたくさんあるようですけど、railsの場合だとこれを使うと便利です。
[sitemap_generator]
http://rubygems.org/gems/sitemap_generator


使い方は以下のような感じ ※rails3の場合
Gemfileに以下を追加後、bundle install

gem 'sitemap_generator'

installが終わったら

rake sitemap:install

を実行するとconfig/sitemap.rbというファイルができあがります。


sitemap.rbに実際にファイルに書き込まれる内容をプログラムとして記述します。
[例]

SitemapGenerator::Sitemap.default_host = "http://www.hoge.com"

SitemapGenerator::Sitemap.create do
  add "/info", :priority => 0.5
  add "/info/about", :priority => 0.5
  News.find_each do |n|
    add "/news/detail/#{n.id}", :lastmod => n.updated_at
  end
  ・
  ・
end

下記を実行するとsitemap_index.xml.gz,sitemap1.xml.gzというファイルができると思います。

rake sitemap:refresh

※エラーになったらsitemap.rbのエラー箇所を修正


ファイルが作成されるのを確認できたら、robots.txtにsitemapの記述をします。

Sitemap: http://www.example.com/sitemap_index.xml.gz


最後にdeploy.rbに以下を記述 ※railsだったらcapistranoは必須だよね?

after "deploy", "refresh_sitemaps"
task :refresh_sitemaps do
  run "cd #{latest_release} && RAILS_ENV=#{rails_env} rake sitemap:refresh"
end

これでデプロイした際にsitemapが更新されるようになります。

Rails3のTimezoneについて

Rails3のTimezoneを設定する場合には、application.rbに下記のように書いたりするわけですが、

config.time_zone = 'Tokyo'

これだけだとActiveRecordからDBに登録される時間がUTCで保存されてしまいます。


これは前から知ってたのですが、表示の際にはActiveRecordがうまく変換してくれるのでいいかなーと思ってほっときました。
が、日付で検索をかけるときに、いちいちUTCに変換かけないといけないことが判明したので対応することにしました。


やりかたは簡単でapplication.rbに以下も追加します。
config.active_record.default_timezone = 'Tokyo'

config.active_record.default_timezone = :local

これでDBにも設定されたTimezoneで保存されます。


★追記 2011/10/15
default_timezoneに'Tokyo'と書くとApacheのerrorログに「warning: :database_timezone option must be :utc or :local」と大量にはき出されてました><
なので:localに修正

参考URL
http://d.hatena.ne.jp/mosa003/20110412/1302615346

ある日rakeでエラーになったので対応方法のメモ

rake db:migrateを実行したら以下のエラーが。。というかrake自体がだめっぽい

$ rake -T
/Users/yamazaki/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2/lib/rake/version.rb:4: warning: already initialized constant MAJOR
/Users/yamazaki/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2/lib/rake/version.rb:5: warning: already initialized constant MINOR
/Users/yamazaki/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2/lib/rake/version.rb:6: warning: already initialized constant BUILD
/Users/yamazaki/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2/lib/rake/version.rb:3: warning: already initialized constant NUMBERS
/Users/yamazaki/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2/lib/rake/version.rb:9: warning: already initialized constant VERSION
/Users/yamazaki/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2/lib/rake.rb:26: warning: already initialized constant RAKEVERSION
/Users/yamazaki/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2/lib/rake/early_time.rb:17: warning: already initialized constant EARLY
/Users/yamazaki/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2/lib/rake/alt_system.rb:32: warning: already initialized constant WINDOWS
/Users/yamazaki/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2/lib/rake/application.rb:28: warning: already initialized constant DEFAULT_RAKEFILES
WARNING: Possible conflict with Rake extension: String#ext already exists
WARNING: Possible conflict with Rake extension: String#pathmap already exists
/Users/yamazaki/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2/lib/rake/task_arguments.rb:73: warning: already initialized constant EMPTY_TASK_ARGS
/Users/yamazaki/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2/lib/rake/invocation_chain.rb:49: warning: already initialized constant EMPTY
/Users/yamazaki/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2/lib/rake/file_utils.rb:10: warning: already initialized constant RUBY
/Users/yamazaki/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2/lib/rake/file_utils.rb:84: warning: already initialized constant LN_SUPPORTED
/Users/yamazaki/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2/lib/rake/dsl_definition.rb:143: warning: already initialized constant Commands
/Users/yamazaki/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2/lib/rake/file_list.rb:44: warning: already initialized constant ARRAY_METHODS
/Users/yamazaki/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2/lib/rake/file_list.rb:47: warning: already initialized constant MUST_DEFINE
/Users/yamazaki/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2/lib/rake/file_list.rb:51: warning: already initialized constant MUST_NOT_DEFINE
/Users/yamazaki/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2/lib/rake/file_list.rb:55: warning: already initialized constant SPECIAL_RETURN
/Users/yamazaki/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2/lib/rake/file_list.rb:61: warning: already initialized constant DELEGATING_METHODS
/Users/yamazaki/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2/lib/rake/file_list.rb:364: warning: already initialized constant DEFAULT_IGNORE_PATTERNS
/Users/yamazaki/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2/lib/rake/file_list.rb:370: warning: already initialized constant DEFAULT_IGNORE_PROCS
/Users/yamazaki/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2/lib/rake.rb:64: warning: already initialized constant FileList
/Users/yamazaki/.rvm/gems/ruby-1.9.2-p180@global/gems/rake-0.9.2/lib/rake.rb:65: warning: already initialized constant RakeFileUtils
rake aborted!
stack level too deep

(See full trace by running task with --trace)

うーむ。多分、rubyを1.9に変更したのが原因だな

rvmを使っていたので、gemsetを新しく作ることで回避できました。

rvm gemset create hoge
rvm gemset use hoge

新しくrailsを入れる

gem install rails

プロジェクトのディレクトリに移動して

bundle install --system

無事解決

$ rake -T
rake about              # List versions of all Rails frameworks and the environment
rake db:create          # Create the database from config/database.yml for the current Rails.env (use db:create:all to create all dbs in the config)
rake db:drop            # Drops the database for the current Rails.env (use db:drop:all to drop all databases)
rake db:fixtures:load   # Load fixtures into the current environment's database.
rake db:migrate         # Migrate the database (options: VERSION=x, VERBOSE=false).
rake db:migrate:status  # Display status of migrations
rake db:rollback        # Rolls the schema back to the previous version (specify steps w/ STEP=n).
rake db:schema:dump     # Create a db/schema.rb file that can be portably used against any DB supported by AR
rake db:schema:load     # Load a schema.rb file into the database
rake db:seed            # Load the seed data from db/seeds.rb
rake db:setup           # Create the database, load the schema, and initialize with the seed data (use db:reset to also drop the db first)
rake db:structure:dump  # Dump the database structure to an SQL file
rake db:version         # Retrieves the current schema version number
rake doc:app            # Generate docs for the app -- also availble doc:rails, doc:guides, doc:plugins (options: TEMPLATE=/rdoc-template.rb, TITLE="Custom Title")
rake log:clear          # Truncates all *.log files in log/ to zero bytes
rake middleware         # Prints out your Rack middleware stack
rake notes              # Enumerate all annotations (use notes:optimize, :fixme, :todo for focus)
rake notes:custom       # Enumerate a custom annotation, specify with ANNOTATION=CUSTOM
rake rails:template     # Applies the template supplied by LOCATION=/path/to/template
rake rails:update       # Update both configs and public/javascripts from Rails (or use just update:javascripts or update:configs)
rake routes             # Print out all defined routes in match order, with names.
rake secret             # Generate a cryptographically secure secret key (this is typically used to generate a secret for cookie sessions).
rake stats              # Report code statistics (KLOCs, etc) from the application
rake test               # Runs test:units, test:functionals, test:integration together (also available: test:benchmark, test:profile, test:plugins)
rake test:recent        # Run tests for {:recent=>"test:prepare"} / Test recent changes
rake test:uncommitted   # Run tests for {:uncommitted=>"test:prepare"} / Test changes since last checkin (only Subversion and Git)
rake time:zones:all     # Displays all time zones, also available: time:zones:us, time:zones:local -- filter with OFFSET parameter, e.g., OFFSET=-6
rake tmp:clear          # Clear session, cache, and socket files from tmp/ (narrow w/ tmp:sessions:clear, tmp:cache:clear, tmp:sockets:clear)
rake tmp:create         # Creates tmp directories for sessions, cache, sockets, and pids

参考
http://blog.s21g.com/articles/1930

Rails本番運用で役に立つプラグイン exception_notification

結構有名なやつらしいのですが、ググったら情報が古い(Rails2系)のと、初めて使ったというのもありメモしておきます。

これは何かというと、アプリケーションでエラーが発生した際に、エラーが起きたことをメールで送信するというものです。本番運用ではエラーの監視は必須ですからね。

使い方は本当に簡単です。
# Rails3が前提です


まずはinstall

gem install exception_notification

http://rubygems.org/gems/exception_notification


Gemfileに以下を追記

gem 'exception_notification', :require => 'exception_notifier'


本番運用のみメールを送信する際は以下ファイルに記述
environments/production.rb

config.middleware.use ExceptionNotifier,
  :email_prefix => "[Whatever] ", # Subjectのpreフィックスを記述
  :sender_address => %{"notifier" <notifier@example.com>}, # Fromを記述
  :exception_recipients => %w{exceptions@example.com} # mail送信する一覧を記述

※テスト時にも確認したい場合は、development.rbに書く

これで完了です。(ActionMailerの設定もお忘れ無く)


飛んでくるメールの内容も、Sessionやら、Requestヘッダーの内容やら細かく記載してあるのでかなり重宝します。


[リファレンス]
http://rubydoc.info/gems/exception_notification/2.4.0/frames