Install TensorFlow for C

TensorFlow provides a C API that can be used to build bindings for other languages. The API is defined in c_api.h and designed for simplicity and uniformity rather than convenience.

Supported Platforms

TensorFlow for C is supported on the following systems:

  • Linux, 64-bit, x86
  • macOS X, Version 10.12.6 (Sierra) or higher

Setup

Download

TensorFlow C libraryURL
Linux
Linux CPU only https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-linux-x86_64-1.11.0.tar.gz
Linux GPU support https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-gpu-linux-x86_64-1.11.0.tar.gz
macOS
macOS CPU only https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-darwin-x86_64-1.11.0.tar.gz

Extract

Extract the downloaded TensorFlow C library to /usr/local/lib (or use a different directory, if preferred):

sudo tar -xz libtensorflow.tar.gz -C /usr/local

Linker

If you extract the TensorFlow C library to a system directory, such as /usr/local, configure the linker with ldconfig:

sudo ldconfig

Or, if you extract the TensorFlow C library to a non-system directory, such as ~/mydir, then configure the linker environmental variables:

Linux

export LIBRARY_PATH=$LIBRARY_PATH:~/mydir/lib
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/mydir/lib

mac OS

export LIBRARY_PATH=$LIBRARY_PATH:~/mydir/lib
export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:~/mydir/lib

Build

Example program

With the TensorFlow C library installed, create an example program with the following source code (hello_tf.c):

#include <stdio.h>
#include <tensorflow/c/c_api.h>

int main() {
  printf("Hello from TensorFlow C library version %s\n", TF_Version());
  return 0;
}

Compile

Compile the example program to create an executable, then run:

gcc hello_tf.c -o hello_tf

./hello_tf

The command outputs: Hello from TensorFlow C library version number

If the program doesn't build, make sure that gcc can access the TensorFlow C library. If extracted to /usr/local, explictly pass the library location to the compiler:

gcc -I/usr/local/include -L/usr/local/lib hello_tf.c -ltensorflow -o hello_tf

Build from source

TensorFlow is open source. Read the instructions to build TensorFlow's C library from source code.