Can't install Tensorflow 2.3 on Raspberry Pi 4

Solution 1:

For the Raspberry Pi 4 ARM64 platform TensorFlow is not available as pre-built version for pip and must be compiled manually. Follow these instructions and cross compile it on a different more powerful environment, not on the Pi 4. The instructions are not up to date anymore, I have completed them here. These are the steps to cross compile TensorFlow 2.3.1 on an Ubuntu Linux:

git clone [email protected]:tensorflow/tensorflow.git
cd tensorflow

The old version 2.3.1 does not know Python 3.8. When using Python 3.7 then this might not be necessary.

wget https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/ci_build/Dockerfile.pi-python38 -o tensorflow/tools/ci_build/Dockerfile.pi-python38

CMake is now also required, I'm using here the master branch, which is corresponding to version 2.5.0 (in case this does not work anymore in the future with the master version):

wget -O tensorflow/tools/ci_build/install/install_cmake.sh https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/tools/ci_build/install/install_cmake.sh
chmod +x tensorflow/tools/ci_build/install/install_cmake.sh

To support Python 3.8 add support for it, again taken from the master branch (version 2.5.0):

wget -O tensorflow/tools/ci_build/install/install_pi_python3x_toolchain.sh https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/tools/ci_build/install/install_pi_python3x_toolchain.sh
chmod +x tensorflow/tools/ci_build/install/install_pi_python3x_toolchain.sh

You also need these files for the build:

wget -O tensorflow/tools/ci_build/pi/build_raspberry_pi.sh https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/tools/ci_build/pi/build_raspberry_pi.sh
chmod +x tensorflow/tools/ci_build/pi/build_raspberry_pi.sh

There is an issue with newer numpy version. Use this cherry-pick from git:

git cherry-pick 75ea0b31477d6ba9e990e296bbbd8ca4e7eebadf

Now finally all can be compiled. This takes several hours most likely.

tensorflow/tools/ci_build/ci_build.sh PI-PYTHON38 tensorflow/tools/ci_build/pi/build_raspberry_pi.sh AARCH64

If something fails and some files have to be patched or added keep in mind to clean the Docker container to prevent using the Docker cache when restarting the build process. docker images, docker rmi <imageid> and docker rm <container> are your friends.