HOWTO: Install Tensorflow on FreeBSD

I had quite a pain trying to compile Tensorflow so here is a guide including patches.

First off, you need clang 3.8 or later (That means you need FreeBSD 11.0-RELEASE or later)

Guide last updated 2017-06-05 to work with Tensorflow 1.1.0

  • Let's start by switching your pkg repo to latest instead of quarterly:

    Open up /etc/pkg/FreeBSD.conf and change URL to
    Code:
    url: "pkg+http://pkg.FreeBSD.org/${ABI}/latest",

    Run pkg update -f && pkg upgrade to get you up to date.

  • Now, lets install all the dependencies:

    pkg install bazel protobuf re2 python27 py27-pip py27-protobuf py27-numpy py27-wheel py27-mock py27-werkzeug git

  • Download Tensorflow
    cd ~
    git clone https://github.com/tensorflow/tensorflow.git
    cd tensorflow
    git checkout v1.1.0

  • We can now run configure:

    ./configure

  • Not quite out of the woods yet, now we need to apply some changes.

    We have to symlink our python2.7 binary to python
    cd /usr/local/bin
    ln -s python2.7 python

    Now let's start a build run so bazel extracts all dependencies:
    cd ~/tensorflow
    bazel build --jobs 8 -c opt //tensorflow/tools/pip_package:build_pip_package --verbose_failures

    Note that --jobs 8 is the number of cores my CPU has, please adjust this to match your server, do note that they will all be utilized 100% for a few moments during the build.

    At this point you will get a error, thats okay, bazel has now extracted all the dependencies so we can apply the needed changes.

  • In your tensorflow folder you now have a bazel-tensorflow folder (that is if you have followed this guide, if you named your git clone target to something else, the folder will be named bazel-<yournameofthefolder>

    Now let's make the needed changes
    1. Open bazel-tensorflow/external/grpc/include/grpc/impl/codegen/port_platform.h and delete all rows stating
      Code:
      #define GPR_HAVE_IP_PKTINFO 1
      (there are 5 of those at the time of writing this)

    2. Apply all protobuf patches from ports:
      cd bazel-tensorflow/external/protobuf

      fetch "https://svnweb.freebsd.org/ports/head/devel/protobuf/files/patch-src-google-protobuf-compiler-plugin.pb.cc?revision=432762&view=co&pathrev=439752"
      fetch "https://svnweb.freebsd.org/ports/head/devel/protobuf/files/patch-src-google-protobuf-compiler-plugin.pb.h?revision=432762&view=co&pathrev=439752"
      fetch "https://svnweb.freebsd.org/ports/head/devel/protobuf/files/patch-src-google-protobuf-testing-zcgunzip.cc?revision=428734&view=co&pathrev=439752"
      fetch "https://svnweb.freebsd.org/ports/head/devel/protobuf/files/patch-src-google-protobuf-testing-zcgzip.cc?revision=428734&view=co&pathrev=439752"
      fetch "https://svnweb.freebsd.org/ports/head/devel/protobuf/files/patch-src-google-protobuf-stubs-atomicops.h?revision=428734&view=co&pathrev=439752"

      patch < patch-src-google-protobuf-compiler-plugin.pb.cc\?revision\=432762\&view\=co\&pathrev\=439752
      patch < patch-src-google-protobuf-compiler-plugin.pb.h\?revision\=432762\&view\=co\&pathrev\=439752
      patch < patch-src-google-protobuf-stubs-atomicops.h\?revision\=428734\&view\=co\&pathrev\=439752
      patch < patch-src-google-protobuf-testing-zcgunzip.cc\?revision\=428734\&view\=co\&pathrev\=439752
      patch < patch-src-google-protobuf-testing-zcgzip.cc\?revision\=428734\&view\=co\&pathrev\=439752
  • We can now restart the build and this time it will finish
    cd ~/tensorflow
    bazel build --jobs 8 -c opt //tensorflow/tools/pip_package:build_pip_package --verbose_failures

  • Now let's build the pip package
    bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

  • Last part, su to root and install the pip package
    su
    pip install /tmp/tensorflow_pkg/tensorflow-1.1.0-cp27-cp27mu-freebsd_11_0_RELEASE_p9_amd64.whl

    Note that the filename is based on what version of FreeBSD you are running so match the filename to the one thats present in your /tmp/tensorflow_pkg/ folder.
Voila, you got tensorflow!
 
Last edited:
Finally I though tensorflow will be running on my beloved fbsd box, sadly this guide didn't work out for me, as few other I've found on the internet.
Bazel from pkg did segfault, reinstalled it from ports which did help.
FreeBSD 10.3-STABLE FreeBSD 10.3-STABLE #0 r302080: Wed Jun 22 16:12:56 EEST 2016 amd64
Some info on last attempt:

ERROR: /root/.cache/bazel/_bazel_rr/6bc698f1e95b24c9306a89d411788848/external/protobuf/BUILD:248:1: C++ compilation of rule '@protobuf//:protoc_lib' failed: clang fail
ed: error executing command
(cd /root/.cache/bazel/_bazel_rr/6bc698f1e95b24c9306a89d411788848/execroot/tensorflow && \
exec env - \
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin \
/usr/bin/clang -U_FORTIFY_SOURCE '-D_FORTIFY_SOURCE=1' -fstack-protector -Wall -fno-omit-frame-pointer -g0 -O2 -DNDEBUG -ffunction-sections -fdata-sections '-std=c++0
x' -MD -MF bazel-out/local_freebsd-opt/bin/external/protobuf/_objs/protoc_lib/external/protobuf/src/google/protobuf/compiler/code_generator.pic.d '-frandom-seed=bazel-o
ut/local_freebsd-opt/bin/external/protobuf/_objs/protoc_lib/external/protobuf/src/google/protobuf/compiler/code_generator.pic.o' -fPIC -iquote external/protobuf -iquote
bazel-out/local_freebsd-opt/genfiles/external/protobuf -iquote external/bazel_tools -iquote bazel-out/local_freebsd-opt/genfiles/external/bazel_tools -isystem external
/protobuf/src -isystem bazel-out/local_freebsd-opt/genfiles/external/protobuf/src -isystem external/bazel_tools/tools/cpp/gcc3 -DHAVE_PTHREAD -Wall -Wwrite-strings -Wov
erloaded-virtual -Wno-sign-compare -Wno-unused-function -no-canonical-prefixes -Wno-builtin-macro-redefined '-D__DATE__="redacted"' '-D__TIMESTAMP__="redacted"' '-D__TI
ME__="redacted"' -c external/protobuf/src/google/protobuf/compiler/code_generator.cc -o bazel-out/local_freebsd-opt/bin/external/protobuf/_objs/protoc_lib/external/prot
obuf/src/google/protobuf/compiler/code_generator.pic.o): com.google.devtools.build.lib.shell.BadExitStatusException: Process exited with status 1.
In file included from external/protobuf/src/google/protobuf/compiler/code_generator.cc:37:
external/protobuf/src/google/protobuf/compiler/plugin.pb.h:238:29: error: expected member name or ';' after declaration specifiers
::google::protobuf::int32 major() const;
~~~~~~~~~~~~~~~~~~~~~~~~~ ^
/usr/include/sys/types.h:360:20: note: expanded from macro 'major'
#define major(x) ((int)(((u_int)(x) >> 8)&0xff)) /* major number */
^
....

external/protobuf/src/google/protobuf/compiler/plugin.pb.h:723:43: note: to match this '('
/usr/include/sys/types.h:361:19: note: expanded from macro 'minor'
#define minor(x) ((int)((x)&0xffff00ff)) /* minor number */
^
15 errors generated.
Target //tensorflow/tools/pip_package:build_pip_package failed to build
INFO: Elapsed time: 36.020s, Critical Path: 32.33s
 
You need FreeBSD 11.0-RELEASE or later for it to compile and work properly.

The problem is you need clang 3.8 or later (you have clang 3.4 in fbsd 10.3)

I've updated the howto with that particular info.
 
Hello blodan,

I did step by step everything exactly as you wrote. Unfortunately I got the same error as nimmen has. I'm running FreeBSD 11-STABLE r313782M with Clang 3.9.1. Could you please help to solve this issue?

Thank you!
 
I have solved the issue.

I don't know why, but bazel tried to build bundled version of protobuf. I've applied patches from /usr/ports/devel/protobuf/files/* to the version of protobuf which it tried to build.
 
Hello, small update to the "nimmen" error.

This is not related to protobuf, thats because of macro
Code:
#define major(x)  ((int)(((u_int)(x) >> 8)&0xff)) /* major number */
and next candidate
Code:
#define minor(x) ((int)((x)&0xffff00ff))> /* minor number */
from file /usr/include/sys/types.h impact on function name in class
Code:
Version::major()
. Thats c/c++ spec.

here is sample for you:
cat 1.cpp
Code:
//no includes
//priv macro original /usr/include/sys/types.h
#define major(x) (int)

class myClass
{
  public:
    char major();
}

char myClass::major()
{
  return 0;
}

int main()
{
  myClass a;
  return 0;
}

and clang compilation result:
clang 1.cpp -o 1
Code:
1.cpp:8:10: error: expected member name or ';' after declaration specifiers
    char major();
    ~~~~ ^
1.cpp:3:19: note: expanded from macro 'major'
#define major(x) (int)
                  ^
1.cpp:8:10: error: expected ')'
1.cpp:3:19: note: expanded from macro 'major'
#define major(x) (int)
                  ^
1.cpp:8:10: note: to match this '('
1.cpp:3:18: note: expanded from macro 'major'
#define major(x) (int)
                 ^
1.cpp:11:10: error: expected unqualified-id
myClass::major()
         ^
3 errors generated.

You have many ways to correct this error in code:
1) lazy way , may impact on later behavior in case if header external/protobuf/src/google/protobuf/compiler/plugin.pb.h is used or macros defined in /usr/include/sys/types.h
Code:
#ifdef major
#undef major
#endif
2) my approach, I've changed all methods name in file external/protobuf/src/google/protobuf/compiler/plugin.pb.h and external/protobuf/src/google/protobuf/compiler/plugin.pb.cc
Code:
major()
to
Code:
majora()
and
Code:
minor()
to
Code:
minora()

