aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/docs_src/install/install_c.md
blob: 3a153e81145676aab7e9f95f9d1c78fa7531a2cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Installing TensorFlow for C

TensorFlow provides a C API defined in
[`c_api.h`](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/c/c_api.h),
which is suitable for
[building bindings for other languages](https://www.tensorflow.org/extend/language_bindings).
The API leans towards simplicity and uniformity rather than convenience.


## Supported Platforms

This guide explains how to install TensorFlow for C.  Although these
instructions might also work on other variants, we have only tested
(and we only support) these instructions on machines meeting the
following requirements:

  * Linux, 64-bit, x86
  * macOS X, Version 10.11 (El Capitan) or higher


## Installation

Take the following steps to install the TensorFlow for C library and
enable TensorFlow for C:

  1. Decide whether you will run TensorFlow for C on CPU(s) only or
     with the help of GPU(s). To help you decide, read the section
     entitled "Determine which TensorFlow to install" in one of the
     following guides:

       * @{$install_linux#determine_which_tensorflow_to_install$Installing TensorFlow on Linux}
       * @{$install_mac#determine_which_tensorflow_to_install$Installing TensorFlow on macOS}

  2. Download and extract the TensorFlow C library into `/usr/local/lib` by
     invoking the following shell commands:

         TF_TYPE="cpu" # Change to "gpu" for GPU support
         OS="linux" # Change to "darwin" for macOS
         TARGET_DIRECTORY="/usr/local"
         curl -L \
           "https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-${TF_TYPE}-${OS}-x86_64-1.4.0-rc1.tar.gz" |
           sudo tar -C $TARGET_DIRECTORY -xz

     The `tar` command extracts the TensorFlow C library into the `lib`
     subdirectory of `TARGET_DIRECTORY`. For example, specifying `/usr/local`
     as `TARGET_DIRECTORY` causes `tar` to extract the TensorFlow C library
     into `/usr/local/lib`.

     If you'd prefer to extract the library into a different directory,
     adjust `TARGET_DIRECTORY` accordingly.

  3. In Step 2, if you specified a system directory (for example, `/usr/local`)
     as the `TARGET_DIRECTORY`, then run `ldconfig` to configure the linker.
     For example:

     <pre><b>sudo ldconfig</b></pre>

     If you assigned a `TARGET_DIRECTORY` other than a system
     directory (for example, `~/mydir`), then you must append the extraction
     directory (for example, `~/mydir/lib`) to two environment variables.
     For example:

     <pre> <b>export LIBRARY_PATH=$LIBRARY_PATH:~/mydir/lib</b> # For both Linux and macOS X
     <b>export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/mydir/lib</b> # For Linux only
     <b>export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:~/mydir/lib</b> # For macOS X only</pre>



## Validate your installation

After installing TensorFlow for C, enter the following code into a file named
`hello_tf.c`:

```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;
}
```

### Build and Run

Build `hello_tf.c` by invoking the following command:


<pre><b>gcc hello_tf.c</b></pre>


Running the resulting executable should output the following message:


<pre><b>a.out</b>
Hello from TensorFlow C library version <i>number</i></pre>


### Troubleshooting

If building the program fails, the most likely culprit is that `gcc` cannot
find the TensorFlow C library.  One way to fix this problem is to specify
the `-I` and `-L` options to `gcc`.  For example, if the `TARGET_LIBRARY`
was `/usr/local`, you would invoke `gcc` as follows:

<pre><b>gcc -I/usr/local/include -L/usr/local/lib hello_tf.c -ltensorflow</b></pre>

If executing `a.out` fails, ask yourself the following questions:

  * Did the program build without error?
  * Have you assigned the correct directory to the environment variables
    noted in Step 3 of [Installation](#installation)?
  * Did you export those environment variables?

If you are still seeing build or execution error messages, search (or post to)
[StackOverflow](www.stackoverflow.com/questions/tagged/tensorflow) for
possible solutions.