rvmとRubyのインストール
前回は、作業用ユーザーの環境に rvm と Ruby をインストールしたけど、今回は root ユーザーの環境にインストールする。ま、このへんは基本的に前回と同じ作業なので、簡単に。rvm のインストールから。
[root@www2465uo takatoh]# curl -L https://get.rvm.io | bash -s stable
[root@www2465uo takatoh]# source /etc/profile.d/rvm.sh
rvm list known
でインストール可能なバージョンが見られる。
[root@www2465uo takatoh]# 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[-head] # security released on head [ruby-]1.9.3[-p547] [ruby-]2.0.0-p451 [ruby-]2.0.0[-p481] [ruby-]2.1.1 [ruby-]2.1[.2] [ruby-]2.1-head ruby-head # GoRuby goruby # Topaz topaz # TheCodeShop - MRI experimental patches tcs # jamesgolick - All around gangster jamesgolick # Minimalistic ruby implementation - ISO 30170:2012 mruby[-head] # JRuby jruby-1.6.8 jruby[-1.7.13] jruby-head # Rubinius rbx-1.3.3 rbx-2.0.0 rbx-2.1.1 rbx[-2.2.10] rbx-head # Ruby Enterprise Edition ree-1.8.6 ree[-1.8.7][-2012.02] # Kiji kiji # MagLev maglev[-head] maglev-1.0.0 # Mac OS X Snow Leopard Or Newer macruby-0.10 macruby-0.11 macruby[-0.12] macruby-nightly macruby-head # Opal opal # IronRuby ironruby[-1.1.3] ironruby-head
Ruby 2.0.0 をインストール。
[root@www2465uo takatoh]# rvm install 2.0.0
[root@www2465uo takatoh]# ruby -v ruby 2.0.0p481 (2014-05-08 revision 45883) [x86_64-linux] [root@www2465uo takatoh]# which ruby /usr/local/rvm/rubies/ruby-2.0.0-p481/bin/ruby
へえ、root ユーザーでインストールすると /usr/local 以下にインストールされるんだ。
Ruby on Railsのインストール
[root@www2465uo takatoh]# gem install rails
長々とメッセージが出ていっぱいインストールされる。
サンプルアプリの作成
とりあえず Rails でアプリを作ってみる。
[root@www2465uo takatoh]# rails new myapp -d mysql create create README.rdoc create Rakefile create config.ru create .gitignore create Gemfile create app 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/assets/images/.keep create app/mailers/.keep create app/models/.keep create app/controllers/concerns/.keep create app/models/concerns/.keep create bin create bin/bundle create bin/rails create bin/rake create config create config/routes.rb create config/application.rb create config/environment.rb create config/secrets.yml create config/environments create config/environments/development.rb create config/environments/production.rb create config/environments/test.rb create config/initializers create config/initializers/assets.rb create config/initializers/backtrace_silencers.rb create config/initializers/cookies_serializer.rb create config/initializers/filter_parameter_logging.rb create config/initializers/inflections.rb create config/initializers/mime_types.rb create config/initializers/session_store.rb create config/initializers/wrap_parameters.rb create config/locales create config/locales/en.yml create config/boot.rb create config/database.yml create db create db/seeds.rb create lib create lib/tasks create lib/tasks/.keep create lib/assets create lib/assets/.keep create log create log/.keep create public create public/404.html create public/422.html create public/500.html create public/favicon.ico create public/robots.txt create test/fixtures create test/fixtures/.keep create test/controllers create test/controllers/.keep create test/mailers create test/mailers/.keep create test/models create test/models/.keep create test/helpers create test/helpers/.keep create test/integration create test/integration/.keep create test/test_helper.rb create tmp/cache create tmp/cache/assets create vendor/assets/javascripts create vendor/assets/javascripts/.keep create vendor/assets/stylesheets create vendor/assets/stylesheets/.keep run bundle install Don't run Bundler as root. Bundler can ask for sudo if it is needed, and installing your bundle as root will break this application for all non-root users on this machine. Fetching gem metadata from https://rubygems.org/........... Resolving dependencies... Using rake 10.3.2 Using i18n 0.6.11 Using json 1.8.1 Using minitest 5.4.0 Using thread_safe 0.3.4 Using tzinfo 1.2.2 Using activesupport 4.1.4 Using builder 3.2.2 Using erubis 2.7.0 Using actionview 4.1.4 Using rack 1.5.2 Using rack-test 0.6.2 Using actionpack 4.1.4 Using mime-types 1.25.1 Using polyglot 0.3.5 Using treetop 1.4.15 Using mail 2.5.4 Using actionmailer 4.1.4 Using activemodel 4.1.4 Using arel 5.0.1.20140414130214 Using activerecord 4.1.4 Using bundler 1.6.2 Using coffee-script-source 1.7.1 Using execjs 2.2.1 Using coffee-script 2.3.0 Using thor 0.19.1 Using railties 4.1.4 Using coffee-rails 4.0.1 Using hike 1.2.3 Using multi_json 1.10.1 Using jbuilder 2.1.3 Using jquery-rails 3.1.1 Gem::Ext::BuildError: ERROR: Failed to build gem native extension. /usr/local/rvm/rubies/ruby-2.0.0-p481/bin/ruby extconf.rb checking for ruby/thread.h... yes checking for rb_thread_call_without_gvl() in ruby/thread.h... yes checking for rb_thread_blocking_region()... yes checking for rb_wait_for_single_fd()... yes checking for rb_hash_dup()... yes checking for rb_intern3()... yes ----- Using mysql_config at /usr/bin/mysql_config ----- checking for mysql.h... no checking for mysql/mysql.h... no ----- mysql.h is missing. please check your installation of mysql and try again. ----- *** 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=/usr/local/rvm/rubies/ruby-2.0.0-p481/bin/ruby --with-mysql-dir --without-mysql-dir --with-mysql-include --without-mysql-include=${mysql-dir}/include --with-mysql-lib --without-mysql-lib=${mysql-dir}/ --with-mysql-config --without-mysql-config extconf failed, exit code 1 Gem files will remain installed in /usr/local/rvm/gems/ruby-2.0.0-p481/gems/mysql2-0.3.16 for inspection. Results logged to /usr/local/rvm/gems/ruby-2.0.0-p481/extensions/x86_64-linux/2.0.0/mysql2-0.3.16/gem_make.out An error occurred while installing mysql2 (0.3.16), and Bundler cannot continue. Make sure that `gem install mysql2 -v '0.3.16'` succeeds before bundling. run bundle exec spring binstub --all /usr/local/rvm/gems/ruby-2.0.0-p481@global/gems/bundler-1.6.2/lib/bundler/resolver.rb:352:in `resolve': Could not find gem 'mysql2 (>= 0) ruby' in the gems available on this machine. (Bundler::GemNotFound) from /usr/local/rvm/gems/ruby-2.0.0-p481@global/gems/bundler-1.6.2/lib/bundler/resolver.rb:165:in `start' from /usr/local/rvm/gems/ruby-2.0.0-p481@global/gems/bundler-1.6.2/lib/bundler/resolver.rb:129:in `resolve' from /usr/local/rvm/gems/ruby-2.0.0-p481@global/gems/bundler-1.6.2/lib/bundler/definition.rb:203:in `resolve' from /usr/local/rvm/gems/ruby-2.0.0-p481@global/gems/bundler-1.6.2/lib/bundler/definition.rb:133:in `specs' from /usr/local/rvm/gems/ruby-2.0.0-p481@global/gems/bundler-1.6.2/lib/bundler/definition.rb:178:in `specs_for' from /usr/local/rvm/gems/ruby-2.0.0-p481@global/gems/bundler-1.6.2/lib/bundler/definition.rb:167:in `requested_specs' from /usr/local/rvm/gems/ruby-2.0.0-p481@global/gems/bundler-1.6.2/lib/bundler/environment.rb:18:in `requested_specs' from /usr/local/rvm/gems/ruby-2.0.0-p481@global/gems/bundler-1.6.2/lib/bundler/runtime.rb:13:in `setup' from /usr/local/rvm/gems/ruby-2.0.0-p481@global/gems/bundler-1.6.2/lib/bundler.rb:120:in `setup' from /usr/local/rvm/gems/ruby-2.0.0-p481@global/gems/bundler-1.6.2/lib/bundler/setup.rb:17:in `' from /usr/local/rvm/rubies/ruby-2.0.0-p481/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require' from /usr/local/rvm/rubies/ruby-2.0.0-p481/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
おや、何かエラーが出た。mysql2 の gem がインストールできてないみたいだ。じゃ、直接インストールしてみるか。
[root@www2465uo takatoh]# gem install mysql2 -v '0.3.16' Building native extensions. This could take a while... ERROR: Error installing mysql2: ERROR: Failed to build gem native extension. /usr/local/rvm/rubies/ruby-2.0.0-p481/bin/ruby extconf.rb checking for ruby/thread.h... yes checking for rb_thread_call_without_gvl() in ruby/thread.h... yes checking for rb_thread_blocking_region()... yes checking for rb_wait_for_single_fd()... yes checking for rb_hash_dup()... yes checking for rb_intern3()... yes ----- Using mysql_config at /usr/bin/mysql_config ----- checking for mysql.h... no checking for mysql/mysql.h... no ----- mysql.h is missing. please check your installation of mysql and try again. ----- *** 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=/usr/local/rvm/rubies/ruby-2.0.0-p481/bin/ruby --with-mysql-dir --without-mysql-dir --with-mysql-include --without-mysql-include=${mysql-dir}/include --with-mysql-lib --without-mysql-lib=${mysql-dir}/ --with-mysql-config --without-mysql-config extconf failed, exit code 1 Gem files will remain installed in /usr/local/rvm/gems/ruby-2.0.0-p481/gems/mysql2-0.3.16 for inspection. Results logged to /usr/local/rvm/gems/ruby-2.0.0-p481/extensions/x86_64-linux/2.0.0/mysql2-0.3.16/gem_make.out
ダメのようだね。mysql.h が見つからない、といっているらしい。
[root@www2465uo takatoh]# yum install mysql-devel Loaded plugins: fastestmirror, security Loading mirror speeds from cached hostfile * base: ftp.tsukuba.wide.ad.jp * epel: ftp.riken.jp * extras: ftp.tsukuba.wide.ad.jp * updates: ftp.tsukuba.wide.ad.jp Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package mysql-devel.x86_64 0:5.1.73-3.el6_5 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: mysql-devel x86_64 5.1.73-3.el6_5 updates 129 k Transaction Summary ================================================================================ Install 1 Package(s) Total download size: 129 k Installed size: 388 k Is this ok [y/N]: y Downloading Packages: mysql-devel-5.1.73-3.el6_5.x86_64.rpm | 129 kB 00:00 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : mysql-devel-5.1.73-3.el6_5.x86_64 1/1 Verifying : mysql-devel-5.1.73-3.el6_5.x86_64 1/1 Installed: mysql-devel.x86_64 0:5.1.73-3.el6_5 Complete!
今度はどうだ。
[root@www2465uo takatoh]# cd myapp [root@www2465uo myapp]# bundle install Don't run Bundler as root. Bundler can ask for sudo if it is needed, and installing your bundle as root will break this application for all non-root users on this machine. Fetching gem metadata from https://rubygems.org/........... Resolving dependencies... Using rake 10.3.2 Using i18n 0.6.11 Using json 1.8.1 Using minitest 5.4.0 Using thread_safe 0.3.4 Using tzinfo 1.2.2 Using activesupport 4.1.4 Using builder 3.2.2 Using erubis 2.7.0 Using actionview 4.1.4 Using rack 1.5.2 Using rack-test 0.6.2 Using actionpack 4.1.4 Using mime-types 1.25.1 Using polyglot 0.3.5 Using treetop 1.4.15 Using mail 2.5.4 Using actionmailer 4.1.4 Using activemodel 4.1.4 Using arel 5.0.1.20140414130214 Using activerecord 4.1.4 Using bundler 1.6.2 Using coffee-script-source 1.7.1 Using execjs 2.2.1 Using coffee-script 2.3.0 Using thor 0.19.1 Using railties 4.1.4 Using coffee-rails 4.0.1 Using hike 1.2.3 Using multi_json 1.10.1 Using jbuilder 2.1.3 Using jquery-rails 3.1.1 Installing mysql2 0.3.16 Using tilt 1.4.1 Using sprockets 2.11.0 Using sprockets-rails 2.1.3 Using rails 4.1.4 Using rdoc 4.1.1 Using sass 3.2.19 Using sass-rails 4.0.3 Using sdoc 0.4.1 Using spring 1.1.3 Using turbolinks 2.2.2 Using uglifier 2.5.3 Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.
OK みたい。
じゃ、アプリを起動してみよう。おっと、その前に 3000 番ポートを開けておかなきゃ。/etc/sysconfig/iptables を編集して次の行を挿入。
-A RH-Firewall-1-INPUT -p tcp --dport 3000 -j ACCEPT
で、iptables を再起動。
[root@www2465uo myapp]# service iptables restart iptables: チェインをポリシー ACCEPT へ設定中filter [ OK ] iptables: ファイアウォールルールを消去中: [ OK ] iptables: モジュールを取り外し中: [ OK ] iptables: ファイアウォールルールを適用中: [ OK ]
よし、アプリを起動してブラウザからアクセスしてみる。
あー、MySQL の方を設定してないからか。
MySQLにデータベースを作る
myapp というデータベースを作って、myapp@localhost からアクセスできるようにする。
[root@www2465uo myapp]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 441 Server version: 5.1.73 Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> CREATE USER myapp@localhost IDENTIFIED BY 'myapp'; Query OK, 0 rows affected (0.00 sec) mysql> CREATE DATABASE myapp_development; Query OK, 1 row affected (0.00 sec) mysql> GRANT ALL ON myapp_development.* TO myapp; Query OK, 0 rows affected (0.00 sec) mysql> EXIT; Bye
Railsのデータベースに関する設定
config/database.yml を編集。
# MySQL. Versions 5.0+ are recommended. # # Install the MYSQL driver # gem install mysql2 # # Ensure the MySQL gem is defined in your Gemfile # gem 'mysql2' # # And be sure to use new-style password hashing: # http://dev.mysql.com/doc/refman/5.0/en/old-client.html # default: &default adapter: mysql2 encoding: utf8 pool: 5 username: myapp password: myapp socket: /var/lib/mysql/mysql.sock development: <<: *default database: myapp_development (後略)
Railsの起動と確認
[root@www2465uo myapp]# rake db:setup myapp_development already exists Mysql2::Error: Access denied for user 'myapp'@'localhost' to database 'myapp_test': CREATE DATABASE `myapp_test` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci` /usr/local/rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:303:in `query' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:303:in `block in execute' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract_adapter.rb:373:in `block in log' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/activesupport-4.1.4/lib/active_support/notifications/instrumenter.rb:20:in `instrument' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract_adapter.rb:367:in `log' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:303:in `execute' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.1.4/lib/active_record/connection_adapters/mysql2_adapter.rb:228:in `execute' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:371:in `create_database' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.1.4/lib/active_record/tasks/mysql_database_tasks.rb:16:in `create' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.1.4/lib/active_record/tasks/database_tasks.rb:88:in `create' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.1.4/lib/active_record/tasks/database_tasks.rb:102:in `block in create_current' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.1.4/lib/active_record/tasks/database_tasks.rb:209:in `block in each_current_configuration' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.1.4/lib/active_record/tasks/database_tasks.rb:208:in `each' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.1.4/lib/active_record/tasks/database_tasks.rb:208:in `each_current_configuration' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.1.4/lib/active_record/tasks/database_tasks.rb:101:in `create_current' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.1.4/lib/active_record/railties/databases.rake:17:in `block (2 levels) in ' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/rake-10.3.2/lib/rake/task.rb:240:in `call' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/rake-10.3.2/lib/rake/task.rb:240:in `block in execute' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/rake-10.3.2/lib/rake/task.rb:235:in `each' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/rake-10.3.2/lib/rake/task.rb:235:in `execute' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/rake-10.3.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain' /usr/local/rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/rake-10.3.2/lib/rake/task.rb:172:in `invoke_with_call_chain' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/rake-10.3.2/lib/rake/task.rb:201:in `block in invoke_prerequisites' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/rake-10.3.2/lib/rake/task.rb:199:in `each' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/rake-10.3.2/lib/rake/task.rb:199:in `invoke_prerequisites' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/rake-10.3.2/lib/rake/task.rb:178:in `block in invoke_with_call_chain' /usr/local/rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/rake-10.3.2/lib/rake/task.rb:172:in `invoke_with_call_chain' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/rake-10.3.2/lib/rake/task.rb:201:in `block in invoke_prerequisites' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/rake-10.3.2/lib/rake/task.rb:199:in `each' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/rake-10.3.2/lib/rake/task.rb:199:in `invoke_prerequisites' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/rake-10.3.2/lib/rake/task.rb:178:in `block in invoke_with_call_chain' /usr/local/rvm/rubies/ruby-2.0.0-p481/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/rake-10.3.2/lib/rake/task.rb:172:in `invoke_with_call_chain' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/rake-10.3.2/lib/rake/task.rb:165:in `invoke' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/rake-10.3.2/lib/rake/application.rb:150:in `invoke_task' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/rake-10.3.2/lib/rake/application.rb:106:in `block (2 levels) in top_level' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/rake-10.3.2/lib/rake/application.rb:106:in `each' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/rake-10.3.2/lib/rake/application.rb:106:in `block in top_level' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/rake-10.3.2/lib/rake/application.rb:115:in `run_with_threads' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/rake-10.3.2/lib/rake/application.rb:100:in `top_level' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/rake-10.3.2/lib/rake/application.rb:78:in `block in run' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/rake-10.3.2/lib/rake/application.rb:176:in `standard_exception_handling' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/rake-10.3.2/lib/rake/application.rb:75:in `run' /usr/local/rvm/gems/ruby-2.0.0-p481/gems/rake-10.3.2/bin/rake:33:in `' /usr/local/rvm/gems/ruby-2.0.0-p481/bin/rake:23:in `load' /usr/local/rvm/gems/ruby-2.0.0-p481/bin/rake:23:in `
なんかいろいろメッセージが出てるけど、見ないことにする。
で、ブラウザからアクセスすると:
今度はOK。