Hope it helps.
 
I had quite a pain trying to compile Tensorflow so here is a guide including patches.
It is equally painful and brittle compiling it on the Red Hat so many of my colleagues are just using

http://singularity.lbl.gov/

to run Ubuntu/Tensorflow docker images in the userland on the top of Red Hat. Think of Singularity as DragonFly BSD vkernel.

However I am puzzled about one thing. What is the purpose of running Tensorflow on FreeBSD? I was under impression that NVidia is not releasing binary blob CUDA GPU drivers for FreeBSD. Does NVidia support GPU CUDA computing on FreeBSD? If the answer is NO than compiling Tensorflow on FreeBSD is an exercise in masochism.
 
Is it possible to build a python 3 package?

Yes it is, just install the py36- packages intead of py27-, I'm not sure if you need to roll your own poudriere repo for that to work though.

I'm running it on python 3.6 myself and it works fine, however I roll my own poudriere repo with DEFAULT_VERSIONS= python=3.6
 
However I am puzzled about one thing. What is the purpose of running Tensorflow on FreeBSD? I was under impression that NVidia is not releasing binary blob CUDA GPU drivers for FreeBSD. Does NVidia support GPU CUDA computing on FreeBSD? If the answer is NO than compiling Tensorflow on FreeBSD is an exercise in masochism.

