Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Install pyTorch in Raspberry Pi 4 (or any other)

Install the prerequisites (the last one for numpy):

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

Increase the swap size:

  • Stop the swap : sudo dphys-swapfile swapoff
  • Modify the size of the swap by editing as root the following file : /etc/dphys-swapfile. Modify the valiable CONF_SWAPSIZE and change its value to CONF_SWAPSIZE=2048
  • Run following from command prompt: dphys-swapfile setup to update the changes.
  • Start the swap back again: sudo dphys-swapfile swapon
  • Check if the changes have taken place: free -m

For raspberry pi 4 there may be an issue with the Gcc and G++ version. Install an older version ans use it

apt-get install gcc-4.9 g++-4.9

Set the environment variables:

  export CC=gcc-4.9
  export CXX=g++-4.9
  export USE_CUDA=0
  export USE_MKLDNN=0
  export USE_NNPACK=0
  export USE_QNNPACK=0
  export USE_NUMPY=1
  export NO_CUDA=1
  export NO_MKLDNN=1 
  export NO_NNPACK=1
  export NO_QNNPACK=1
  export ONNX_ML=1 ## this is extremely important otherwise you will run into onnx_pb.h error. 
  # other workaround is edit the onnx_pb.h file and add #define ONNX_ML 1 inside it to skip

Download pyTorch:

git clone --recursive
cd pytorch
  • Optionally if you want to install a specific branch e.g. I want to install v1.0.1 because the torchjit is not broken in that branch:
git checkout v1.0.1

Build torch:

sudo -E python3 build
sudo -E python3 install

Life Hack : Always build a wheel and keep it somewhere safe because you have already build from source.

sudo -E python3 bdist_wheel

The .whl file will be in pytorch/dist folder.

There can be multiple errors. The most irritating one is `fatal error: onnx/onnx.pb.h: No such file or directory

compilation terminated.This is a [bug]( which is recent. That is why it is imperative to set the flagONNX_ML=, this bypasses the onnx_pb.hfile search. The other hard coded way would be to manually edit the fileonnx_pb.hand set# DEFINE ONNX_ML 1` at the beginning. This is a hack, other features might break because of this.

Everytime you face an error do python3 clean --all to clean up the corrupted builds.

If you face some procol buffer problem, do this git submodule update --remote third_party/protobuf (pytorch/pytorch#22564 (comment))

Check successful installation:

>>> import torch
>>> import numpy as np
>>> a = torch.from_numpy(np.random.randn(1, 100))
>>> print(a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment