The gem install ffi fails due to the gem trying to build a bundled copy of libffi, which does not work due to assumptions that /usr/bin/env exists and too old libffi copy.
However, you can build the ffi gem against the system libffi – install the packages:
apt install clang make ruby-dev libffi-dev
After that the ffi gem should build:
gem install ffi