A Step by Step guide to installing PyTorch in Raspberry Pi

Suparna S Nair
Aug 17, 2019 · 8 min read

Udacity Secure and Privacy AI Challenge

In this article, I walk you through the steps to install PyTorch in your Raspberry Pi. This is the third article of the series wherein you end up training a recurrent neural network (RNN) on two Raspberry Pis using a concept called Federated learning. ✨

This tutorial assumes that you are familiar with the following:

  • Setting up Raspberry Pi
  • Enabling SSH in RPi and connecting to it from your laptop
  • Connecting multiple Raspberry Pis with the laptop over the same network and accessing internet in RPis
  • A basic understanding on the concept of machine learning
  • Basic operations using Python (especially, installation of packages using pip)

In case you are in doubt, you can refresh the basics of setting up Raspberry Pi from here and here!


First things first — What is PyTorch?

Before you understand PyTorch, you need to have an idea of what the latest buzzword, or jargon machine learning is all about.

Machine learning is the science of getting computers to act without being explicitly programmed. It can be considered as a subset of artificial intelligence

Curious to learn the basics of Machine Learning? This is a great course to start with. I warn you that it crunches your old school math skills, but its worth the quest! This course gives a really strong foundation to the theoretical aspects of Machine Learning. 😊

Curios how I found this? Check out this funny article!

So, let’s come back to PyTorch.

PyTorch is an open source machine learning library based on the Torch library, used for applications such as computer vision and natural language processing. It is primarily developed by Facebook’s artificial intelligence research group. It is free and open-source software released under the Modified BSD license (Credits: Wikipedia).

In short, PyTorch is a machine learning library in Python. In this tutorial, we will install PyTorch in your Raspberry Pi.

Okay, so a little bit about my story here. My inspiration to write this article emerged from the frustrated nights I had spend trying to install PyTorch in my RPi for a cool little project I was working on as part of Udacity’s Secure and Private AI Scholarship Challenge. The instructions I found in the internet looked really simple to me 😁 and I was so confident that I could finish the entire project in a day 👀. It was just a few instructions in command line that I had to follow. But things didn’t turn out the way I imagined. Installation of PyTorch took me an entire weekend and kept me sleepless and frustrated. I had been using Python in my projects and work for the past 3–4 years and this is something that entirely exhausted my spirits! Determined to not leave it at that, I searched for alternate ways to get PyTorch installed. Finally after several days of trial and error from various internet searches (Kudos to all those great scholars 👍, but unfortunately, I couldn’t keep track of all the websites that had saved me), I found an easy way to get this activity done.

So this is a compilation of the steps that worked for me as well as others in the scholarship challenge.


Preparing for the PyTorch installation

The latest OS of Raspberry Pi, the Raspbian Buster comes with Python 3.7 installed. So I’d recommend you to have Raspberry Pi flashed with Raspbian Buster to have a hassle-free installation of PyTorch.

The most simplest way to install a Python package is via PIP (a recursive acronym for PIP installs Packages or Preferred Installer Program)

For example, if you want to install an RS232 library in Python, launch Python3 using:

And then type in:

This would fetch the package from the Python Package Index (PyPi) and installs it to your machine (be it Raspberry Pi or laptop or any other device that has Python installed and has internet access).

You might have noticed that I have used python3 here. Python has two flavors, Python 2x and Python3x. Python2x was the legacy and Python3x is the flavor which will have stable releases and updates going forward.

Your Raspberry Pi will also have both flavors of Python installed. To launch the Python 2x version, simply type python to launch it in the terminal. Typing in python3 would launch the 3x version. I’d recommend using the 3x version.

Similarly, the Python pip package also has two flavors for Python 2x and 3x. The command pip invokes the pip package for Python2x and pip3 invokes the pip package for Python3x.

Now that you have brushed up the basics of installing a Python package in your Raspberry Pi, let’s talk a bit about the various Raspberry Pi architectures.

Raspberry Pi has ARM processor architecture instead of the Intel x86 architecture that is present in most of the desktop PCs (for the curious readers, this article gives a detailed information on how ARM and Intel x86 processor architectures differ). The aspect which is important for us here is that, Python packages that are installed in RPi should be compiled for its specific architecture. This means that you cannot simply copy a package from your x86 based PC into your Raspberry Pi since it will not be compatible with the processor architecture of RPi.

Installing Python packages from pip will download and install the ARM-compatible version in your Raspberry Pi.

Now, the sad part: PyTorch does not have an official package in PyPi for Raspberry Pi (ARM compatible) for versions 1.0 and above. We need a PyTorch version of 1.0 or above for some of the other packages to work (especially the PySyft package, which we will be downloading later).

