Installing TensorFlow on Raspberry Pi - Straightforward Method

Subscribe to my newsletter and never miss my upcoming articles

Installing TensorFlow directly from pip for Raspberry Pi will not work in most situations. This is due to certain unexplained compatibility bugs that may be time exhausting and frustrating to solve manually. Another typical error is network error when downloading from pypi.org reaches 80-99%.

Today, I'm going to demonstrate the easiest way to install TensorFlow on Raspberry Pi in 5 minutes without any mistake. However, before I start, you would need a decent network (at least 256kbps) and a data plan of around 300MB.

Steps to Install TensorFlow on Raspberry Pi

Notice: Please execute the following steps in order and don't skip any.

1. Update and Upgrade APT

Launch the terminal. You will need to upgrade your apt-get package repository using the command below.

# to upgrade the list of packages on your system
apt-get update

# upgrade all these packages to the latest version
apt-get upgrade

2. Installing Python 3

While you might have Python 3 installed on your Raspberry Pi, just enter the following commands to be double sure.

# this command might take few minutes to execute
apt-get install python3

3. Set Default Python to Python 3 through ~/.bashrc

To avoid conflict set default Python to Python 3. Note, Python 2 has been deprecated - don't use it!

# this command opens your /~.bashrc file
nano ~/.bashrc

Copy the command below and append it towards the end of the file and press Control + S (to save) and Control + X (to exit).

alias python=python3

# pip3 comes with python3
alias pip=pip3

Source your ~/.bashrc file to effect the change using the following command:

# source your ~/.bashrc file
source ~/.bashrc

4. Download TensorFlow using wget

This is definitely the hardest part of the setup

At the time of writing this tutorial, the latest TensorFlow is 2.4.0. The link can be found on this Github page. In the future, we might have a newer release just make sure you copy the link that follows this format tensorflow-.._-cp37-none-linux_armv7l.whl.

Let's use wget to download the package:

# this command downloads the required package
wget https://github.com/lhelontra/tensorflow-on-arm/releases/download/v2.4.0/tensorflow-2.4.0-cp37-none-linux_armv7l.whl

In case of network failure, you don't need to start downloading all over again as wget can help you resume failed downloads when you add a continue flag; use the command below to resume a failed download.

# this command downloads the required package
wget --continue https://github.com/lhelontra/tensorflow-on-arm/releases/download/v2.4.0/tensorflow-2.4.0-cp37-none-linux_armv7l.whl

5. Install the Downloaded TensorFlow Package

After downloading the TensorFlow wheel package for Raspberry you can install it by using the following command (this might take up to 15 minutes):

# note the file name must correspond to what you download
# here I am about to install version 2.4.0
pip install tensorflow-2.4.0-cp37-none-linux_armv7l.whl

In case of any error, please retry the command or leave a comment below.

6. Testing the TensorFlow

The first test is to make sure that TensorFlow is properly installed on our Raspberry Pi, enter the following command:

# show the list of all installed packages and filter tensorflow
pip list | grep tensorflow

The output should look like below:

tensorflow==2.4.0

The second test is to check if TensorFlow is actually working. Run the command below:

# open a python interpreter
python

Copy the following code line by line and paste them in the interpreter:

# imports tensorflow
import tensorflow as tf

# show tensorflow version
tf.__version__

The output should look like below:

'2.4.0'

That's all. Wishing you all the best in your IoT and AI career!

Buy me a Coffee

Pink and Cream Modern Feminine Personal General Thanks Folded Card.png

Support me on Patreon and get some goodies!

References

No Comments Yet