How to build a shared library for TensorFlow on Travis-CI
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Building a shared library that links against TensorFlow on Travis CI is mostly a C++ build-and-dependency problem, not a special Travis-only feature. The important parts are choosing a consistent Python and TensorFlow version, obtaining the correct compiler and linker flags from TensorFlow, and making the CI environment match what you expect locally. Travis is less common today than some newer CI platforms, but the mechanics are still straightforward.
Decide What You Are Building Against
There are two common cases:
- a shared library that links against TensorFlow headers and libraries exposed through the Python package
- a custom TensorFlow op or plugin built from C++
For many lightweight cases, installing TensorFlow in Python and using tf.sysconfig to obtain compile and link flags is the easiest route.
A minimal source file might look like this:
This is enough to verify that your build can see TensorFlow headers and produce a shared object.
Use tf.sysconfig for Flags
TensorFlow's Python package can tell you which compiler and linker flags are required.
That is much safer than hardcoding include and library paths manually, because TensorFlow packaging details vary by version and platform.
A local build command can then look like:
Travis CI Configuration
A minimal .travis.yml for this style of build might be:
This does three things:
- installs TensorFlow
- confirms the package imports
- compiles the shared library using TensorFlow's own reported flags
Keep the Build Environment Predictable
TensorFlow and C++ toolchains can be sensitive to version drift. A build that works locally may fail in CI because of:
- different Python versions
- incompatible TensorFlow wheels
- older system compilers
- missing system libraries
That is why pinning versions helps.
Use a version that you know matches the rest of your project and platform expectations.
Artifact Handling
If the library build is an intermediate verification step, checking that libexample.so exists may be enough. If the built artifact needs to be downloaded later, configure Travis to upload it as a build artifact or publish it in a release workflow.
The important point is that CI should do more than "compile without crashing." It should verify that the resulting library is actually present and, ideally, loadable by a small test.
A Basic Runtime Check
A simple post-build smoke test can catch linker issues early.
If compilation succeeds but loading fails, you may have runtime library-path or symbol issues even though the compiler stage passed.
Common Pitfalls
The most common mistake is hardcoding include or linker paths instead of using tf.sysconfig. TensorFlow packaging details change enough that hardcoded paths become brittle quickly.
Another mistake is letting the CI environment drift from local development, especially in Python version, TensorFlow version, or compiler version.
Developers also often stop at a successful compile without testing whether the shared library can actually be loaded.
Finally, be aware that Travis itself is not the hard part here. The real issues are TensorFlow binary compatibility and reliable dependency pinning.
Summary
- Install TensorFlow in CI and use
tf.sysconfigfor compile and link flags. - Keep Python, TensorFlow, and compiler versions consistent with local development.
- Build the shared library with
-sharedand-fPICas appropriate. - Add at least a smoke test that confirms the artifact exists and can be loaded.
- Treat TensorFlow version compatibility as part of the build design, not as an afterthought.
Related reading
- How to build and use Google TensorFlow C api
- How to build TensorFlow Lite as a static library and link to it from a separate CMake project?
- How to bulk write TFRecords?
- How to cache data during the first epoch correctly Tensorflow, dataset?
- How to calculate AUC with tensorflow?
- How to calculate input_dim for a keras sequential model?
- how to calculate PDF in tensorflow
- How to calculate perplexity of RNN in tensorflow
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.