Does Python SciPy need BLAS?
Asked Answered
D

7

184
numpy.distutils.system_info.BlasNotFoundError: 
    Blas (http://www.netlib.org/blas/) libraries not found.
    Directories to search for the libraries can be specified in the
    numpy/distutils/site.cfg file (section [blas]) or by setting
    the BLAS environment variable.

Which tar do I need to download off this site?

I've tried the fortrans, but I keep getting this error (after setting the environment variable obviously).

Derosa answered 21/9, 2011 at 8:11 Comment(1)
So the question is which is the right version for your system to download from some site, but neither system details nor the site are given in the question? It's not surprising that no answers were forthcoming.Inflated
B
143

The SciPy webpage used to provide build and installation instructions, but the instructions there now rely on OS binary distributions. To build SciPy (and NumPy) on operating systems without precompiled packages of the required libraries, you must build and then statically link to the Fortran libraries BLAS and LAPACK:

mkdir -p ~/src/
cd ~/src/
wget http://www.netlib.org/blas/blas.tgz
tar xzf blas.tgz
cd BLAS-*

## NOTE: The selected Fortran compiler must be consistent for BLAS, LAPACK, NumPy, and SciPy.
## For GNU compiler on 32-bit systems:
#g77 -O2 -fno-second-underscore -c *.f                     # with g77
#gfortran -O2 -std=legacy -fno-second-underscore -c *.f    # with gfortran
## OR for GNU compiler on 64-bit systems:
#g77 -O3 -m64 -fno-second-underscore -fPIC -c *.f                     # with g77
gfortran -O3 -std=legacy -m64 -fno-second-underscore -fPIC -c *.f    # with gfortran
## OR for Intel compiler:
#ifort -FI -w90 -w95 -cm -O3 -unroll -c *.f

# Continue below irrespective of compiler:
ar r libfblas.a *.o
ranlib libfblas.a
rm -rf *.o
export BLAS=~/src/BLAS-*/libfblas.a

Execute only one of the five g77/gfortran/ifort commands. I have commented out all, but the gfortran which I use. The subsequent LAPACK installation requires a Fortran 90 compiler, and since both installs should use the same Fortran compiler, g77 should not be used for BLAS.

Next, you'll need to install the LAPACK stuff. The SciPy webpage's instructions helped me here as well, but I had to modify them to suit my environment:

mkdir -p ~/src
cd ~/src/
wget http://www.netlib.org/lapack/lapack.tgz
tar xzf lapack.tgz
cd lapack-*/
cp INSTALL/make.inc.gfortran make.inc          # On Linux with lapack-3.2.1 or newer
make lapacklib
make clean
export LAPACK=~/src/lapack-*/liblapack.a

Update on 3-Sep-2015: Verified some comments today (thanks to all): Before running make lapacklib edit the make.inc file and add -fPIC option to OPTS and NOOPT settings. If you are on a 64bit architecture or want to compile for one, also add -m64. It is important that BLAS and LAPACK are compiled with these options set to the same values. If you forget the -fPIC SciPy will actually give you an error about missing symbols and will recommend this switch. The specific section of make.inc looks like this in my setup:

FORTRAN  = gfortran 
OPTS     = -O2 -frecursive -fPIC -m64
DRVOPTS  = $(OPTS)
NOOPT    = -O0 -frecursive -fPIC -m64
LOADER   = gfortran

On old machines (e.g. RedHat 5), gfortran might be installed in an older version (e.g. 4.1.2) and does not understand option -frecursive. Simply remove it from the make.inc file in such cases.

The lapack test target of the Makefile fails in my setup because it cannot find the blas libraries. If you are thorough you can temporarily move the blas library to the specified location to test the lapack. I'm a lazy person, so I trust the devs to have it working and verify only in SciPy.

Bohi answered 7/2, 2012 at 9:8 Comment(9)
There is no libfblas in blas.tgz though.Derosa
Worked for me, but I had to edit the make.inc file by setting OPTS = -O2 -fPIC and NOOPT = -O0 -fPIC.Sailcloth
@jdborg it is apparently called just liblapack.a nowCoit
I had to add the -m64 options to the OPTS and NOOPT settings in the make.inc file. Only of you are on a 64 bit machine of course.Specimen
To those that started yesterday to downvote, I'd like to know why. Downvotes should not be given just because you like another answer more than this. They should be given if this answer is factually incorrect or does not answer the question. Please clarify. Otherwise the downvotes are not helpful.Bohi
+1 Many thanks for this! Also, for scipy, I had to do export LAPACK=~/src/lapack-3.5.0/ instead of adding the libflapack.a in the end.Swinger
The scipy website no longer hosts these instructions so this recipe is invaluable. For those on a recent redhat/fedora/centos distro, the new procedure is sudo yum install -y "@Development Tools" gcc-gfortran blas-static lapack-static numpy-f2py numpy scipy && pip install --upgrade scipyHouppelande
I needed a shared library, so here's the command I used for libblas: gfortran -O3 -std=legacy -m64 -fno-second-underscore -fPIC -o libblas.so.3 -shared -Wl,-soname=libblas.so.3 *.fGerson
Note that at the beginning of the make process a select few files are compiled with different options. To compile those files correctly, edit make.inc and change to LOADER = gfortran -fPIC.Homerus
C
340

If you need to use the latest versions of SciPy rather than the packaged version, without going through the hassle of building BLAS and LAPACK, you can follow the below procedure.

Install linear algebra libraries from repository (for Ubuntu),

sudo apt-get install gfortran libopenblas-dev liblapack-dev

Then install SciPy, (after downloading the SciPy source): python setup.py install or

pip install scipy

As the case may be.

Cellarage answered 26/1, 2013 at 20:18 Comment(9)
This worked for me, tks! Although I had to change libopenblas-dev to libblas-dev (Ubuntu 10.04).Sundstrom
worked perfectly for me running Mint 13 ( precise fork ).You need to download the source separately, if you use easy_install -U #updateKenyatta
It worked flawlessly. I did not have to do anything different in my case (ubuntu 12.0.4)Eagre
I still got fatal error: Python.h: No such file or directoryGoofy
sudo apt-get install python-dev fixed it. https://mcmap.net/q/20731/-fatal-error-python-h-no-such-file-or-directoryGoofy
sudo yum install blas-devel lapack-devel worked for meProposal
sudo yum install blas-devel lapack-devel needed on RedHat or CentOS.Constipation
sudo pip3 install scipy (I'm on Ubuntu 15.04, Python 3.4.3)Busch
@kakarukeys Maybe because the question doesn't say anything about Ubuntu?Eat
B
143

The SciPy webpage used to provide build and installation instructions, but the instructions there now rely on OS binary distributions. To build SciPy (and NumPy) on operating systems without precompiled packages of the required libraries, you must build and then statically link to the Fortran libraries BLAS and LAPACK:

mkdir -p ~/src/
cd ~/src/
wget http://www.netlib.org/blas/blas.tgz
tar xzf blas.tgz
cd BLAS-*

## NOTE: The selected Fortran compiler must be consistent for BLAS, LAPACK, NumPy, and SciPy.
## For GNU compiler on 32-bit systems:
#g77 -O2 -fno-second-underscore -c *.f                     # with g77
#gfortran -O2 -std=legacy -fno-second-underscore -c *.f    # with gfortran
## OR for GNU compiler on 64-bit systems:
#g77 -O3 -m64 -fno-second-underscore -fPIC -c *.f                     # with g77
gfortran -O3 -std=legacy -m64 -fno-second-underscore -fPIC -c *.f    # with gfortran
## OR for Intel compiler:
#ifort -FI -w90 -w95 -cm -O3 -unroll -c *.f

# Continue below irrespective of compiler:
ar r libfblas.a *.o
ranlib libfblas.a
rm -rf *.o
export BLAS=~/src/BLAS-*/libfblas.a

Execute only one of the five g77/gfortran/ifort commands. I have commented out all, but the gfortran which I use. The subsequent LAPACK installation requires a Fortran 90 compiler, and since both installs should use the same Fortran compiler, g77 should not be used for BLAS.

Next, you'll need to install the LAPACK stuff. The SciPy webpage's instructions helped me here as well, but I had to modify them to suit my environment:

mkdir -p ~/src
cd ~/src/
wget http://www.netlib.org/lapack/lapack.tgz
tar xzf lapack.tgz
cd lapack-*/
cp INSTALL/make.inc.gfortran make.inc          # On Linux with lapack-3.2.1 or newer
make lapacklib
make clean
export LAPACK=~/src/lapack-*/liblapack.a

Update on 3-Sep-2015: Verified some comments today (thanks to all): Before running make lapacklib edit the make.inc file and add -fPIC option to OPTS and NOOPT settings. If you are on a 64bit architecture or want to compile for one, also add -m64. It is important that BLAS and LAPACK are compiled with these options set to the same values. If you forget the -fPIC SciPy will actually give you an error about missing symbols and will recommend this switch. The specific section of make.inc looks like this in my setup:

FORTRAN  = gfortran 
OPTS     = -O2 -frecursive -fPIC -m64
DRVOPTS  = $(OPTS)
NOOPT    = -O0 -frecursive -fPIC -m64
LOADER   = gfortran

On old machines (e.g. RedHat 5), gfortran might be installed in an older version (e.g. 4.1.2) and does not understand option -frecursive. Simply remove it from the make.inc file in such cases.

The lapack test target of the Makefile fails in my setup because it cannot find the blas libraries. If you are thorough you can temporarily move the blas library to the specified location to test the lapack. I'm a lazy person, so I trust the devs to have it working and verify only in SciPy.

Bohi answered 7/2, 2012 at 9:8 Comment(9)
There is no libfblas in blas.tgz though.Derosa
Worked for me, but I had to edit the make.inc file by setting OPTS = -O2 -fPIC and NOOPT = -O0 -fPIC.Sailcloth
@jdborg it is apparently called just liblapack.a nowCoit
I had to add the -m64 options to the OPTS and NOOPT settings in the make.inc file. Only of you are on a 64 bit machine of course.Specimen
To those that started yesterday to downvote, I'd like to know why. Downvotes should not be given just because you like another answer more than this. They should be given if this answer is factually incorrect or does not answer the question. Please clarify. Otherwise the downvotes are not helpful.Bohi
+1 Many thanks for this! Also, for scipy, I had to do export LAPACK=~/src/lapack-3.5.0/ instead of adding the libflapack.a in the end.Swinger
The scipy website no longer hosts these instructions so this recipe is invaluable. For those on a recent redhat/fedora/centos distro, the new procedure is sudo yum install -y "@Development Tools" gcc-gfortran blas-static lapack-static numpy-f2py numpy scipy && pip install --upgrade scipyHouppelande
I needed a shared library, so here's the command I used for libblas: gfortran -O3 -std=legacy -m64 -fno-second-underscore -fPIC -o libblas.so.3 -shared -Wl,-soname=libblas.so.3 *.fGerson
Note that at the beginning of the make process a select few files are compiled with different options. To compile those files correctly, edit make.inc and change to LOADER = gfortran -fPIC.Homerus
M
66

On Fedora, this works:

 yum install lapack lapack-devel blas blas-devel
 pip install numpy
 pip install scipy

Remember to install 'lapack-devel' and 'blas-devel' in addition to 'blas' and 'lapack' otherwise you'll get the error you mentioned or the "numpy.distutils.system_info.LapackNotFoundError" error.

Membership answered 8/3, 2013 at 3:56 Comment(5)
It says "public key for blas-dev.." not installed. :(Nitrate
@OlgaMu Use "yum --nogpgcheck install blas-devel"Membership
blas-static & lapack-static were required for me on fedora 20.Houppelande
Thank you! This worked for me on my AWS EC2 Amazon Linux machine.Pallette
This doesn't work for a virtualenvironment though, it is failing unless I use sudo for the pip install. Any ideas?Uranous
H
44

I guess you are talking about installation in Ubuntu. Just use:

apt-get install python-numpy python-scipy

That should take care of the BLAS libraries compiling as well. Else, compiling the BLAS libraries is very difficult.

Harriott answered 12/12, 2011 at 4:58 Comment(4)
@V3ss0n, building BLAS and LAPACK are notoriously difficult. It is much easier to compile and install kernels from scratch than compiling those two damned packages imho.Gainey
@Coder, I figured out how to do this on my Debian box. Should work for Ubuntu as well. check this out: ydevel.tumblr.com/post/37462965735/… [DISCLAIMER: I'm the author of the post]Kelila
@NicholasMancuso It is indeed difficult but I manage to build whole python (Relocatable) + Lapack + ATLAS (Better than BLAS) Scipy , Scikit, Maplotlib , NetworkX , and alot of other C Dependencies . My whole Runtime folder now 900 MB (Python , Perl , Java included too) haha,. But that makes my projects totally relocated with its own runtime in one folder. Let me know if you need it. Way a lot better than Virtualenv.Footmark
@yatisagade, @Coder, indeed, you need to be able to use pip install scipy or build it from source in a virtual environment.Parallelogram
J
12

For Windows users there is a nice binary package by Chris (warning: it's a pretty large download, 191 MB):

Jarvey answered 11/7, 2014 at 14:53 Comment(0)
U
2

Following the instructions given by 'cfi' works for me, although there are a few pieces they left out that you might need:

1) Your lapack directory, after unzipping, may be called lapack-X-Y (some version number), so you can just rename that to LAPACK.

cd ~/src
mv lapack-[tab] LAPACK

2) In that directory, you may need to do:

cd ~/src/LAPACK 
cp lapack_LINUX.a libflapack.a
Urnfield answered 8/4, 2012 at 20:51 Comment(0)
I
-2

Try using

sudo apt-get install python3-scipy
Impose answered 23/10, 2014 at 1:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.