tensorflow for ruby
Ruby C C++ Other




This repository contains a Ruby API for utilizing TensorFlow.

Linux CPU Linux GPU PIP Mac OS CPU
Build Status Not Configured Not Configured

Code Climate Join the chat at https://gitter.im/tensorflowrb/Lobby Inline docs


Everything is at RubyDoc. You can also generate docs by bundle exec rake doc.

Blog Posts

  1. Introductory blog post
  2. Developers blog post
  3. Image Recognition Tutorial



It's easiest to get started using the prebuilt Docker container.


docker run --rm -it nethsix/ruby-tensorflow-ubuntu:0.0.1 /bin/bash


cd /repos/tensorflow.rb/
bundle exec rspec

Image Classification Tutorial:

cd /repos/tensorflow.rb/image/

For more details about all the fun machine-learning stuff already pre-installed, see: https://hub.docker.com/r/nethsix/ruby-tensorflow-ubuntu/

Outside of Docker

Alternatively, you can install outside of a Docker container by following the following steps.

Explicit dependencies

Implicit dependencies (No Action Required)


All the dependencies mentioned above must be installed in your system before you proceed further.

Clone and Install TensorFlow

This package depends on the TensorFlow shared libraries, in order to compile these libraries do the following:

git clone --recurse-submodules https://github.com/tensorflow/tensorflow
cd tensorflow

This command clones the repository and a few sub modules. After this you should do:

bazel build -c opt //tensorflow:libtensorflow.so

This command takes in the order of 10-15 minutes to run and creates a shared library. When finished, copy the newly generated libtensorflow.so shared library:

# Linux
sudo cp bazel-bin/tensorflow/libtensorflow.so /usr/lib/

sudo cp bazel-bin/tensorflow/libtensorflow.so /usr/local/lib
export LIBRARY_PATH=$PATH:/usr/local/lib (may be required)

Install tensorflow.rb

Clone and install this Ruby API:

git clone https://github.com/somaticio/tensorflow.rb.git
cd tensorflow.rb/ext/sciruby/tensorflow_c
ruby extconf.rb
make install # Creates ../lib/ruby/site_ruby/X.X.X/<arch>/tf/Tensorflow.bundle (.so Linux)
cd ./../../..
bundle exec rake install

The last command is for installing the gem.

Run tests and verify install

bundle exec rake spec

This command is to run the tests.

Install Script

I have also made a make shift install script in tools directory. You are free to use it, but it still needs some work and its best if you follow the installation procedure above or use docker. You are welcome to make improvements to the script.


Copyright (c) 2016, Arafat Dad Khan. somatic

All rights reserved.