Instantly share code, notes, and snippets.

What would you like to do?
How to install PyTorch v0.3.1 on RaspberryPi 3B - Tutorial

How to install PyTorch v0.3.1 on RaspberryPi 3B


Important I'd recommend you use at least a 16GB sd card. I tried it with an 8GB card and it baaaaarely fits. I had to uninstall a lot of packages and regularly clean up.

Go to and download the Raspbian Stretch image (either one).

Use Etcher (from to "burn" the image onto the SD card.

Before ejecting the SD card: if you want to use the Raspberry Pi (RPi from here on out) headlessly (without screen plugged in), you can just create an empty text file with the filename ssh on the boot volume of the SD card. This will turn on the ssh server and delete the file on next boot.

Login to your RPi with

# password is raspberry

Update everything

sudo apt-get update && sudo apt-get upgrade

Set up SWAP and dependencies

Create a SWAP file of size 2GB

sudo dd if=/dev/zero of=/swap1 bs=1M count=2048

Make this empty file into a swap-compatible file

sudo mkswap /swap1

Then disable the old swap space and enable the new one

sudo nano /etc/fstab

This above command will open a text editor on your /etc/fstab file. You may already have a swap file. If this is the case, your fstab will have a line that looks like this:

/swap0 swap swap

If you have an existing SWAP file

In this line, please change the /swap0 to /swap1.

If you don't have an existing SWAP file

Add the following line at the end of the /etc/fstab file:

/swap1 swap swap

In both cases

Then save the file with CTRL+o and ENTER. Close the editor with CTRL+x.

Now your system knows about the new swap space, and it will change it upon reboot, but if you want to use it right now, without reboot, you can manually turn off and empty the old swap space and enable the new one:

sudo swapoff /swap0

sudo swapon /swap1

Install required packages

sudo apt-get install libopenblas-dev cython libatlas-dev \
m4 libblas-dev python3-dev cmake python3-yaml

Get the PyTorch code and compile

Export compilation flags

export NO_CUDA=1
# this will disable CUDA components of PyTorch,
# because the little RaspberriPi doesn't have a
# GPU that supports CUDA

# disable distributed computing...
# unless you want to glue like 100 RPis together
# and make a cluster or something

Move into a download directory (let's say ~/Downloads) and clone the pytorch repo:

cd ~/Downloads
git clone --recursive
cd pytorch

Start the build process and grab a coffee - this took me about 3 hours.

python3 build

Once this finished (make sure there are no error of course) install the whole shebang

sudo -E python3 install
# the -E is important here to preserve
# the environmental variables (NO_DISTRIBUTED and NO_CUDA)

Et voila, this should have worked without any errors. To test the installation, please change into a different directory first

>>> import torch
>>> x = torch.rand(5, 3)
>>> print(x)

This comment has been minimized.

timonbimon commented Apr 8, 2018

Thanks for the tutorial! Before I go watch LOTR for 3 hours and come back to a failed installation, can you tell me: is setting up the SWAP necessary? You never mention why you added that part before the installation. :)


This comment has been minimized.

briandw commented Apr 10, 2018

Have you seen this error when building:
Gloo can only be built on 64-bit systems.


This comment has been minimized.

vfgeert commented Apr 25, 2018

  • @timonbimon The swap is necessary because otherwise you'll get the error "virtual memory exhausted: cannot allocate memory."
  • @briandw Did you do "export NO_DISTRIBUTED=1" beforehand like instructed?

This comment has been minimized.

stocyr commented May 28, 2018

I had to apply the patch for issue pytorch/pytorch#4472 as well using git cherry-pick -n af8b64a
Working on a RaspberryPi 3B+ with 2018-04-18-raspbian-stretch and also used the dphys-swap system instead of /etc/fstab.


This comment has been minimized.

jinyu121 commented Jun 7, 2018

If someone can read Chinese, here is also the solution


This comment has been minimized.

manoharvhr commented Sep 5, 2018

Hi, does this work on PYNQ Z1 boards as well?


This comment has been minimized.

manoharvhr commented Sep 14, 2018 used this and it worked like a charm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment