CentOS Stream 10

Ruby on Rails 8 : インストール2025/08/07

 

Ruby フレームワーク Ruby on Rails 8 をインストールします。

[1]

こちらを参考に Ruby をインストールしておきます

[2] その他必要なパッケージをインストールしておきます。
# EPEL, CRB からインストール

[root@dlp ~]#
dnf --enablerepo=crb,epel -y install ruby-devel rpm-build make gcc gcc-c++ libxml2 libxml2-devel libyaml libyaml-devel mariadb-devel zlib-devel libxslt-devel nodejs git yarnpkg
[3] Rails 8 をインストールします。
[root@dlp ~]#
gem install bundler

[root@dlp ~]#
gem install nokogiri -- --use-system-libraries

[root@dlp ~]#
gem install rails --version="~>8.0"

[root@dlp ~]#
rails -v

Rails 8.0.2
[4] テストアプリケーションを作成して動作確認します。
DB を使用するため、事前に、こちらを参考に MariaDB サーバーをインストールしておきます
また、Firewalld 稼働中、且つ、他ホストからもテストアプリケーションにアクセスする場合は、ポート 3000 の許可も必要です。
# テストアプリケーション用の DB とユーザーを作成

[root@dlp ~]#
mysql

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.11.11-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database sample_app_test; 
Query OK, 1 row affected (0.000 sec)

MariaDB [(none)]> create database sample_app_development; 
Query OK, 1 row affected (0.000 sec)

MariaDB [(none)]> grant all privileges on sample_app_test.* to serverworld@'localhost' identified by 'password'; 
Query OK, 0 rows affected (0.005 sec)

MariaDB [(none)]> grant all privileges on sample_app_development.* to serverworld@'localhost' identified by 'password'; 
Query OK, 0 rows affected (0.005 sec)

MariaDB [(none)]> exit
Bye

[root@dlp ~]#
gem install mysql2 -- --with-mysql-config=/usr/bin/mysql_config

[root@dlp ~]#
rails new SampleApp -d mysql

[root@dlp ~]#
cd SampleApp

[root@dlp SampleApp]#
vi config/database.yml
default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: serverworld     # MariaDB 接続ユーザー
  password: password        # MariaDB 接続パスワード
  host: localhost

[root@dlp SampleApp]#
vi config/application.rb
module SampleApp
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 7.0
    # 13行目 : ホストを指定する
    # 指定しない場合は [localhost] または IP アドレス宛のみアクセス可
    # サブドメイン含めてドメイン名単位で許可する場合は以下のように指定
    config.hosts << ".srv.world" 

# テストアプリケーション作成

[root@dlp SampleApp]#
rails generate scaffold testapp name:string title:string body:text

[root@dlp SampleApp]#
rails db:migrate

== 20250807015233 CreateTestapps: migrating ===================================
-- create_table(:testapps)
   -> 0.0146s
== 20250807015233 CreateTestapps: migrated (0.0147s) ==========================

[root@dlp SampleApp]#
rails server --binding=0.0.0.0

=> Booting Puma
=> Rails 8.0.2 application starting in development
=> Run `bin/rails server --help` for more startup options
Puma starting in single mode...
* Puma version: 6.6.1 ("Return to Forever")
* Ruby version: ruby 3.3.8 (2025-04-09 revision b200bad6cd) +YJIT [x86_64-linux]
*  Min threads: 3
*  Max threads: 3
*  Environment: development
*          PID: 5775
* Listening on http://0.0.0.0:3000
Use Ctrl-C to stop
[5] 任意のクライアントコンピューターで Web ブラウザーを起動し、[http://(ホスト名 または IP アドレス):3000/] にアクセスして、以下のようなページが表示されれば OK です。
  [http://(ホスト名 または IP アドレス):3000/testapps/] にアクセスすると、作成したテストアプリケーションの画面が利用できます。
関連コンテンツ