Ubuntu 24.04 Ruby on Rails 7をインストールする

環境
Ubuntu 24.04
ruby 3.2.3

1.必要なパッケージをインストールしておきます。
# apt -y install ruby-dev libmysqlclient-dev gcc make yarnpkg libxml2 libxml2-dev libxslt-dev libyaml-dev nodejs git

2.Rails 7 をインストールします。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# gem install bundler
# gem install nokogiri -- --use-system-libraries
# gem install rails -N --version='~> 7.0, < 8.0'
# rails -v
Rails 7.1.3.4
# gem install bundler # gem install nokogiri -- --use-system-libraries # gem install rails -N --version='~> 7.0, < 8.0' # rails -v Rails 7.1.3.4
# gem install bundler
# gem install nokogiri -- --use-system-libraries
# gem install rails -N --version='~> 7.0, < 8.0'
# rails -v
Rails 7.1.3.4

3.テストアプリケーション用の DB とユーザーを作成

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 58
Server version: 10.11.7-MariaDB-2ubuntu2 Ubuntu 24.04
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 arkgamedb@'localhost' identified by 'password';
Query OK, 0 rows affected (0.005 sec)
MariaDB [(none)]> grant all privileges on sample_app_development.* to arkgamedb@'localhost' identified by 'password';
Query OK, 0 rows affected (0.005 sec)
MariaDB [(none)]> exit
Bye
# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 58 Server version: 10.11.7-MariaDB-2ubuntu2 Ubuntu 24.04 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 arkgamedb@'localhost' identified by 'password'; Query OK, 0 rows affected (0.005 sec) MariaDB [(none)]> grant all privileges on sample_app_development.* to arkgamedb@'localhost' identified by 'password'; Query OK, 0 rows affected (0.005 sec) MariaDB [(none)]> exit Bye
# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 58
Server version: 10.11.7-MariaDB-2ubuntu2 Ubuntu 24.04

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 arkgamedb@'localhost' identified by 'password'; 
Query OK, 0 rows affected (0.005 sec)

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

MariaDB [(none)]> exit
Bye

mysql2をインストールする

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# gem install mysql2 -- --with-mysql-config=/usr/bin/mysql_config
# gem install mysql2 -- --with-mysql-config=/usr/bin/mysql_config
# gem install mysql2 -- --with-mysql-config=/usr/bin/mysql_config

# rails new SampleApp -d mysql
# cd SampleApp

database.ymlを設定します
# vi config/database.yml

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
default: &default
adapter: mysql2
encoding: utf8mb4
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: arkgamedb # MariaDB 接続ユーザー
password: password # MariaDB 接続パスワード
host: localhost
default: &default adapter: mysql2 encoding: utf8mb4 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> username: arkgamedb # MariaDB 接続ユーザー password: password # MariaDB 接続パスワード host: localhost
default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: arkgamedb     # MariaDB 接続ユーザー
  password: password        # MariaDB 接続パスワード
  host: localhost

5.rubyのプログラムを作成する
# vi config/application.rb

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
module SampleApp
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 7.1
# 13行目 : ホストを指定する
# 指定しない場合は [localhost] または IP アドレス宛のみアクセス可
# サブドメイン含めてドメイン名単位で許可する場合は以下のように指定
config.hosts << ".arkgame.com"
module SampleApp class Application < Rails::Application # Initialize configuration defaults for originally generated Rails version. config.load_defaults 7.1 # 13行目 : ホストを指定する # 指定しない場合は [localhost] または IP アドレス宛のみアクセス可 # サブドメイン含めてドメイン名単位で許可する場合は以下のように指定 config.hosts << ".arkgame.com"
module SampleApp
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 7.1
    # 13行目 : ホストを指定する
    # 指定しない場合は [localhost] または IP アドレス宛のみアクセス可
    # サブドメイン含めてドメイン名単位で許可する場合は以下のように指定
    config.hosts << ".arkgame.com"

4.テストアプリケーション作成
# rails generate scaffold testapp name:string title:string body:text
# rails db:migrate

5.railsサーバを起動する

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# rails server --binding=0.0.0.0
# rails server --binding=0.0.0.0
# rails server --binding=0.0.0.0

 

IT

Posted by arkgame