Not everyone has that big of a workload you need to run it on a GPU, we are running Tensorflow in CPU mode and by compiling it on FreeBSD I can use our current infrastructure for it.

If we reach such a huge load we need to start using GPU:s I'm totally going ubuntu on that cluster as thats the os tensorflow supports officially
 
I am getting errors when using it after a system update:
Code:
[libprotobuf ERROR external/protobuf/src/google/protobuf/descriptor_database.cc:57] File already exists in database: tensorflow/core/util/test_log.proto
[libprotobuf FATAL external/protobuf/src/google/protobuf/descriptor.cc:1275] CHECK failed: generated_database_->Add(encoded_file_descriptor, size):
Abort trap (core dumped)
 
JFYI, I submit a new port PR for tensorflow:
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=219609

It can build successfully for 10.x+ & i386 & amd64 & py27 & py3x
There are 10 and 11 binary pkg for tensorflow-1.2.0 here: https://github.com/amutu/tf-freebsd-pkg

I installed the py27 package on 11.0 system. Unfortunately, it doesnt seem to work.
Code:
$ python2
Python 2.7.13 (default, Jun  8 2017, 01:15:00)
[GCC 4.2.1 Compatible FreeBSD Clang 3.8.0 (tags/RELEASE_380/final 262564)] on freebsd11
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/tensorflow/__init__.py", line 24, in <module>
    from tensorflow.python import *
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 63, in <module>
    from tensorflow.python.framework.framework_lib import *
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/framework_lib.py", line 100, in <module>
    from tensorflow.python.framework.subscribe import subscribe
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/subscribe.py", line 26, in <module>
    from tensorflow.python.ops import variables
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 26, in <module>
    from tensorflow.python.ops import control_flow_ops
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 70, in <module>
    from tensorflow.python.ops import tensor_array_ops
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/tensor_array_ops.py", line 33, in <module>
    from tensorflow.python.util import tf_should_use
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/util/tf_should_use.py", line 28, in <module>
    from backports import weakref  # pylint: disable=g-bad-import-order
