Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Installing PyTorch on
the Raspberry Pi
by Emmet  Published May 17, 2023

 Beginner    

In this project, we will be showing you how to


install PyTorch on your Raspberry Pi.

Pytorch is a machine-learning library


developed by Meta and is based on the Torch
library.

You can use this library on your Raspberry Pi


for natural language processing and computer
vision.

Best of all, you can get decent performance


from PyTorch with the Raspberry Pi, especially
if you are using newer releases such as the Pi
4.

The only requirement of running Pytorch on


your device is that you must be running a 64-
bit operating system.

Over the following sections, we will show you


how easy it is to install and use Pytorch on your
Raspberry Pi.

Equipment

Below is a list of the equipment we used when


we installed PyTorch on the Raspberry Pi.

Recommended
 Raspberry Pi

 Micro SD Card

 Power Supply

 Ethernet Cable or Wi-Fi

Optional
 Raspberry Pi Case

 USB Mouse

 USB Keyboard

 HDMI Cable

 Monitor

We last tested this tutorial on a Raspberry Pi


400 that was running the latest version of
Raspberry Pi OS Bullseye

Preparing your Raspberry


Pi for PyTorch

Before installing PyTorch, we must ensure that


we have several packages already installed to
our system. The main one here is Python itself.

Remember that you must be running a 64-bit


operating system to install PyTorch onto the
Raspberry Pi. So now is the best time to switch
over if you aren’t running a 64-bit system and
want to run PyTorch.

1. Your first step is to update the systems


package list and upgrade any out-of-date
packages.

You can perform both tasks by running the


following straightforward commands in the
terminal.

Terminal $  Copy

sudo apt update


sudo apt upgrade -y

2. Once the upgrade completes, we can


ensure we have both Python and its package
manager called pip installed to our system.

We will use “ pip ” later on in this guide to


install PyTorch to your system.

Terminal $  Copy

sudo apt install python3 pip3

3. You can ensure you have Python installed


using the command below.

Terminal $  Copy

python3 --version

If everything is working correctly, you should


see a message similar to the one below shown
within the terminal.

Output

Python 3.9.2

Installing PyTorch to the


Raspberry Pi

At this point, we now have the Raspberry Pi set


up and ready to install and run PyTorch.

In this section, we will install PyTorch and any


other Python packages that it requires to
operate.

4. Installing PyTorch to your Raspberry Pi is as


straightforward as using the command below in
the terminal.

We install the main part of Torch as well as its


vision and audio libraries.

sudo pip3 install torch torchvision

Please note that depending on your internet


speed, this process can take a couple of
minutes as PyTorch relies on several other
Python libraries.

5. We also need to ensure that we have the


latest version of ‘ numpy ‘ installed on your
device.

Use the following command within your


terminal to ensure the latest version is
available.

sudo pip3 install numpy --upgrade

Testing PyTorch is Working

With PyTorch now installed on our Raspberry


Pi, we can now move on to testing that
everything is working as intended.

6. Once the installation process completes, we


can now check to verify it has been installed
correctly.

Launch the Python interpreter by typing the


command below within the terminal.

Terminal $  Copy

python3

7. With the Python interpreter now opened, we


can start by importing the Torch package into
the current session.

To import PyTorch, all you need to do is type “


import ” followed by “ torch “.

Python >  Copy

import torch

8. If you imported PyTorch successfully, we


can now grab the version by using the following
line.

This line grabs the “ __version__ ” variable


specified within the Torch library.

Python >  Copy

torch.__version__

If everything is working correctly, you should


see the following line appear within the
terminal.

With this line, you can see that we are currently


running PyTorch 2.0.1 on our Raspberry Pi.

Python > Output

'2.0.1'

9. Finally, lets ensure no errors will be thrown


when we go to actually run a PyTorch function.

Use the line shown below to quickly test that no


errors will occur when we go to use PyTorch.

Python >  Copy

torch.Tensor([0, 0, 0])

Below is the expected output after running the


line above within the interpreter.

Python > Output

tensor([0., 0., 0.])

10. If everything has worked properly, you can


quit Python using the command below.

Python >  Copy

quit()

Conclusion

At this point in the tutorial, you should now


have successfully installed PyTorch on your
Raspberry Pi.

PyTorch is a library for Python that allows you


to run various machine-learning models on
your device.

Please comment below if you have issues


getting this library to work on your Pi.

If you found this tutorial helpful, be sure to


check out our many other Raspberry Pi
projects.

Weekly Updates Straight To Your


Inbox
Receive our Raspberry Pi projects, coding
tutorials, Linux guides and more!

Email*

Subscribe

Recommended

Configuring I2C on Using Two-Factor


the Raspberry Pi Authentication for
SSH

How to Open the Setting up Parsec


Terminal on Ubuntu on the Raspberry Pi

You might also like