We are thus presented with different ways to achieve the installation of PyTorch:

  • The hard way — Compile PyTorch from source from the official Git repository: There is an excellent article that details this here, so I would rather not duplicate it in this article
  • The easy way — Install PyTorch from the pre-compiled wheel package, uploaded by some of the most helpful souls out there: This was my savior. I stumbled upon some of the wheel files uploaded by people who had hard time building the source from within Raspberry Pi just like me, and had decided to help people who’d lost hope! 😇 And that’s the best part of having an open source community I’d say! 👏

Let’s stick to the second option — I found a wheel file uploaded by someone while rummaging through GitHub for solutions. I have uploaded this here in my Google Drive (unfortunately, I couldn’t trace back to the source so I had uploaded the file myself once again). All thanks to the wonderful person who did it in the first place!

Wait a minute, I mentioned Wheel file. What’s that?

A WHL file is a package saved in the Wheel format, which is the standard built-package format used for Python distributions. It contains all the files for a Python install and metadata.

Now the next tricky aspect: The wheel file that I have uploaded works for armv7l version of processor architecture for Raspberry Pi.

There are versions of ARM architectures for different versions of Raspberry Pis. I, for instance have Raspberry Pi 2 B V1.1 and Raspberry Pi 3 B2 models. The later versions like Raspberry Pi 3 B+ or Raspberry Pi 4 would require aarch64 version, or older RPis might require armv6l version of the wheel file (As I mentioned previously, you need a Python package tailored/compiled for the specific architecture supported by your RPi).

Luckily, I was able to find Pytorch wheels for other processor architecture versions as well here (This requires Python 3.6 however and not 3.7) and here. Hats off again to all those helpful souls!

I will keep updating this article with the wheel files I get for different versions in future.

In order to check if the given wheel file (or any wheel file for Raspberry Pi for a specific Python package) is compatible with your RPi’s processor architecture, type in the following:

This would print the compatible version of processor architecture in terminal. If you see armv7l there, lucky you! Let’s go ahead with the installation then!


Installing PyTorch dependencies

Now that we have sorted out the wheel file required for your PyTorch installation, let’s install some of the dependencies required for a smooth and correct installation of PyTorch. From the terminal, type in the following (don’t get overwhelmed with the technical jargon, it takes a while for you to get used to this. It did, for me as well. 😊)


Copying the wheel file into Raspberry Pi

Now, there are several ways to accomplish this. You can upload this wheel file in a GitHub repository and perform a wget into that link so that it gets downloaded directly into your Raspberry Pi.

At the time of writing this article, I hadn’t tried this. Instead, I used the old school way of copying the wheel file into the RPi via a pendrive. I list down the steps for the same below:

  • Copy the wheel file onto your pendrive
  • Insert the pendrive into the USB slot of the RPi
  • From terminal type:

You should get the name of your pendrive when you type ls.

cd to the path where your file is present in the pendrive. To copy the file to say, the Documents directory inside RPi, type in :

Done! To check if copy has been performed successfully, go to Documents folder in RPi and peform an ls. You should see the wheel file copied.


Finally — Installing PyTorch

Perform the following:

It takes less than a minute for the installation to complete.

To test if installation has indeed worked, launch python3 and import torch.

If you get no errors, then congratulations! You have crossed yet another milestone in the journey to training a neural network through Raspberry Pis!

Phew 😅, that was a bit more involved than the previous article’s activities, isn’t it? Don’t worry, things might get a bit more involved in the coming articles, but I’m sure you will enjoy this learning as you progress into more complicated aspects of the project.

In the next article, I will take you through the steps to install yet another cool Python package called PySyft which is a library for performing secure, private deep learning (don’t get overwhelmed with the technical words). There are numerous articles on the fundamental concepts out there in the internet and several good fellows ready to help you in your journey 😊.

Curious to learn more about PyTorch? HERE is a really good introductory course on PyTorch, with full of hands-on coding exercises and detailed video tutorials. Also, let’s not forget the Official PyTorch site which hosts numerous tutorials and examples. Check it out HERE. Happy Coding! 😁 ☕And yeah, you earned that Coffee!

Secure and Private AI Writing Challenge

Articles and tutorials written by Secure and Private AI…

Suparna S Nair

Written by

An artist and tinkerer by passion, a software engineer by profession!

Secure and Private AI Writing Challenge

Articles and tutorials written by Secure and Private AI Writing Challenge Scholars

More From Medium

More from Secure and Private AI Writing Challenge

More from Secure and Private AI Writing Challenge

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade