TL;DR: skip to the last paragraph to know how to increase swap space and avoid memory clogging. Also, don't use pip for numpy and scipy.
To get the latest version of scipy on the raspberry Pi you need to build from source. The main reason is that scipy relies on compiled C and fortran libraries, that need to be compiled on the same architecture. Usually
pip install would fetch prebuilt packages for you, but Raspberry Pi's ARM architecture is not really supported.
If this was all you needed it would be relatively easy (main instructions can be found here and are summarized below), but there is an additional problem with the raspberry pi that I realized only after a week of research and trial and error (skip at the end if you just need the fix), which is due, I believe, to the small amount of memory available.
I'll just try and summarize the whole process (so that people don't have to jump around the internet like I did).
There are a few requirements for building scipy. As far as python packages go, you'll need
setuptools, and (if you want to build the documentation)
You should try and use the version of these packages that comes with Raspbian (in packages such as
python3-numpy), but they might need to be built separately (OT).
As far as system requirements go, you'll need a few packages that can be installed with
- A BLAS/LAPACK math library with development headers, e.g.
- C and Fortran compilers,
Finally you need the source code, that you can download from here (Scipy 1.0.0 is the latest stable version as I am writing).
Then it's just a matter of
tar -xzvf scipy-v1.0.0.tar.gz
Compiling the source
At this point, if you start the build process, it will seem to go fine, but it will hang after a few minutes.
Adding bigger swap space
This is due to the compiling script occupying the totality of both RAM and swap memory by spawning multiple processes (and replicating memory by consequence). The problem is that in the Raspberry Pi the swap space is particularly small (only 100MB I think), while the norm would be to have it the same size of your RAM.
As explained here and here, swap space can be increased typing the following:
sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
sudo /sbin/mkswap /var/swap.1
sudo chmod 600 /var/swap.1
sudo /sbin/swapon /var/swap.1
which will give you 1GB of swap space.
Then one can finally build and install with
python3 setup.py build
python3 setup.py install --user
(you can drop the
--user flag if you want to install it system-wide, but you'll need root privilege).
Finally, one remove the extra swap and restore the default:
sudo swapoff /var/swap.1
sudo rm /var/swap.1