Install PyTorch on a Raspberry Pi 4.
Last updated: September 22, 2023
This page will guide you through the installation of PyTorch 2.0.0, or an earlier version, 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.
Regularly, we get the question if we have an SD image of a Raspberry Pi 4 with pre-installed frameworks and deep-learning examples.
We are happy to comply with this request. Please, find a complete working Raspberry Pi 4 dedicated to deep learning on our GitHub page. Download the zip file from our GDrive site, unzip and flash the image on a 32 GB SD-card, and enjoy!
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 ncnn on your Raspberry Pi 4. Run the command getconf LONG_BIT and verify your version.
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 2.0 is the logical continuation of 1.13. It is faster and has a tighter connection with Python. Version 2.0 requires minimal Python 3.8. You can only install it on the Bullseye OS. Find more information on this page.
PyTorch 1.13, 1.12, 1.11.
The new PyTorch version has the usual improvements and bug fixes. Take a look at the changelog.
PyTorch 1.10 has the usual improvements and bug fixes. Please note, some operations have different behavior compared to version 1.9. Take a look at the changelog.
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.
Installation by wheel.
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.
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. The old Buster 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. With the new Raspberry Pi Bullseye OS, your Python version is 3.9.2.
Undoubtedly, the Python version will upgrade over time and you will need a different wheel. See out GitHub page for all the wheels.