Install TensorFlow 2.2.0 on Raspberry 64 OS - Q-engineering
Q-engineering
Q-engineering
Go to content
images/empty-GT_imagea-1-.png
TensorFlow on Raspberry Pi

Install TensorFlow 2.2.0 on Raspberry 64 OS

32-OS TensorFlow 2.2.0

Introduction.

The Raspberry Pi is moving towards a 64-bit operating system. Within a year or so, the 32-bit OS will be fully replaced by the faster 64-bit version.
The Raspberry Foundation has recently released a more than functional beta version. Time to explore it. This guide will install TensorFlow 2.2.0 on a Raspberry Pi 4 with a 64-bit operating system. The second part of this guide covers the installation on a Raspberry Pi with Ubuntu 18.04 or 20.04.

We discuss two installations, one for Python 3 and one C++ API library.
This manual is written for the Raspberry Pi 4. It can be used for the Raspberry 3 B +, but we don't encourage the idea given the computing power of the Raspberry Pi 3. TensorFlow occupies about 1 GByte on your SD-microcard. Unfortunately, there is no official pip3 wheel available for the 2.2.0 version. However, we created our wheel with Bazel and put it on GitHub for your convenience.

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.

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.

VersionCheck_64

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 (±33 for Python, ±10 for the C++ library). With all the tedious work already done, it takes still one hour to install TensorFlow 2.2.0 on your Raspberry 64-bit OS. For the diehards, the complete procedure is covered later in this manual.
TensorFlow 2.2.0 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 comes with the message ".whl is not a supported wheel on this platform".
Note also sudo pip3 install Cython==0.29.16. Without the version 0.29.16 directive, the h5py installation fails.
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.2.0-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.

Python version check

# get a fresh start (remember, the 64-bit OS is still under development)
$ sudo apt-get update
$ sudo apt-get upgrade
# 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
$ sudo pip3 install pybind11
$ sudo pip3 install Cython==0.29.16
# install h5py with Cython version 0.29.16 (± 6 min @1950 MHz)
$ sudo pip3 install h5py
# upgrade setuptools 40.8.0 -> 47.1.1
$ sudo pip3 install --upgrade setuptools
# install gdown to download from Google drive
$ pip3 install gdown
# set PATH
$ export PATH=$PATH:/home/pi/.local/bin
# download the wheel
$ gdown https://drive.google.com/uc?id=1fR9lsi_bsI_npPFB-wZyvgjbO0V9FbMf
# install TensorFlow (± 63 min @1950 MHz)
$ sudo -H pip3 install tensorflow-2.2.0-cp37-cp37m-linux_aarch64.whl
# and complete the installation by rebooting
$ reboot
When the installation is succesful, you should get the following screendump.

Tensorflow 2.2 succes 64 bit
TensorFlow 2.2.0 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
# set PATH
$ export PATH=$PATH:/home/pi/.local/bin
# download the tarball
$ gdown https://drive.google.com/uc?id=1TErP9AfTTiWQB9E82JnwAaqFMuM82YaN
# unpack the ball
$ sudo tar -C /usr/local -xzf libtensorflow_cp37_64OS_2_2_0.tar.gz
# and finish by rebooting
$ reboot
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.

TensorFlow API succes



Installation from scratch

Memory swap size.

Building the full TensorFlow 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
$ reboot.
If all went well, you should have something like this.

Large swap

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 some tools
$ sudo apt-get install build-essential zip unzip
# install Java
$ sudo apt-get install openjdk-11-jdk
Next, we can download and unzip the Bazel software. We need Bazel release 2.0.0 for TensorFlow 2.2.0, so be sure you install the right version.
$ wget https://github.com/bazelbuild/bazel/releases/download/2.0.0/bazel-2.0.0-dist.zip
$ unzip -d bazel bazel-2.0.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 to increase the size of this memory to a minimum 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 152). 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
Bazel 800 Mb heap

Once the Java environment for Bazil 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-2.0.0-dist.zip
Bazel succes 64

Install TensorFlow 2.2.0 for Python 3.

With Bazel up and running we can start building TensorFlow 2.2.0 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 (remember, the 64-bit OS is still under development)
$ sudo apt-get update
$ sudo apt-get upgrade
# 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
$ sudo pip3 install keras_applications==1.0.8 --no-deps
$ sudo pip3 install keras_preprocessing==1.1.0 --no-deps
$ sudo pip3 install pybind11
$ sudo pip3 install Cython==0.29.16
# install h5py with Cython version 0.29.16 (± 6 min @1950 MHz)
$ sudo pip3 install h5py
# upgrade setuptools 40.8.0 -> 47.1.1
$ sudo pip3 install --upgrade setuptools
# download TensorFlow 2.2.0
$ wget -O tensorflow.zip https://github.com/tensorflow/tensorflow/archive/v2.2.0.zip
# unpack and give the folder a convenient name
$ unzip tensorflow.zip
$ mv tensorflow-2.2.0 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. Most important, define Python 3 as default Python version. Python 2 generates a lot of missing dependencies errors. Give 'no' to all other questions.
$ cd tensorflow
$ ./configure
pi@raspberrypi:~/tensorflow $ ./configure
Extracting Bazel installation...
WARNING: --batch mode is deprecated. Please instead explicitly shut down your Bazel server using the command "bazel shutdown".
You have bazel 2.0.0- (@non-git) installed.
Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python3

Found possible Python library paths:
 /usr/local/lib/python3.7/dist-packages
 /usr/lib/python3/dist-packages
Please input the desired Python library path to use.  Default is [/usr/local/lib/python3.7/dist-packages] <enter>

Do you wish to build TensorFlow with XLA JIT support? [Y/n]: n
No XLA JIT support will be enabled for TensorFlow.

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.

Do you wish to build TensorFlow with MPI support? [y/N]: n
No MPI support will be enabled for TensorFlow.

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=gdr          # Build with GDR support.
--config=verbs        # Build with libverbs support.
--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=noignite     # Disable Apache Ignite support.
--config=nokafka      # Disable Apache Kafka support.
--config=nonccl       # Disable NVIDIA NCCL support.
Configuration finished
With the scrip file now all set and done, the great build can start with the command below. The -Xmx1624m sets the memory size of the Java environment in which 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 \
             --copt=-ftree-vectorize \
             --copt=-funsafe-math-optimizations \
             --copt=-ftree-loop-vectorize \
             --copt=-fomit-frame-pointer \
             //tensorflow/tools/pip_package:build_pip_package
After 33 hours compilation, hopefully, you will see with the following screen.

Compile succes

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.2.0 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 marked in light blue. By the way, this installation takes about half an hour to complete.
# 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.16
# install h5py with Cython version 0.29.16 (± 6 min @1950 MHz)
$ sudo pip3 install h5py
# upgrade setuptools 40.8.0 -> 47.1.1
$ sudo pip3 install --upgrade setuptools
$ sudo pip3 install -U --user six wheel mock
# get to the folder where the wheel is located and install tensowflow
$ cd /tmp/tensorflow_pkg
$ sudo pip3 install tensorflow-2.2.0-cp37-cp37m-linux_aarch64.whl

Install the TensorFlow 2.2.0 C++ API.
As mentioned earlier, you can install the TensorFlow 2.2.0 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.
Next, 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.2.0, 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.1.0
$ wget -O tensorflow.zip https://github.com/tensorflow/tensorflow/archive/v2.2.0.zip
# unpack and give the folder a convenient name
$ unzip tensorflow.zip
$ mv tensorflow-2.2.0 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. Python 2 still generates a lot of missing dependencies errors. Give '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.

TF succes 2_1_0

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.
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

Installation on Raspberry Pi 4 with Ubuntu

Ubuntu 18.04

The installation of TensorFlow on a Raspberry Pi with Ubuntu 18.04 is, in fact, identical to the installation on a Raspberry Pi 64-bit OS, as described above. All procedures and commands are the same. The only difference is the Python version. Ubuntu 18.04 uses Python 3.6, where Raspberry PI 64 OS has the Python version 3.7.
The required wheel for Ubuntu 18.04 is, therefore, tensorflow-2.2.0-cp36-cp36m-linux_aarch64.whl. For the correct procedure, follow the next commands.
Ubuntu 18.04
# get a fresh start (remember, the 64-bit OS is still under development)
$ sudo apt-get update
$ sudo apt-get upgrade
# 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
$ sudo pip3 install pybind11
$ sudo pip3 install Cython==0.29.16
# install h5py with Cython version 0.29.16 (± 6 min @1950 MHz)
$ sudo pip3 install h5py
# upgrade setuptools 40.8.0 -> 47.1.1
$ sudo pip3 install --upgrade setuptools
# install gdown to download from Google drive
$ pip3 install gdown
# set PATH
$ export PATH=$PATH:/home/pi/.local/bin
# download the wheel
$ gdown https://drive.google.com/uc?id=1fR9lsi_bsI_npPFB-wZyvgjbO0V9FbMf
# install TensorFlow (± 63 min @1950 MHz)
$ sudo -H pip3 install tensorflow-2.2.0-cp36-cp36m-linux_aarch64.whl
# and complete the installation by rebooting
$ reboot

Ubuntu 20.04.

The installation of TensorFlow on a Raspberry Pi 4 with Ubuntu 20.04 is, again, almost identical the one for the Raspberry Pi 64-bit OS. Ubuntu 20.04 has Python 3.8, so we need tensorflow-2.2.0-cp38-cp38-linux_aarch64.whl for this installation.
Ubuntu 20.04
# get a fresh start (remember, the 64-bit OS is still under development)
$ sudo apt-get update
$ sudo apt-get upgrade
# 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
$ sudo pip3 install pybind11
$ sudo pip3 install Cython==0.29.16
# install h5py with Cython version 0.29.16 (± 6 min @1950 MHz)
$ sudo pip3 install h5py
# upgrade setuptools 40.8.0 -> 47.1.1
$ sudo pip3 install --upgrade setuptools
# install gdown to download from Google drive
$ pip3 install gdown
# set PATH
$ export PATH=$PATH:/home/pi/.local/bin
# download the wheel
$ gdown https://drive.google.com/uc?id=1uKa6dsyqBkuwo-BYR8GenG_56z8UGgKk
# install TensorFlow (± 73 min @1950 MHz)
$ sudo -H pip3 install tensorflow-2.2.0-cp38-cp38-linux_aarch64.whl
# and complete the installation by rebooting
$ reboot
Final comments.
If you plan to build TensorFlow from scratch on a Raspberry Pi running Ubuntu 20.04, note the absence of Python2, a requirement of Bazel. Without Python2 Bazel cannot be installed. The only solution we found effective seems to be a symbolic link to Python3 as shown below. Once the symlink is established, the procedure is identical to the one we used for the Raspberry Pi 64-bit OS. Please pay attention to your memory size. Memory and swap together need to be more than 6 Gbyte. See the remarks regarding this topic above.
# symlink
$ sudo ln -s /usr/bin/python3 usr/bin/python
Deep learning examples for Raspberry Pi
Raspberry and alt
Install 32 OS
Raspberry Pi 4
Install 64 OS
Back to content