
Install TensorFlow 2.3.1 on Raspberry 64 OS
TensorFlow Lite
TensorFlow Addons
64-OS TensorFlow 2.2.0
64-OS TensorFlow 2.4.1
Introduction.
TensorFlow is a large software library specially developed for deep learning. It consumes a vast amount of resources. You can execute TensorFlow on a Raspberry Pi 4, but don't expect miracles. It can run your models, if not too complex, but it will not be able to train new models. Nor can it perform the so-called transfer learning. In addition to running your pre-built deep learning models, you may use the library to convert so-called frozen TensorFlow models to TensorFlow Lite flat buffer models.
If you only want to get some impression of deep learning, please consider installing TensorFlow Lite. It is much faster and uses far fewer recourses, as being designed for small computers like a Raspberry Pi. There are many ready build models you can use. See our installation guide for the 64-bit Raspberry here.
If you have Ubuntu on your Raspberry Pi, please follow these links for the installation of TensorFlow 2.3.1, Ubuntu 18.04 or 20.04.
We only have a guide for the Raspberry Pi 64-bit operating system. As the massive TensorFlow evolves, building it on a simple 32-bit machine is getting more and more difficult. Many tricks and workarounds were required to compile bazel 3.1.0 and TensorFlow 2.3.1. Besides, most of our visitors today use a 64-bit operating system on their Raspberry Pi for their deep learning models because it is faster and more robust.
We discuss two installations, one for Python 3 and one C++ API library. Unfortunately, there is no official pip3 wheel available for the 2.3.1 version. However, we created our wheel with Bazel and put it on GitHub for your convenience.
Version check.
Please check your operating system before installing TensorFlow on your Raspberry 64-bit OS. Run the command uname -a and verify your version with the screen dump below.

You also need to check your C++ compiler version with the command gcc -v. It must also be an aarch64-linux-gnu version, as shown in the screenshot. If you have a 64-bit operating system, but your gcc version is different from the one given above, reinstall the whole operating system with the latest version. The guide is found here: Install 64 bit OS on Raspberry Pi 4. You must have a 64-bit C ++ compiler as we are going to build libraries. Even if you use Python wheels, gcc is called behind the curtains.
Also, note the zram swap size of more than 3 Gbyte after installation according to our instructions.
The shortcut.
TensorFlow is installed by a Google software installer called Bazel. In the end, Bazel generates a wheel to install the TensorFlow Python version or a tarball when it comes to installing the C++ version. Both methods are well known to Raspberry Pi users. We have posted the Bazel outcomes on our GitHub page. Feel free to use these shortcuts. The whole TensorFlow installation procedure from start to end takes many hours (±41 for Python, ±10 for the C++ library, overclocked to 1900 MHz). With all the tedious work already done, it takes still one hour and twenty minutes to install TensorFlow 2.3.1 on your Raspberry 64-bit OS. For the diehards, the complete procedure is covered later in this manual.
TensorFlow 2.3.1 for Python 3.
The whole shortcut procedure is found below. The wheel was too large to store at GitHub, so Google drive is used. Please make sure you have latest pip3 and python3 version installed, otherwise, pip may come with the message ".whl is not a supported wheel on this platform".
Python check.
Check your Python3 version. Each version needs a unique wheel. Currently, the Raspberry Pi 64-bit operating system uses Python 3.7.3. So you need to download tensorflow-2.3.1-cp37-cp37m-linux_aarch64.whl. Undoubtedly, the Python version will upgrade over time and you will need a different wheel. See out GitHub page for all the wheels.

# get a fresh start
$ sudo apt-get update
$ sudo apt-get upgrade
# install pip and pip3
$ sudo apt-get install python-pip python3-pip
# remove old versions, if not placed in a virtual environment (let pip search for them)
$ sudo pip uninstall tensorflow
$ sudo pip3 uninstall tensorflow
# install the dependencies (if not already onboard)
$ sudo apt-get install gfortran
$ sudo apt-get install libhdf5-dev libc-ares-dev libeigen3-dev
$ sudo apt-get install libatlas-base-dev libopenblas-dev libblas-dev
$ sudo apt-get install liblapack-dev
# upgrade setuptools 47.1.1 -> 50.3.0
$ sudo -H pip3 install --upgrade setuptools
$ sudo -H pip3 install pybind11
$ sudo -H pip3 install Cython==0.29.21
# install h5py with Cython version 0.29.21 (± 6 min @1950 MHz)
$ sudo -H pip3 install h5py==2.10.0
# install gdown to download from Google drive
$ sudo -H pip3 install gdown
# download the wheel
$ gdown https://drive.google.com/uc?id=1jbkp2rSZZ3YY-AM1vuHyB9hI05zrZGHg
# install TensorFlow (± 63 min @1950 MHz)
$ sudo -H pip3 install tensorflow-2.3.1-cp37-cp37m-linux_aarch64.whl
When the installation is successful, you should get the following screendump.

TensorFlow 2.3.1 C++ API.
If you are planning to program in C++, you will need the C++ API build of TensorFlow instead of the Python version. Installing the C ++ library using the pre-build tarball from our GitHub page saves you a lot of time. Please follow the procedure below.
# get a fresh start
$ sudo apt-get update
$ sudo apt-get upgrade
# remove old versions (if found)
$ sudo rm -r /usr/local/lib/libtensorflow*
$ sudo rm -r /usr/local/include/tensorflow
# the dependencies
$ sudo apt-get install wget curl libhdf5-dev libc-ares-dev libeigen3-dev
$ sudo apt-get install libatomic1 libatlas-base-dev zip unzip
# install gdown to download from Google drive (if not already done)
$ pip install gdown
# download the tarball
$ gdown https://drive.google.com/uc?id=1Rm-pICH2nHYFlcvg9Jac7aYT_-Ikrkla
# unpack the ball
$ sudo tar -C /usr/local -xzf libtensorflow_cp37_64OS_2_3_1.tar.gz
You should end up having your TensorFlow library installed at the /usr/local/lib location and the header files in the folder usr/local/include/tensorflow/c.

Installation from scratch.
Memory swap size.
Building the full TensorFlow 2.3.1 package requires more than 6 Gbytes of RAM. If you have a Raspberry Pi 4 with 8 Gbyte RAM, you are in the clear. Otherwise, make sure to increase the swap size to meet this demand. With 4 Gbyte RAM onboard, zram can deliver the extra 2 Gbyte. With 2 Gbyte of RAM, you can no longer rely on zram to compress above a factor of 2. In this case, they have to reinstall dphys-swapfile to get the extra space from your SD card. Please follow the next commands if you have to install dphys-swapfile.
# install dphys-swapfile
$ sudo apt-get install dphys-swapfile
# give the required memory size
$ sudo nano /etc/dphys-swapfile
# reboot afterwards
$ sudo reboot
If all went well, you should have something like this.

For the record, the figure shown is total amount of swap space allocated by dphys-swapfile and zram. Please, don't forget to remove dphys-swapfile when your done.
Bazel.
Bazel is a free software tool from Google used for automatically building and testing software packages. You could compare it to CMake, used by OpenCV, but the latter only builds software and has no test facility. Bazel is written in Java, a platform-independent language, largely based on C ++ in terms of syntax. To compile Bazel, we must first install Java and some other dependencies with the following commands.
# get a fresh start
$ sudo apt-get update
$ sudo apt-get upgrade
# install pip and pip3
$ sudo apt-get install python-pip python3-pip
# install some tools
$ sudo apt-get install build-essential zip unzip curl
# install Java
$ sudo apt-get install openjdk-11-jdk
Next, we can download and unzip the Bazel software. We need Bazel release 3.1.0 for TensorFlow 2.3.1, so be sure you install the right version.
$ wget https://github.com/bazelbuild/bazel/releases/download/3.1.0/bazel-3.1.0-dist.zip
$ unzip -d bazel bazel-3.1.0-dist.zip
$ cd bazel
During installation, Bazel uses a predefined ratio of the available working memory. This ratio is too small due to the limited size of the RAM of the RaspBerry Pi. To prevent crashes, we must define the size of this memory to a maximum 800 Mbyte during the procedure. This is done by adding some extra information to the script file compile.sh. You can add the text -J-Xmx800M to the line that begins with run..(around line 154). See the screen below. Use the well-known <Ctrl + X>, <Y>, <Enter> to save the change (see the slide show above).
$ nano scripts/bootstrap/compile.sh -c

Once the Java environment for Bazel has been maximized to 800 Mb, you can start building the Bazel software with the next commands. When finished, copy the binary file to the /usr/local/bin location so that bash can find the executable anywhere. The final action is to delete the zip file. The total build takes about 33 minutes.
# start the build
$ env EXTRA_BAZEL_ARGS="--host_javabase=@local_jdk//:jdk" bash ./compile.sh
# copy the binary
$ sudo cp output/bazel /usr/local/bin/bazel
# clean up
$ cd ~
$ rm bazel-3.1.0-dist.zip
# if you have a copied bazel to /usr/local/bin you may also
# delete the whole bazel directory, freeing another 500 MByte
$ sudo rm -rf bazel