ImportError: cannot import name weakref
>>> import tensorflow as tf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/tensorflow/__init__.py", line 24, in <module>
    from tensorflow.python import *
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
ImportError: cannot import name pywrap_tensorflow
 
  • Thanks
Reactions: Jov
I installed the py27 package on 11.0 system. Unfortunately, it doesnt seem to work.
Code:
$ python2
Python 2.7.13 (default, Jun  8 2017, 01:15:00)
[GCC 4.2.1 Compatible FreeBSD Clang 3.8.0 (tags/RELEASE_380/final 262564)] on freebsd11
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/tensorflow/__init__.py", line 24, in <module>
    from tensorflow.python import *
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 63, in <module>
    from tensorflow.python.framework.framework_lib import *
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/framework_lib.py", line 100, in <module>
    from tensorflow.python.framework.subscribe import subscribe
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/subscribe.py", line 26, in <module>
    from tensorflow.python.ops import variables
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 26, in <module>
    from tensorflow.python.ops import control_flow_ops
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.py", line 70, in <module>
    from tensorflow.python.ops import tensor_array_ops
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/tensor_array_ops.py", line 33, in <module>
    from tensorflow.python.util import tf_should_use
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/util/tf_should_use.py", line 28, in <module>
    from backports import weakref  # pylint: disable=g-bad-import-order
ImportError: cannot import name weakref
>>> import tensorflow as tf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/tensorflow/__init__.py", line 24, in <module>
    from tensorflow.python import *
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
ImportError: cannot import name pywrap_tensorflow

Thanks for your report, the problem is miss several run time python libs, now fixed:

python ./tf_demo2.py
WARNING:tensorflow:Using temporary folder as model directory: /tmp/tmpR2sldG
WARNING:tensorflow:From /usr/local/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/head.py:625: scalar_summary (from tensorflow.python.ops.logging_ops) is deprecated and will be removed after 2016-11-30.
Instructions for updating:
Please switch to tf.summary.scalar. Note that tf.summary.scalar uses the node name instead of the tag. This means that TensorFlow will automatically de-duplicate summary names based on the scope they are created in. Also, passing a tensor or list of tags to a scalar summary op is no longer supported.
2017-06-22 11:25:21.911917: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE3 instructions, but these are available on your machine and could speed up CPU computations.
2017-06-22 11:25:21.911951: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2017-06-22 11:25:21.911956: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-06-22 11:25:21.911959: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-06-22 11:25:21.911963: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-06-22 11:25:21.911966: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
can't determine number of CPU cores: assuming 4
can't determine number of CPU cores: assuming 4
WARNING:tensorflow:From /usr/local/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/head.py:625: scalar_summary (from tensorflow.python.ops.logging_ops) is deprecated and will be removed after 2016-11-30.
Instructions for updating:
Please switch to tf.summary.scalar. Note that tf.summary.scalar uses the node name instead of the tag. This means that TensorFlow will automatically de-duplicate summary names based on the scope they are created in. Also, passing a tensor or list of tags to a scalar summary op is no longer supported.
WARNING:tensorflow:From /usr/local/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/head.py:625: scalar_summary (from tensorflow.python.ops.logging_ops) is deprecated and will be removed after 2016-11-30.
Instructions for updating:
Please switch to tf.summary.scalar. Note that tf.summary.scalar uses the node name instead of the tag. This means that TensorFlow will automatically de-duplicate summary names based on the scope they are created in. Also, passing a tensor or list of tags to a scalar summary op is no longer supported.
train loss: {'loss': 7.4620623e-08, 'global_step': 1000}
eval loss: {'loss': 0.0025487086, 'global_step': 1000}

