Complete Guide to Resolving BLAS Library Missing Issues During pip Installation of SciPy

Nov 25, 2025 · Programming · 10 views · 7.8

Keywords: SciPy installation | BLAS libraries | pip dependency management | Linux development environment | Python scientific computing

Abstract: This article provides a comprehensive analysis of the BLAS library missing error encountered when installing SciPy via pip, offering complete solutions based on best practice answers. It first explains the core role of BLAS and LAPACK libraries in scientific computing, then provides step-by-step guidance on installing necessary development packages and environment variable configuration in Linux systems. By comparing the differences between apt-get and pip installation methods, it delves into the essence of dependency management and offers specific methods to verify successful installation. Finally, it discusses alternative solutions using modern package management tools like uv and conda, providing comprehensive installation guidance for users with different needs.

Problem Background and Error Analysis

In the Python scientific computing ecosystem, SciPy and NumPy are two core numerical computing libraries. Many developers need to explicitly specify these dependencies in their setup.py when packaging their own Python packages. While using system package managers like apt-get can install them smoothly, switching to pip installation often encounters BLAS library missing errors.

The specific error message shows: numpy.distutils.system_info.BlasNotFoundError: Blas libraries not found. The root cause of this error is that SciPy needs to link against underlying mathematical libraries during compilation, and the pip installation method does not handle these system-level dependencies by default.

Core Role of BLAS and LAPACK Libraries

BLAS (Basic Linear Algebra Subprograms) and LAPACK (Linear Algebra PACKage) are fundamental libraries for numerical linear algebra computations. They provide efficient implementations of matrix operations, linear system solving, eigenvalue computations, and other core functionalities. Many modules in SciPy, particularly scipy.linalg and scipy.optimize, heavily depend on these underlying libraries to achieve high-performance mathematical operations.

Unlike installing pre-compiled versions directly through system package managers, pip install scipy attempts to compile from source, which requires pre-installation of development versions of these mathematical libraries in the system.

Complete Solution

Based on best practices, resolving BLAS library missing issues requires three key steps:

1. Install System Dependency Packages

In Ubuntu or Debian systems, the following development packages need to be installed:

sudo apt-get install libblas-dev liblapack-dev libatlas-base-dev gfortran

These packages provide respectively:

2. Configure Environment Variables and Compilation Options

In some cases, it may be necessary to manually specify library search paths. This can be achieved by setting environment variables:

export BLAS=/usr/lib/libblas.so
export LAPACK=/usr/lib/liblapack.so

Alternatively, specify library paths by editing NumPy's configuration file numpy/distutils/site.cfg:

[blas]
libraries = blas
library_dirs = /usr/lib
include_dirs = /usr/include

[lapack]
libraries = lapack
library_dirs = /usr/lib
include_dirs = /usr/include

3. Install Fortran-related Tools

Since many scientific computing libraries are written in Fortran, ensure necessary Fortran tools are installed:

sudo apt-get install gfortran python-numpy-f2py

numpy-f2py is a Fortran to Python interface generator provided by NumPy, which is crucial for compiling certain SciPy modules.

Verifying Successful Installation

After completing the above steps, attempt to install SciPy:

pip install scipy==0.9.0

After installation completes, verify through Python interactive environment:

python -c "import scipy, numpy; print(f'SciPy version: {scipy.__version__}'); print(f'NumPy version: {numpy.__version__}')"

Also test basic linear algebra functionality:

import scipy.linalg
import numpy as np

# Create a random matrix
A = np.random.rand(3, 3)
# Compute eigenvalues
eigenvalues = scipy.linalg.eigvals(A)
print(f"Eigenvalues: {eigenvalues}")

Alternative Solutions with Modern Package Management Tools

Beyond traditional pip installation, modern Python package management tools offer more convenient solutions:

Using uv for Project Management

uv is a fast Python package manager that automatically handles dependencies and environments:

# Install uv
curl -LsSf https://astral.sh/uv/install.sh | sh

# Create new project
uv init my-science-project
cd my-science-project

# Add SciPy dependencies
uv add scipy numpy

# Run Python
uv run python

Using conda Environment Management

Conda can better handle binary dependencies, avoiding compilation issues:

# Create new environment
conda create -n science-env python=3.9
conda activate science-env

# Install SciPy
conda install scipy numpy

Deep Understanding of Dependency Management

Understanding the fundamental differences between apt-get and pip installation methods is crucial:

When packaging your own Python package, explicitly declare dependencies in setup.py:

from setuptools import setup, find_packages

setup(
    name="my-package",
    version="0.1.0",
    packages=find_packages(),
    install_requires=[
        "numpy>=1.5.1",
        "scipy>=0.9.0",
    ],
    python_requires=">=3.6",
)

Troubleshooting and Best Practices

If compilation issues persist, consider the following strategies:

  1. Use virtual environments: Always install packages in virtual environments to avoid polluting system Python environment
  2. Check compiler versions: Ensure GCC and gfortran versions are compatible
  3. Examine detailed logs: Use pip install scipy -v to get detailed compilation logs
  4. Consider using Docker: Build in container environments to ensure environment consistency

By understanding underlying dependency relationships and adopting correct installation strategies, various issues during SciPy installation can be effectively resolved, laying a solid foundation for scientific computing projects.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.