Install TensorFlow 2.3.1 for Python 3.
With Bazel up and running we can start building TensorFlow 2.3.1 on our 64-bit Raspberry Pi for Python 3. It is almost becoming standard practice. First, install some dependencies, then download the zip from GitHub and finally unpack the software.
# get a fresh start
$ sudo apt-get update
$ sudo apt-get upgrade
# install pip3
$ sudo apt-get install python3-pip
# the dependencies
$ sudo apt-get install build-essential make cmake wget zip unzip
$ sudo apt-get install libhdf5-dev libc-ares-dev libeigen3-dev
$ sudo apt-get install libatlas-base-dev libopenblas-dev libblas-dev
$ sudo apt-get install gfortran liblapack-dev
# upgrade setuptools 47.1.1 -> 51.0.0
$ sudo pip3 install --upgrade setuptools
$ sudo pip3 install keras_applications --no-deps
$ sudo pip3 install keras_preprocessing --no-deps
$ sudo pip3 install -U --user six wheel mock
# download TensorFlow 2.3.1
$ wget -O tensorflow.zip https://github.com/tensorflow/tensorflow/archive/v2.3.1.zip
# unpack and give the folder a convenient name
$ unzip tensorflow.zip
$ mv tensorflow-2.3.1 tensorflow
The final step before building the Python 3 installation wheel is to configure Bazel. This is done by a script file and the command-line options. Let's start with the script file. With the following command, Bazel asks you a few questions. Define Python 3 as the default Python version and 'no' to all other questions.
$ cd tensorflow
$ ./configure
pi@raspberrypi:~/tensorflow $ ./configure
You have bazel 3.1.0- (@non-git) installed.
Please specify the location of python. [Default is /usr/bin/python3]: <enter>
Found possible Python library paths:
/usr/lib/python3/dist-packages
/usr/local/lib/python3.7/dist-packages
Please input the desired Python library path to use. Default is [/usr/lib/python3/dist-packages] <enter>
Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: n
No OpenCL SYCL support will be enabled for TensorFlow.
Do you wish to build TensorFlow with ROCm support? [y/N]: n
No ROCm support will be enabled for TensorFlow.
Do you wish to build TensorFlow with CUDA support? [y/N]: n
No CUDA support will be enabled for TensorFlow.
Do you wish to download a fresh release of clang? (Experimental) [y/N]: n
Clang will not be downloaded.
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native -Wno-sign-compare]: <enter>
Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: n
Not configuring the WORKSPACE for Android builds.
Preconfigured Bazel build configs. You can use any of the below by adding "--config=<>" to your build command. See .bazelrc for more details.
--config=mkl # Build with MKL support.
--config=monolithic # Config for mostly static monolithic build.
--config=ngraph # Build with Intel nGraph support.
--config=numa # Build with NUMA support.
--config=dynamic_kernels # (Experimental) Build kernels into separate shared objects.
--config=v2 # Build TensorFlow 2.x instead of 1.x. <---
Preconfigured Bazel build configs to DISABLE default on features:
--config=noaws # Disable AWS S3 filesystem support.
--config=nogcp # Disable GCP support.
--config=nohdfs # Disable HDFS support.
--config=nonccl # Disable NVIDIA NCCL support.
Configuration finished
With the script file now all set and done, the massive build can start with the command below. The -Xmx1624m sets the memory size of the Java environment where Bazel runs. Give it as much space as possible. We advise 80% of your Raspberry Pi memory size. In our case we have 2 Gbyte onboard, so 1624 Mbyte leaves just enough space for other threads apart from Bazel.
Another point is the number of cores that Basel uses. Always use one core. Better a little slower than a crash and start all over again.
Close all other applications during build, the fewer other processes running, the faster your compilation will be.
$ sudo bazel --host_jvm_args=-Xmx1624m build \
--config=opt \
--config=noaws \
--config=nogcp \
--config=nohdfs \
--config=nonccl \
--config=monolithic \
--config=v2 \
--local_cpu_resources=1 \
--define=tflite_pip_with_flex=true \
--copt=-ftree-vectorize \
--copt=-funsafe-math-optimizations \
--copt=-ftree-loop-vectorize \
--copt=-fomit-frame-pointer \
//tensorflow/tools/pip_package:build_pip_package
After 41 hours compilation, hopefully, you will see with the following screen.

Now we have to generate the wheel and install it. This is done by the commands below.
A few words about the installation of scipy. TensorFlow 2.3.1 uses scipy version 1.14.1. It is known to be problematic to install this version on a Raspberry Pi, due to the lack of a proper wheel. Some may install scipy from source. However, if you install the dependencies of scipy first, your newly created TensorFlow wheel will install scipy 1.14.1 correctly in the end. These dependencies are highlighted in light blue. By the way, this installation takes about an hour and a half.
# synthesize the wheel
$ sudo bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
# install some dependencies (if not already done)
$ sudo apt-get install libatlas3-base libopenblas-dev libopenblas-base libblas-dev
$ sudo apt-get install libgfortran5 liblapack-dev
$ sudo pip3 install pybind11
$ sudo pip3 install Cython==0.29.21
# install h5py with Cython version 0.29.21
$ sudo pip3 install h5py
# remove old versions if found
$ sudo pip uninstall tensorflow
$ sudo pip3 uninstall tensorflow
# get to the folder where the wheel is located and install tensorflow
$ cd /tmp/tensorflow_pkg
$ sudo pip3 install tensorflow-2.3.1-cp37-cp37m-linux_aarch64.whl
Install the TensorFlow 2.3.1 C++ API.
As mentioned earlier, you can install the TensorFlow 2.3.1 C++ API very quickly by using the tarball on our GitHub page; no need to go through lengthy installation procedures. For those who want to build the API themselves, the installation guide now follows.
First, install Bazel if not already done. The procedure is also descript above. Once Bazel is working, you can install the dependencies and download TensorFlow 2.3.1, if not already done for the Python 3 installation earlier.
# the dependencies
$ sudo apt-get install build-essential make cmake wget zip unzip
$ sudo apt-get install libhdf5-dev libc-ares-dev libeigen3-dev
$ sudo apt-get install libatlas-base-dev
# download TensorFlow 2.3.1
$ wget -O tensorflow.zip https://github.com/tensorflow/tensorflow/archive/v2.3.1.zip
# unpack and give the folder a convenient name
$ unzip tensorflow.zip
$ mv tensorflow-2.3.1 tensorflow
Again, we need to configure Bazel before the actual build can start. Despite we are going to build a C++ API, the Bazel script file needs to be set to Python 3 as default Python version and 'no' to all other questions.
$ cd tensorflow
$ ./configure
Once all the questions of the script file are answered, you end up with the same output screen as shown above.
The last step is the command line with its options. Again, almost identical to the one earlier used. Most important here is the --config=monolithic flag. Without this direction, TensorFlow will not work with OpenCV or visa versa. Just like the pip compilation, the memory for Java environment (-Xmx1624m) has to be maximized to 80% of the memory the Raspberry Pi has onboard. The last line indicates a tarball library build instead of a pip wheel.
$ sudo bazel --host_jvm_args=-Xmx1624m build \
--config=opt \
--config=noaws \
--config=nogcp \
--config=nohdfs \
--config=nonccl \
--config=monolithic \
--config=v2 \
--local_cpu_resources=1 \
--copt=-ftree-vectorize \
--copt=-funsafe-math-optimizations \
--copt=-ftree-loop-vectorize \
--copt=-fomit-frame-pointer \
//tensorflow/tools/lib_package:libtensorflow
After a long 9 hours wait, hopefully, you will see with the following screen.

The last step is installing the tarball. This is done by the command below.
$ sudo tar -C /usr/local -xzf bazel-bin/tensorflow/tools/lib_package/libtensorflow.tar.gz
Once installed you must have the same folders (/usr/local/lib and usr/local/include/tensorflow/c) as shown at the beginning of this page.
Cleaning.
After a successful installation, there are many files no longer needed. The intermediate object files generated by bazel occupy some 6 GByte of your disk. Removing doesn't harm anything.
# clean up the whole bazel cache
$ sudo rm -rf ~/.cache/bazel
If you had to reinstall dphys-swapfile, it's time to uninstall it again. This way you will extend the life of your SD card.
# remove the dphys-swapfile (if installed)
$ sudo /etc/init.d/dphys-swapfile stop
$ sudo apt-get remove --purge dphys-swapfile

Install Keras on Raspberry Pi 64 OS.
Once TensorFlow has been install, you can load Keras if you like. Keras is a high-level shell running on top of TensorFlow. It claims to be more user-friendly than TensorFlow, giving a more intuitive set of abstractions that make it easy to develop deep learning models. The installation is just one command.
$ pip3 install keras
Deep learning examples for Raspberry Pi