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:
libblas-dev: Development files for BLAS libraryliblapack-dev: Development files for LAPACK librarylibatlas-base-dev: Optimized BLAS implementationgfortran: Fortran compiler (many mathematical libraries are written in Fortran)
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:
- System-level installation:
apt-get install python3-scipyinstalls pre-compiled binary packages containing all necessary system dependencies - Python-level installation:
pip install scipydownloads source code from Python Package Index and compiles locally, thus requiring system development libraries
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:
- Use virtual environments: Always install packages in virtual environments to avoid polluting system Python environment
- Check compiler versions: Ensure GCC and gfortran versions are compatible
- Examine detailed logs: Use
pip install scipy -vto get detailed compilation logs - 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.