TensorFlow on FreeBSD

The beginning of the 3 day journey.
(The only issue on TensorFlow related to BSD, and of course it wasn't useful.)

The first thing any FreeBSD user does when looking for software that isn't officially supported is to look for an unofficial port. Of course there was nothing when I looked. The only relevant results were related to a GitHub issue that was closed because "the build system is to blame". Alright then, great start!

First, I apologize for not supplying a port archive myself. After reading the FreeBSD handbook for creating a port, it's too complex of a task for me right now. I've only been using FreeBSD for two weeks. I would also not like to waste anyone's time giving them a terrible port archive and mess up their system.

Instead I'll explain how you can do it yourself! It's surprisingly easy once you've put the time in investigating why certain problems happen...

I began by reading the standard TensorFlow Download and Setup instructions. Since I knew FreeBSD had some Linux compatibility layer, I tried loading the Python 64-bit pip package. Obviously that didn't work. I learned shortly after FreeBSD only supports 32-bit binary. Then I remember hearing from people that Mac is somewhat based off of FreeBSD...Following the Mac instructions, I'm met with an "incompatible binary" error, as expected.

At this point I needed to compile from source. I followed the Mac instructions again, because the ones for Linux talked about GPU support and a bunch of other things that would've only made this harder or downright not possible. The first wall I hit was compiling Bazel, Google's own build tool. The errors being output totally put me off from wanting to continue. Luckily after some digging there was a glimmer of hope.

When you follow that Bugzilla link, it leads you to the real bug report. At first I thought this was the end of the line because there was no attachment with the port. I emailed the author who responded fairly quickly, and after looking back, I realized the link was active and downloaded the port for Bazel 0.2.2b.

I recommend you use Clang 3.8 to build everything throughout the entire process. I had the least trouble with it, but 3.4 works too if you fiddle around a bit.

When Bazel is installed, you can move onto the real fun: compiling TensorFlow! Originally there was no intention on doing this write up so I didn't record where I had made changes - thank God for version control keeping track of things.

The Instructions

Forewarning: I am not liable for any damage to your system. There are commands that require root access and override some things. You have been warned!

Make the following changes in these files:

Around line 41:

-#if defined(__clang__) && (!defined(SWIG))
-#define THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
 #define THREAD_ANNOTATION_ATTRIBUTE__(x)  // no-op

Around line 132:

-#if !defined(__APPLE__)
-    case ENONET:  // Machine is not on the network

/tensorflow/stream_executor/rng.cc b/tensorflow/stream_executor/rng.cc
Around line 45:

-#if defined(__APPLE__)
 const int RngSupport::kMinSeedBytes;
 const int RngSupport::kMaxSeedBytes;

Next you'll have to run ln -s /usr/bin/libc.so /usr/lib/libdl.so which provides functions that the build process needs.

To finish it off remove cc with rm /usr/bin/cc and replace it with the following script:

/usr/bin/clang "$@" -lm

Make sure it is executable.

This is necessary because there is a package that doesn't link the math library. The build process for TensorFlow looks really huge to me, so this is a shortcut. You will get warnings during the compilation process that -lm is unused a lot, but it doesn't make a difference.

Then you just follow the standard process they detail on their page. For location of Python, you MUST use Python 2. I've tried Python 3 and it doesn't work for whatever reason. Because I was trying a lot of different things at once, it could definitely work for you. Do not run the instruction for GPU support. The name of the output package in /tmp/tensorflow_pkg/ will be different than what's in the picture.

Another warning: If you do not have 8GB of RAM your computer will grind to a halt. Be careful if you have any important work open. FreeBSD should kill the process after a period of time though.

Start it up and woohoo! You're running TensorFlow on FreeBSD:

Frickin' beautiful.

I really encourage anyone to submit a proper pull request for this. I don't know enough about bazel (and don't want to) to submit one myself and honestly I'm tired of messing around. Once that's done, a FreeBSD guru can make a port archive in no time.


Here is another picture of it really in action, eating all my cores, training a convolutional neural network:

Now only I really knew what was going on here...


  1. Nice,
    Will definitely try to compile it on my fbsd box, as the initial/basic try-outs didn't work and had no time to get deeper into the problems.


Post a Comment

Popular Posts