libblas and liblapack issues and speed, with SciPy and Ubuntu

December 19, 2012 | categories: Python, Programming | View Comments

The problem

You've built from source a package that uses the linear algebra libraries BLAS and LAPACK, but now you're getting:

libatlas.so.3: cannot open shared object file: No such file or directory

In my case, I was trying to install SciPy from source using pip install scipy, but then ran into this issue when I tried to use it. I also ran into this other problem:

ImportError: scipy/linalg/clapack.so: undefined symbol: clapack_sgesv

To fix it

  1. Make sure you've got libatlas3-base installed:

    sudo apt-get install libatlas3-base
    
  2. Make sure you're using the right implementation of libblas.so.3, that is, the one that libatlas3-base provides. This implementation lives in /usr/lib/atlas-base/atlas/libblas.so.3.

    To do this, run the following command to set your system's default Atlas implementation:

    sudo update-alternatives --config libblas.so.3
    

    An example of what this might produce, and what number you might need to enter:

    Selection      Path                                     Priority   Status
    ------------------------------------------------------------
    * 0            /usr/lib/openblas-base/libopenblas.so.0   40        auto mode
      1            /usr/lib/atlas-base/atlas/libblas.so.3    35        manual mode
      2            /usr/lib/libblas/libblas.so.3             10        manual mode
      3            /usr/lib/openblas-base/libopenblas.so.0   40        manual mode
    
     Press enter to keep the current choice[*], or type selection number: 1
    
  3. Use liblapack3.so.3 from /usr/lib/atlas-base/atlas/liblapack.so.3 as your default:

    sudo update-alternatives --config liblapack.so.3
    

That's it. Now you shouldn't be getting the aforementioned errors anymore.

More information

The Debian Wiki has an overview of linear algebra libraries available, and it also describes how to use update-alternatives to switch between BLAS and LAPACK implementations.

Speed it up! Build an optimized Atlas for your architecture.

The README.Debian file of libatlas3-base explains:

Building your own optimized packages of Atlas is straightforward. Just get the sources of the package and its build-dependencies:

# apt-get source atlas
# apt-get build-dep atlas
# apt-get install devscripts dpkg-dev dch

and type the following from the atlas source subdir:

# fakeroot debian/rules custom

it should produce a package called:

../libatlas3-base_*.deb

which is optimized for the architecture Atlas has been built on. Then install the package using dpkg -i.

If you, like me, get this "classical" error when building Atlas:

VARIATION EXCEEDS TOLERENCE, RERUN WITH HIGHER REPS

then you'll need to read this: Your install dies with "unable to get timings in tolerance".

Lastly, you might need to remove any existing libopenblas installation from your system for the building of the Debian package to finish successfully:

sudo aptitude purge libopenblas-{base,dev}