I will upload new pkg today.
 
I have fixed the problem and test all packages manually with a simple tf_demo. the packages update here: https://github.com/amutu/tf-freebsd-pkg

please note, for tensorflow-1.2.0,there is a new runtime dependency named backports.weakref which do not port to FreeBSD. I port it on PR 220206. Before it committed, you can install from the same dir,the package name is py(27|36)-backports.weakref-1.0.r1.txz. Please install it first, then install the tensorflow package.
 
I have fixed the problem and test all packages manually with a simple tf_demo. the packages update here: https://github.com/amutu/tf-freebsd-pkg

please note, for tensorflow-1.2.0,there is a new runtime dependency named backports.weakref which do not port to FreeBSD. I port it on PR 220206. Before it committed, you can install from the same dir,the package name is py(27|36)-backports.weakref-1.0.r1.txz. Please install it first, then install the tensorflow package.

Hi! I'm install this pkg. But that next ? How i can use it with python and keras?
I'm using python3.6 with keras pkg.
And whan I'm import keras, I'm see error:

Code:
import keras
Using TensorFlow backend.
Traceback (most recent call last):
File "", line 1, in 
File "/home/www/***.com/venv36/lib/python3.6/site-packages/keras/init.py", line 3, in 
from . import utils
File "/home/www/***.com/venv36/lib/python3.6/site-packages/keras/utils/init.py", line 6, in 
from . import conv_utils
File "/home/www/***.com/venv36/lib/python3.6/site-packages/keras/utils/conv_utils.py", line 3, in 
from .. import backend as K
File "/home/www/***.com/venv36/lib/python3.6/site-packages/keras/backend/init.py", line 83, in 
from .tensorflow_backend import *
File "/home/www/vkalendare.com/venv36/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 1, in 
import tensorflow as tf
ModuleNotFoundError: No module named 'tensorflow'

I can't pip install tensorflow

Code:
(venv36) [root@build ~]# pip freeze
Keras==2.0.6
numpy==1.13.1
PyYAML==3.12
scipy==0.19.1
six==1.10.0
Theano==0.9.0
 
download tensorflow pkg from here: https://github.com/amutu/tf-freebsd-pkg ,then

Code:
pkg install ./<download_pkg_file_name>

Note: Read the README.md first, you should install the depends backports.weakref.

I did it. I was install this pkg successful. And I think its can work for main python. But how i can use it with VENV of python3.6 ?

in new venv, for example
Code:
/usr/local/bin/python3.6 -m venv /path/to/new/virtual/environment
source /path/to/new/virtual/environment/bin/activate
>>>import tensorflow
 
I'm having a spot of bother building tensor flow (1.7) on freebsd (11.0-RELEASE-p1) ( with Python 2.7 and all dependencies compiled from the ports tree)

I have followed a lot of postings and made some progress.
( clone from git hub attempted a few patches etc - much trouble linking with libexecinfo as newbie to bazel )

Ideally I would like to use the installed port but when I run it its says

[me@my_host /usr/ports/science/py-tensorflow]# make install
===> py27-tensorflow-1.2.1_1 is marked as broken: fails to build.
*** Error code 1

any advice on the best path forward from this point ?
 
Unfortunately I haven't managed to get the master of their github repo to work on FreeBSD after 1.1

I have submitted a few PR's that has been merged to get it to compile but when trying to use the current master it segfaults in a lib regarding numCpu's where I'm currently stuck and haven't had time to get it going.

If you really need it on freebsd you need to go with 1.1 or use ubuntu where they have pre-built packages.
 
Back
Top