Ruby on Rails on Android

I have fooled around with Ruboto but I don’t want to build native Android apps. I just want to start an irb session without 3 apps.

Here comes Termux. This is its official description:

Termux is an Android terminal emulator and Linux environment app that works directly with no rooting or setup required. A minimal base system is installed automatically - additional packages are available using the APT package manager.

The definition of a gold mine if you ask me. And yeah, they have a lot of packages, including the latest stable version of Ruby. Go and see for yourself on their github repository.

Get it from Google Play, update the packages list and install Ruby in one line.

$ apt update
$ apt install ruby
$ ruby -v
ruby 2.4.0p0 (2016-12-24 revision 57164) [aarch64-linux-android] # or something like this

Formidable! Now let’s put it to the test.

irb> puts "Hello World!"
Hello World!
=> nil
$ mkdir -p projects/ruby
$ echo 'puts "hello world!"' > projects/ruby/hello.rb
$ ruby projects/ruby/hello.rb
hello world!

Isn’t that wonderful?

I have a OnePlus 2 and 2 GiB of RAM are free most of the time. That’s much more than you can get from Heroku’s free dyno. Let’s see if we can run a Rails app on Android.

First step: bundler

$ gem install bundler

Now for the painful part, installing nokogiri’s dependencies.

We need something to build our native extensions.

$ apt install clang make

And these should do the trick:

$ apt install ruby-dev pkg-config libxml2 libxml2-dev libxslt libxslt-dev
$ gem install pkg-config

Say a prayer and hit return after the following line.

$ gem install nokogiri -- --use-system-libraries

Good. I spent quite some time gathering all of them (more than I care to admit as a matter of fact).

We will need a database for our app. Rails comes by default with a sqlite database and it carries along some dependencies that are not satisfied by default on our android device.

$ apt install libffi libffi-dev
$ apt install libsqlite libsqlite-dev

And something for our asset pipeline:

$ apt-install nodejs

Our reward:

$ gem install rails -v 5.2
$ cd projects/ruby
$ rails new demo
$ cd demo

We need to add a couple of gems for timezones. Open Gemfile with vi and add(be carefull with their order and platform options if any):

gem 'tzinfo'
gem 'tzinfo-data'

The moment of truth:

$ bundle install
$ rails server

It's alive!

You can find a working demo app on my GitHub profile.

Rails main screen Rails main screen logs
Scaffold resource Scaffold resource logs

Rails console

update 14.01.2018: Updated Rails version and added demo app and pictures.

Categories:

Updated: