Sam Westby

How to Install Tensorflow 2 on your Raspberry Pi

For some strange reason, Tensorflow is hard to install on the Raspberry Pi. In this tutorial, I break down each step into bite-sized chunks so you can start your machine learning journey

Samuel Westby

June 9th, 2022

raspberry pi

machine learning

Supplies

Step 1 - Set up your Pi

If you haven't set up your Raspberry Pi yet, you can follow my tutorial for that here.

Step 2 - Find your .whl file

Open a terminal to your Pi. You can do this with SSH or directly on your Pi as a desktop. First, let's update your pi with the following code:

sudo apt update
sudo apt upgrade

Now we're going to check the architecture and OS. In the terminal, run the following commands:
uname -m
cat /etc/os-release
python -V

My output looks like this
screenshot of architecture and os commands
NOTE: you should be using Python 3
Using the output of these commands, you need to find a privately hosted Tensorflow wheel that works with your system. You can find the list of wheels here.

This is how to interpret the file names: screenshot of wheel names for privately hosted wheels
2.5.0 = Tensorflow version
cp37 = Python version 3.7
armv7l = Architecture (uname -m)

NOTE: There are no combinations for Python 3.8 or Python 3.9 with the armv7l architecture. This means you need to downgrade your Python version to 3.7, or upgrade your architecture to the 64-bit aarch64. You can follow my tutorial for downgrading your Python version here

Once you find a .sh file that works for you, open it and we'll come back to it.

Step 3 - Set up your environment

Back in your terminal, make a project directory. This is optional.

cd Desktop
mkdir project
cd project

Now, create a virtual environment. If you are not familiar with virtual environments, I made a video explaining them in depth.
python -m venv env
env/bin/activate

You should now see a small env at the start of your terminal line. screenshot of env bubble in raspberry pi command line
Copy and run the following command in your terminal. This installs all the necessary packages.
sudo apt-get install -y libhdf5-dev libc-ares-dev libeigen3-dev gcc gfortran libgfortran5 libatlas3-base libatlas-base-dev libopenblas-dev libopenblas-base libblas-dev liblapack-dev cython3 libatlas-base-dev openmpi-bin libopenmpi-dev python3-dev
pip install -U wheel mock six

Step 4 - Install Tensorflow

Go back to the Github page with the wheel file that is compatible with your system. You should see a buttom that says "Raw" or "View Raw". Click that. screenshot of view raw button on Github
Copy the url of the raw text and go back to your terminal. Run the following commands:

wget [THE RAW FILE URL YOU COPIED]

The end of the output will have a filename that looks like this. Copy that. screenshot of wget output
sudo chmod +x [FILENAME YOU COPIED]
./[FILENAME YOU COPIED]
sudo pip uninstall tensorflow
pip uninstall tensorflow

Now you have to find the name of your Tensorflow .whl file. Type ls to list the files in your directory and look for a file with a name like tensorflow-[VERSION].whl. VERSION will be a Tensorflow version like 2.5.0. Copy that filename. Replace tensorflow-[VERSION].whl in the code below with the filename you copied. exec $SHELL # restarts the terminal source env/bin/activate # reactivate the virtual env

Step 5: Testing

Restart the shell and reactivate your virtual environment

exec $SHELL
source env/bin/activate

Open a Python interpreter and test out your Tensorflow. If your installed everything correctly, there should be no warnings or errors.
python
import tensorflow
tensorflow.__version__
quit()
NOTE: If you have an hdf5 warning, refer to 6:55 in the video for how to resolve the issue.

Lorem ipsum, dolor sit amet consectetur adipisicing elit. Libero tenetur non laborum dolorem laboriosam quo quibusdam assumenda dolores eveniet. Ipsum?