[Build Error] Error while building tensorflow from source

I want to build tensorflow and have tried several time, but each time I am getting this error. I followed the official instructions given on the tensorflow page. Also, I was not able to find any solution about this issue online.

My OS is Ubuntu 20.04.5 LTS and system is Lenovo v15-g2-itl with Intel Core i3.
Here is the error msg from terminal,

INFO: Analyzed target //tensorflow/tools/pip_package:build_pip_package (0 packages loaded, 0 targets configured).
INFO: Found 1 target…
ERROR: /home/oxe-lpt-50/tensorflow/tensorflow/BUILD:1635:19: Executing genrule //tensorflow:tf_python_api_gen_v2 failed: (Exit 1): bash failed: error executing command /bin/bash -c … (remaining 1 argument skipped)
2023-02-14 18:31:04.022384: I tensorflow/core/util/port.cc:110] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable TF_ENABLE_ONEDNN_OPTS=0.
2023-02-14 18:31:04.084175: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: SSE3 SSE4.1 SSE4.2 AVX AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
Traceback (most recent call last):
File “/home/oxe-lpt-50/.cache/bazel/_bazel_oxe-lpt-50/8c337e2fe02e925d1e887d40f8022df6/execroot/org_tensorflow/bazel-out/k8-opt/bin/tensorflow/create_tensorflow.python_api_tf_python_api_gen_v2.runfiles/org_tensorflow/tensorflow/python/tools/api/generator/create_python_api.py”, line 22, in
from tensorflow.python.tools.api.generator import doc_srcs
File “/home/oxe-lpt-50/.cache/bazel/_bazel_oxe-lpt-50/8c337e2fe02e925d1e887d40f8022df6/execroot/org_tensorflow/bazel-out/k8-opt/bin/tensorflow/create_tensorflow.python_api_tf_python_api_gen_v2.runfiles/org_tensorflow/tensorflow/python/init.py”, line 37, in
from tensorflow.python.eager import context
File “/home/oxe-lpt-50/.cache/bazel/_bazel_oxe-lpt-50/8c337e2fe02e925d1e887d40f8022df6/execroot/org_tensorflow/bazel-out/k8-opt/bin/tensorflow/create_tensorflow.python_api_tf_python_api_gen_v2.runfiles/org_tensorflow/tensorflow/python/eager/context.py”, line 28, in
from tensorflow.core.framework import function_pb2
File “/home/oxe-lpt-50/.cache/bazel/_bazel_oxe-lpt-50/8c337e2fe02e925d1e887d40f8022df6/execroot/org_tensorflow/bazel-out/k8-opt/bin/tensorflow/create_tensorflow.python_api_tf_python_api_gen_v2.runfiles/org_tensorflow/tensorflow/core/framework/function_pb2.py”, line 5, in
from google.protobuf.internal import builder as _builder
ImportError: cannot import name ‘builder’ from ‘google.protobuf.internal’ (/home/oxe-lpt-50/anaconda3/lib/python3.9/site-packages/google/protobuf/internal/init.py)
Target //tensorflow/tools/pip_package:build_pip_package failed to build
Use --verbose_failures to see the command lines of failed build steps.
ERROR: /home/oxe-lpt-50/tensorflow/tensorflow/lite/python/BUILD:69:10 Middleman _middlemen/tensorflow_Slite_Spython_Stflite_Uconvert-runfiles failed: (Exit 1): bash failed: error executing command /bin/bash -c … (remaining 1 argument skipped)
INFO: Elapsed time: 1.653s, Critical Path: 0.51s
INFO: 3 processes: 3 internal.
FAILED: Build did NOT complete successfully

Thank you.

Hi @Ahmad_Tameem & Welcome.
Your machine is 64-bit right?
Can you please tell what command you run?
Did you follow a specific documentation e.g. Building TensorFlow from source?

1 Like

Thank you for your reply, yes I have 64-bit machine. And I have used this documentation provided by tensorflow, Tensorflow - Install from Source

For ./configure I used the default setting with no CUDA support or extra packages (I am building for CPU support only).

I used the following two variation of command for build (each time got same error after 15000 targets),

  1. bazel build //tensorflow/tools/pip_package:build_pip_package
  2. bazel build --config=opt --jobs 2 //tensorflow/tools/pip_package:build_pip_package

