Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save krmerchant/8499de2907989154ce5d57f0063ec4b4 to your computer and use it in GitHub Desktop.
Save krmerchant/8499de2907989154ce5d57f0063ec4b4 to your computer and use it in GitHub Desktop.
How to install PyTorch v0.3.1 on RaspberryPi 3B - Tutorial

How to install PyTorch v0.3.1 on RaspberryPi 3B

Prerequisites

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 https://www.raspberrypi.org/downloads/raspbian/ and download the Raspbian Stretch image (either one).

Use Etcher (from https://etcher.io/) 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
ssh RASPBERRYPI.LOCAL -l pi

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:

# ONLY USE THIS IF YOU HAVE AN EXISTING SWAP FILE:
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

export NO_DISTRIBUTED=1
# 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 https://github.com/pytorch/pytorch
cd pytorch

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

python3 setup.py build

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

sudo -E python3 setup.py 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

cd
python3
>>> import torch
>>> x = torch.rand(5, 3)
>>> print(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment