Install PyTorch on a Raspberry Pi 4.
Last updated: July 12, 2021
This page will guide you through the installation of PyTorch 1.7.0, 1.7.1, 1.8.0, 1.8.1 or 1.9.0, TorchVision, LibTorch and Caffe2 on a Raspberry Pi 4 with a 64-bit operating system.
PyTorch is a software library specially developed for deep learning. It consumes a lot of resources of your Pi. You can run PyTorch on a Raspberry Pi 4, but don't expect miracles. It runs your models, if not too complicated, but it can't train new models. Nor can it perform so-called transfer learning.
PyTorch runs on Python. A C++ API is available, but we have not tested it. If you want your PyTorch models to run fast on your Raspberry Pi, consider exporting your model to ONNX. Once saved as an ONNX model, you can import it into an ARM-friendly C++ framework such as ncnn or MNN. It will speed up your model considerably.
We only have a guide for the Raspberry Pi 64-bit operating system. As PyTorch evolves, building it on a simple 32-bit machine is getting more and more difficult. The JIT trace, for instance, will not work on a 32-bit OS. 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 with a Python 3 wheel. The other method is the build from scratch. Unfortunately, there is no official pip3 wheel available for the Raspberry Pi 4. However, we created these wheels and put them on GitHub for your convenience.
Please check your operating system before installing PyTorch 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. A 64-bit C ++ compiler is needed as we are going to build libraries. Even if you use Python wheels, gcc is called behind the curtains.
Also important, the zram swap size of more than 3 Gbyte after installation according to our instructions. Less is no problem as long as you don't build PyTorch from scratch.
PyTorch is build by Ninja. It takes about 3 hours to complete the whole build. We have posted the wheels on our GitHub page. Feel free to use these. With all the tedious work already done, it takes now only a few minutes to install PyTorch on your Nano. For the diehards, the complete procedure is covered later in this manual.
Some warnings about version 1.9.0. As seen here, quite a few changes are made to the software since the last version. Not all operations and declarations are supported anymore. It can cause backward compatibility issues when your 1.8 networks are running on this new version.
The whole 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".
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 torch-1.X.Y-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.