LoginSignup
0
0

More than 1 year has passed since last update.

Ruby on Rails をTermuxに導入

Posted at

やりたいこと

  • termuxにRuby on Railsを導入したい
  • ググるといくつか参考になる情報あるが、どれを試しても導入できない

環境

  • Android 12
  • 最小限でMySQLを使用したRailsアプリを作成します

構築手順

古いスマホにTermuxを入れてそこにRuby on Railsを導入する。

Termuxをインストール

※以下ページご参照ください。(「Termuxのインストール」まで)
https://qiita.com/tatuya911/items/840107699190451a4651

GooglePlay以外からアプリのインストールする場合は
「不明なアプリのインストールを許可」をしてください。
※自己責任でのインストールになります。

お決まりのコマンドを実行

apt upgrade途中に出てくる選択肢は Enter(N) でOK。

Termux
~ $ apt update
~ $ apt upgrade

必要パッケージをインストール

Termux
~ $ pkg install ruby clang make binutils pkg-config libxslt git mariadb

※以下ページを参考にさせていただきました
https://github.com/termux/termux-packages/discussions/8855

ここからはgemでインストールしていきます。

Termux
~ $ gem install nokogiri --platform=ruby -- --use-system-libraries

termuxでnokogiriをインストールするときは上のオプションがいるようです。
ここでrailsをインストールしていきます。

Termux
~ $ gem install rails

環境によるかもしれませんが、rails newするときに、以下コマンド実行するように言われます。
先にやっておきましょう。

Termux
~ $ gem update bundler

あたらしくRailsアプリケーションを作成します。
(termux_appの部分はご自由にどうぞ)

Termux
~ $ rails new ./termux_app -d mysql

このようなエラーが出ます。

Termux
rails  importmap:install
rails aborted!
LoadError: cannot load such file -- nokogiri/nokogiri
/data/data/com.termux/files/home/termux_app/config/application.rb:7:in `<main>'
/data/data/com.termux/files/home/termux_app/Rakefile:4:in `require_relative'
/data/data/com.termux/files/home/termux_app/Rakefile:4:in `<main>'
bin/rails:4:in `<main>'

エラー内容謎ですが、Gemfileを編集して回避していきます。

Gemfileの編集

以下の通りGemfileを編集します。

  • nokogiriのオプション追加
  • tzinfo-dataのオプション削除(コメントアウト)
Termux
~ $ nano Gemfile
+  gem "nokogiri", force_ruby_platform: true

- gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]
+ gem "tzinfo-data"
+ #, platforms: %i[ mingw mswin x64_mingw jruby ]

Gemfileを編集したら、Gemfileでインストールします。

Termux
~ $ bundle install

これで正常にRailsアプリケーションが作成されます。

Rails起動(失敗...)

まだまだです。。以下コマンドで起動をするとエラーが発生します。

Termux
~ $ rails s

MySQLのエラー回避

ここが一番難航しました。。
複数エラーが発生するので、一つずつ対処していきます。

まず、空ファイルの「mysqld.sock」を作成します。

Termux
~ $ touch /data/data/com.termux/files/usr/var/run/mysqld.sock

※以下サイトを参考にさせていただきました。
https://qiita.com/tkmd35/items/c26d1dffca4be0a4f017

次に、以下コマンドでmysqlを起動します。

Termux
~ $ mysqld_safe

termuxのユーザでmysqlに入り、rootユーザを追加します。

Termux
~ $ mysql -u $(whoami)
~ $ set password for 'root'@'localhost' = password('');

先程作成したrootでmysqlに入り、DBを作成します。

Termux
~ $ mysql -u root
~ $ rails db:create

Rails起動(成功!!)

以下コマンドで起動します。

Termux
~ $ rails s

最後に

恐らくですが時間とともにこのやり方できなくなるかもです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0