Create a gist now

Instantly share code, notes, and snippets.

Embed
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

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)
@timonbimon

This comment has been minimized.

Show comment
Hide comment
@timonbimon

timonbimon 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. :)

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. :)

@briandw

This comment has been minimized.

Show comment
Hide comment
@briandw

briandw Apr 10, 2018

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

briandw commented Apr 10, 2018

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

@vfgeert

This comment has been minimized.

Show comment
Hide comment
@vfgeert

vfgeert 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?

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?
@stocyr

This comment has been minimized.

Show comment
Hide comment
@stocyr

stocyr 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.

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.

@jinyu121

This comment has been minimized.

Show comment
Hide comment
@jinyu121

jinyu121 Jun 7, 2018

If someone can read Chinese, here is also the solution https://haoyu.love/blog509.html

jinyu121 commented Jun 7, 2018

If someone can read Chinese, here is also the solution https://haoyu.love/blog509.html

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