Installing and running Ruby on Rails 7 on Android

Installing and running Ruby on Rails 7 on Android

Why?

A while ago (Black Friday last year actually), I decided to buy myself a tablet. I went down with Lenovo P11 Pro. It was very affordable tablet with decent characteristics. But the one thing that really sold me was the magnetic keyboard that when attached activates so called "production mode", and Lenovo Precision Pen 2 which works flawlesly as a pointing device as well as a normal pen with note taking apps such as Microsoft OneNote.

No alt text provided for this image
Image courtesy of Google

Now, as you might know, as a software engineers, we always tend to do things with our gadgets that would never come to mind to the average user.


The mission - how to get this actually working?

Having this thought in mind - my next goal was very simple. How to turn Lenovo Tablet into an actual mini workstation. Quick Google search for Ruby on Android gave me a lot of dead links and some projects, such as Ruboto. Also SL4A (Scripting layer for Android) is way too old to be usable.

Keep in mind that I also didn't want to go through the hassle of rooting my device, and I was looking for a solution that would work either in Android user mode, or in a sort of shim. I even thought of using some Android Linux emulator, such as Andronix but in that case that means that I have to install a whole distribution and use a VNC in order to work on it.

Upon some more research, I came across an excellent terminal emulator - Termux

Enter the matrix

Go to your App store (Play Store, AppGallery, F-Droid, etc) and search for Termux.

Once you download it, install it, and give it right permissions, you'll be presented with this

No alt text provided for this image

You can see that it already gives you and option to use pkg. Alternatively, you can use apt.

Prerequisites

Before you install Ruby on Rails, you'll need to use a whole bunch of libraries, but let's start with the basics.

pkg update && pkg upgrade\
pkg install ruby build-essential git vim        

Make sure your Ruby works. Type

ruby --version        

It should give you something like the following:

No alt text provided for this image

Great. Now what?

I was pretty much following the steps on how to install the Rails on Ubuntu.

So, the step one:

pkg install ruby-dev pkg-config libxml2 libxml2-dev libxslt libxslt-dev        

And we hit a wall:

No alt text provided for this image

Great - now what? Well, upon some more searching, I found out that Termux is not shipping -dev packages anymore, but basically everything is packed in the main packages. Refer to https://wiki.termux.com/wiki/No_more_-dev_packages

So, I ran the following:

pkg install pkg-config libxml2 libxslt        

Now, the time was for some Ruby stuff

Bundler and pkg-config

gem install bundler && gem install pkg-config        

The first thing that comes to mind after this would be to go and install Rails, but not so fast.

pkg install libffi libsqlite gumbo-parser zlib libzlib liblzma nodejs        

All of those libraries are important, because without them we can't install Nokogiri, nor Rails itself.

Now, the next step is interesting, and it took me almost an hour to spot and fix this issue.

gem install nokogiri --platofm=ruby -- --use-system-libraries        


BOOM

current directory: /data/data/com.termux/files/usr/lib/ruby/gems/3.2.0/gems/nokogiri-1.13.3/ext/nokogir
/data/data/com.termux/files/usr/bin/ruby -I /data/data/com.termux/files/usr/lib/ruby/3.2.0 -r ./siteconf20220228-27822-xbzj3s.rb extconf.rb --use-system-libraries
checking for whether -std=c99 is accepted as CFLAGS... yes
checking for whether -Wno-declaration-after-statement is accepted as CFLAGS... yes
checking for whether -g is accepted as CFLAGS... yes
checking for whether -Winline is accepted as CFLAGS... yes
checking for whether -Wmissing-noreturn is accepted as CFLAGS... yes
Building nokogiri using system libraries.
checking for gzdopen() in -lz... yes
checking for xmlParseDoc() in -lxml2... no
checking for xmlParseDoc() in -llibxml2... no
checking for libxml-2.0 using `pkg_config`... yes: ["-I/data/data/com.termux/files/usr/include/libxml2 ", "-L/data/data/com.termux/files/usr/lib", "-lxml2"]
checking for xmlParseDoc() in -lxml2... yes
checking for xsltParseStylesheetDoc() in -lxslt... yes
checking for exsltFuncRegister() in -lexslt... yes
Using mini_portile version 2.8.0
---------- IMPORTANT NOTICE ----------
Building Nokogiri with a packaged version of libgumbo-1.0.0-nokogiri.
Configuration options: --enable-shared --disable-static
[]
Copying gumbo-parser files into tmp/aarch64-unknown-linux-android/ports/libgumbo/1.0.0-nokogiri/gumbo-parser...
Running 'compile' for libgumbo 1.0.0-nokogiri... ERROR, review '/data/data/com.termux/files/usr/lib/ruby/gems/3.2.0/gems/nokogiri-1.13.3/ext/nokogiri/tmp/aarch64-unknown-linux-android/ports/libgumbo/1.0.0-nokogiri/compile.log' to see what happened. Last lines are:
========================================================================
aarch64-linux-android-clang -fPIC -g -std=c99 -Wall   -c -o foreign_attrs.o foreign_attrs.c
aarch64-linux-android-clang -fPIC -g -std=c99 -Wall   -c -o parser.o parser.c
parser.c:540:10: warning: cast to smaller integer type 'GumboInsertionMode' from 'void *' [-Wvoid-pointer-to-enum-cast]
  return (GumboInsertionMode) modes->data[(modes->length - 1)];
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 warning generated.
aarch64-linux-android-clang -fPIC -g -std=c99 -Wall   -c -o string_buffer.o string_buffer.c
aarch64-linux-android-clang -fPIC -g -std=c99 -Wall   -c -o string_piece.o string_piece.c
aarch64-linux-android-clang -fPIC -g -std=c99 -Wall   -c -o svg_attrs.o svg_attrs.c
aarch64-linux-android-clang -fPIC -g -std=c99 -Wall   -c -o svg_tags.o svg_tags.c
aarch64-linux-android-clang -fPIC -g -std=c99 -Wall   -c -o tag.o tag.c
aarch64-linux-android-clang -fPIC -g -std=c99 -Wall   -c -o tag_lookup.o tag_lookup.c
aarch64-linux-android-clang -fPIC -g -std=c99 -Wall   -c -o token_buffer.o token_buffer.c
aarch64-linux-android-clang -fPIC -g -std=c99 -Wall   -c -o tokenizer.o tokenizer.c
aarch64-linux-android-clang -fPIC -g -std=c99 -Wall   -c -o utf8.o utf8.c
aarch64-linux-android-clang -fPIC -g -std=c99 -Wall   -c -o util.o util.c
aarch64-linux-android-clang -fPIC -g -std=c99 -Wall   -c -o vector.o vector.c
ar rv libgumbo.a ascii.o attribute.o char_ref.o error.o foreign_attrs.o parser.o string_buffer.o string_piece.o svg_attrs.o svg_tags.o tag.o tag_lookup.o token_buffer.o tokenizer.o utf8.o util.o vector.o
make: ar: No such file or directory
make: *** [Makefile:30: libgumbo.a] Error 127
========================================================================
*** 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=/data/data/com.termux/files/usr/bin/$(RUBY_BASE_NAME)
    --help
    --clean
    --prevent-strip
    --enable-system-libraries
    --disable-system-libraries
    --use-system-libraries
    --enable-system-libraries
    --disable-system-libraries
    --use-system-libraries
    --with-zlib-dir
    --without-zlib-dir
    --with-zlib-include
    --without-zlib-include=${zlib-dir}/include
    --with-zlib-lib
    --without-zlib-lib=${zlib-dir}/lib
    --with-z-dir
    --without-z-dir
    --with-z-include
    --without-z-include=${z-dir}/include
    --with-z-lib
    --without-z-lib=${z-dir}/lib
    --with-zlib
    --without-zlib
    --with-xml2-dir
    --without-xml2-dir
    --with-xml2-include
    --without-xml2-include=${xml2-dir}/include
    --with-xml2-lib
    --without-xml2-lib=${xml2-dir}/lib
    --with-xml2lib
    --without-xml2lib
    --with-libxml2-dir
    --without-libxml2-dir
    --with-libxml2-include
    --without-libxml2-include=${libxml2-dir}/include
    --with-libxml2-lib
    --without-libxml2-lib=${libxml2-dir}/lib
    --with-libxml2lib
    --without-libxml2lib
    --with-libxml-2.0-dir
    --without-libxml-2.0-dir
    --with-libxml-2.0-include
    --without-libxml-2.0-include=${libxml-2.0-dir}/include
    --with-libxml-2.0-lib
    --without-libxml-2.0-lib=${libxml-2.0-dir}/lib
    --with-libxml-2.0-config
    --without-libxml-2.0-config
    --with-pkg-config
    --without-pkg-config
    --with-xml2lib
    --without-xml2lib
    --with-xslt-dir
    --without-xslt-dir
    --with-xslt-include
    --without-xslt-include=${xslt-dir}/include
    --with-xslt-lib
    --without-xslt-lib=${xslt-dir}/lib
    --with-xsltlib
    --without-xsltlib
    --with-exslt-dir
    --without-exslt-dir
    --with-exslt-include
    --without-exslt-include=${exslt-dir}/include
    --with-exslt-lib
    --without-exslt-lib=${exslt-dir}/lib
    --with-exsltlib
    --without-exsltlib
    --enable-cross-build
    --disable-cross-build
/data/data/com.termux/files/usr/lib/ruby/gems/3.2.0/gems/mini_portile2-2.8.0/lib/mini_portile2/mini_portile.rb:460:in `block in execute': Failed to complete compile task (RuntimeError)
    from /data/data/com.termux/files/usr/lib/ruby/gems/3.2.0/gems/mini_portile2-2.8.0/lib/mini_portile2/mini_portile.rb:426:in `chdir'
    from /data/data/com.termux/files/usr/lib/ruby/gems/3.2.0/gems/mini_portile2-2.8.0/lib/mini_portile2/mini_portile.rb:426:in `execute'
    from extconf.rb:968:in `compile'
    from /data/data/com.termux/files/usr/lib/ruby/gems/3.2.0/gems/mini_portile2-2.8.0/lib/mini_portile2/mini_portile.rb:187:in `cook'
    from extconf.rb:528:in `block (2 levels) in process_recipe'
    from extconf.rb:309:in `chdir'
    from extconf.rb:309:in `chdir_for_build'
    from extconf.rb:528:in `block in process_recipe'
    from <internal:kernel>:90:in `tap'
    from extconf.rb:427:in `process_recipe'
    from extconf.rb:927:in `<main>'

To see why this extension failed to compile, please check the mkmf.log which can be found here:

  /data/data/com.termux/files/usr/lib/ruby/gems/3.2.0/extensions/aarch64-linux-android/3.2.0/nokogiri-1.13.3/mkmf.log

extconf failed, exit code 1        

I was in shock. I had gumbo-parser intalled and all the other libraries were recognized properly. Then I noticed one thing:

make: ar: No such file or directory        

At that moment, I did a quick command which ar and it came empty.

AHA! - we were missing a binutils. After instaling binutils and running the command again, Nokogiri was finally installed.

pkg install binutils && gem install nokogiri --platform=ruby -- --use-system-libraries        

The very last thing was to create a project folder and install Rails

gem install rails && mkdir -p projects\
&& cd projects && rails new demoproject        

After the Rails is installed and your new project is generated, all you have to do is to edit Gemfile (I used Vim)

vim Gemfile        

and add those two lines

gem 'tzinfo
gem 'tzinfo-data''        

Use ESC key on your Termux and type

:wq        

Congratulations! You just learned how to exit Vim without restarting your device.

Next step

bundle install        

After the bundle is installed, you can test your server

bin/rails s        

If you did everything correctly, your Termux will show server running

No alt text provided for this image

And if you open your browser and navigate to https://localhost:3000 you should be able to see your Rails 7 start page.

I hope you enjoyed reading and have fun developing with Rails on Android.

要查看或添加评论,请登录

Antonio Nesic的更多文章

  • Lessons learned rewriting the old code

    Lessons learned rewriting the old code

    Like it or not, every professional developer sooner or later encounters an old codebase. You know, the one that has…

  • CRUD - AJAX - REST - what's the deal?

    CRUD - AJAX - REST - what's the deal?

    I am aware that this is probably the millionth article on this topic, but for some reason people still keep on asking…

    1 条评论

其他会员也浏览了