Based on the document you have shared, is CUDA is essential for build even I am only building for CPU support?

Thanks again!

No, CUDA/GPU are optional.
Unfortunately I’m unable to reproduce the issue. And searching on the internet I couldn’t find someone else with similar issue (error messages). I’m sure you did it yourself anyways.

Okay, thank you for letting me know and taking the time to look into it. I will try again later with some clean up… or alternatively will build it on some other system.

Thanks again!

I got a very similar message when trying to compile on ubuntu 22.04.
I was trying to compile without SSE4.1 but with CUDA
TF 2.13
CUDA 11.8
cudnn 8.8

bazel build -c opt --copt="-mtune=generic" --copt="-march=x86-64" --verbose_failures //tensorflow/tools/pip_package:build_pip_package
ERROR: /home/oscar/tensorflow/tensorflow/BUILD:1635:19: Executing genrule //tensorflow:tf_python_api_gen_v2 failed: (Exit 1): bash failed: error executing command

from google.protobuf.internal import builder as _builder
ModuleNotFoundError: No module named ‘google.protobuf’
Target //tensorflow/tools/pip_package:build_pip_package failed to build
ERROR: /home/oscar/tensorflow/tensorflow/python/tools/BUILD:82:10 Middleman _middlemen/tensorflow_Spython_Stools_Sfreeze_Ugraph-runfiles failed: (Exit 1): bash failed: error executing command
(cd /home/oscar/.cache/bazel/_bazel_oscar/fcce6a6f08ea56a6f1b9ca3247e24d7f/execroot/org_tensorflow && \

I mispoke before about versions. Before I was using TF 2.13, CUDA 11.8, cudnn 8.8:
CPU: Phenom II X6 1100T

If it’s worthwhile, I went with a simpler build again using bazelist to see if I could get TF 2.13 to compile.
Steps:
bazel clean
./configure (w/o CUDA or anything else)
bazel build --copt=-march=native //tensorflow/tools/pip_package:build_pip_package

Still, it seems to fail the same way:
ERROR: /home/oscar/tensorflow/tensorflow/BUILD:1635:19: Executing genrule //tensorflow:tf_python_api_gen_v2 failed: (Exit 1): bash failed: error executing command /bin/bash -c … (remaining 1 argument skipped)

ERROR: /home/oscar/tensorflow/tensorflow/lite/python/BUILD:71:10 Middleman _middlemen/tensorflow_Slite_Spython_Stflite_Uconvert-runfiles failed: (Exit 1): bash failed: error executing command /bin/bash -c … (remaining 1 argument skipped)
INFO: Elapsed time: 13688.925s, Critical Path: 438.70s
INFO: 16390 processes: 1842 internal, 14548 local.
FAILED: Build did NOT complete successfully

Thanks,
Oscar

Just to follow up. I tried to download TF2.7, as it might be a more stable branch.
Ubuntu 22.04.2 LTS
Phenom II X6 1100T
TF2.7
bazel 3.7.2
python 3.10.6

I tried this build line with no cuda or anything else since this seemed to fix something similar from another post:
bazel build --config=opt --copt=-march=native //tensorflow/tools/pip_package:build_pip_package --cxxopt=“-D_GLIBCXX_USE_CXX11_ABI=0” --noincompatible_do_not_split_linking_cmdline

I don’t get the Middleman error anymore (not sure if that was due to the new build line or just using TF2.7), but I still get this:
ERROR: /home/oscar/build/tensorflow/tensorflow/python/keras/api/BUILD:133:19: Executing genrule //tensorflow/python/keras/api:keras_python_api_gen_compat_v1 failed (Exit 1): bash failed: error executing command /bin/bash -c … (remaining 1 argument(s) skipped)

It looks like getting a newer processor might be the only way forward. The error looks like it might not even be required but it’s a hard fail.

I was stuck in a similar problem and then I got in this github forum Target 'build_pip_package' no longer existing? · Issue #63298 · tensorflow/tensorflow · GitHub that redirected me to this new tensorflow compilation guide Build from source  |  TensorFlow and them I was able to install it on my machine. In case if helps anyone