You can install a BLAS library when installing NumPy.

In this tutorial, you will discover how to install NumPy with a BLAS library in Python

Let’s get started.

Table of Contents

## What Is BLAS?

NumPy is an array library in Python.

It makes use of third-party libraries to perform array functions efficiently.

One example is the BLAS and LAPACK specifications used by NumPy to execute vector, matrix, and linear algebra operations.

The BLAS (Basic Linear Algebra Subprograms) are routines that provide standard building blocks for performing basic vector and matrix operations. The Level 1 BLAS perform scalar, vector and vector-vector operations, the Level 2 BLAS perform matrix-vector operations, and the Level 3 BLAS perform matrix-matrix operations. Because the BLAS are efficient, portable, and widely available, they are commonly used in the development of high quality linear algebra software, LAPACK for example.

— BLAS (Basic Linear Algebra Subprograms)

We may install one of many different libraries that implement the BLAS and LAPACK specifications.

Most libraries that implement BLAS also implement LAPACK because LAPACK is built on top of BLAS. Therefore, we often refer to them simply as BLAS libraries.

The benefit of BLAS libraries is that it makes specific linear algebra operations more efficient by using advanced algorithms and multithreading.

The NumPy linear algebra functions rely on BLAS and LAPACK to provide efficient low level implementations of standard linear algebra algorithms. Those libraries may be provided by NumPy itself using C versions of a subset of their reference implementations but, when possible, highly optimized libraries that take advantage of specialized processor functionality are preferred.

— Linear algebra (numpy.linalg), NumPy API.

You can learn more about BLAS and LAPACK in the tutorial:

## What Are Common BLAS Libraries?

BLAS is just a specification.

We must install a library that implements the specification.

Different libraries will have different capabilities and may optimize for different use cases or hardware.

Some libraries may be suited to your specific hardware. Most will detect the types of CPUs available and will tailor the compilation of the library to take best advantage of your hardware.

Additionally, most libraries offer configuration operations allowing the performance to be tailored, such as configuring the number of threads used.

Some of the more common BLAS libraries include:

- OpenBLAS
- ATLAS: Automatically Tuned Linear Algebra Software
- MKL: Math Kernel Library aka Intel MKL.
- Accelerate: aka Apple Accelerate or vecLib.
- BLIS: BLAS-like Library Instantiation Software Framework
- Eigen: C++ template library for linear algebra

You can learn more about different third-party BLAS libraries in the tutorial:

## How to Install Common BLAS Libraries

A BLAS library is typically installed automatically as a dependency when installing numpy.

As such, if we know how to install numpy, we can install BLAS.

You may have a BLAS library already installed on your system, and you can configure the installation of numpy to search for and use an existing BLAS library. If this is the case, you may have to check the documentation for your preferred package manager.

Below are details of how to install Numpy using a suite of common Python package managers and which BLAS library is installed by default.

### Install NumPy and OpenBLAS with pip

Pip is perhaps the most popular package manager for installing Python libraries.

Pip is a package-management system written in Python and is used to install and manage software packages. The Python Software Foundation recommends using pip for installing Python applications and its dependencies during deployment.

— pip (package manager), Wikipedia.

The default BLAS library installed when using pip to install numpy is OpenBLAS.

The NumPy wheels on PyPI, which is what pip installs, are built with OpenBLAS. The OpenBLAS libraries are included in the wheel. This makes the wheel larger, and if a user installs (for example) SciPy as well, they will now have two copies of OpenBLAS on disk.

— Installing NumPy, NumPy Documentation.

The following command will install numpy with pip

1 |
sudo pip install numpy |

This will compile the BLAS library and numpy from source for your system and is preferred.

You can force numpy to install a binary version of OpenBLAS as part of the numpy installation with the following command:

1 |
sudo pip install numpy --only-binary :numpy: numpy |

Alternatively, you can force Numpy to compile from source which will look for and use an existing BLAS library with the following command:

1 |
sudo pip install numpy --no-binary numpy |

This assumes that appropriate configuration or environmental variables have been set that specify where the BLAS library can be found.

… searches for BLAS and LAPACK dynamic link libraries at build time as influenced by environment variables NPY_BLAS_LIBS, NPY_CBLAS_LIBS, and NPY_LAPACK_LIBS; or NPY_BLAS_ORDER and NPY_LAPACK_ORDER; or the optional file ~/.numpy-site.cfg

— numpy.show_config API.

You can learn more about the pip numpy package hosted on PyPi here:

### Install NumPy and OpenBLAS with Homebrew

Homebrew is a popular program for installing Python libraries on macOS.

Homebrew is a free and open-source software package management system that simplifies the installation of software on Apple’s operating system, macOS, as well as Linux.

— Homebrew (package manager), Wikipedia.

The default BLAS library installed by Homebrew when installing numpy is OpenBLAS.

The following command will install numpy using homebrew:

1 |
sudo brew install numpy |

You can see this in the numpy formula here:

### Install NumPy and OpenBLAS or ATLAS with MacPorts

MacPorts is another popular Python package manager for macOS.

MacPorts, formerly DarwinPorts,[4] is a package manager for macOS and Darwin. It is an open-source software project that aims to simplify the installation of other open source software. It’s similar in function to Fink and the BSD ports collections.

— MacPorts, Wikipedia.

The default BLAS library installed with MacPorts when installing NumPy is OpenBLAS.

It can be installed as follows:

1 |
sudo port install py311-numpy |

NumPy can be installed with alternate BLAS libraries, specified via the “+” operator.

For example, the following will install Numpy for Python 3.11 using the ATLAS BLAS library.

1 |
sudo port install py311-numpy +atlas |

You can learn more about the Numpy macports package here:

### Install NumPy and MKL with Conda

Conda is a package manager popular on Windows and with the Anaconda Python ecosystem.

Conda is an open-source, cross-platform, language-agnostic package manager and environment management system. It was originally developed to solve difficult package management challenges faced by Python data scientists, and today is a popular package manager for Python and R. At first part of Anaconda Python distribution developed by Anaconda Inc., it ended up being useful on its own and for things other than Python, so it was spun out as a separate package, released under the BSD license.

— Conda (package manager), Wikipedia.

By default, conda will install numpy and the MKL (Math Kernel Library) BLAS library by Intel.

Perhaps this makes sense as conda is mostly used by Windows users and most Windows users are on Intel hardware.

In the conda defaults channel, NumPy is built against Intel MKL. MKL is a separate package that will be installed in the users’ environment when they install NumPy.

— Installing NumPy, NumPy Documentation.

You can install numpy with conda using the following command:

1 |
conda install -c anaconda numpy |

You can learn more about the numpy package provided by Conda here:

## What BLAS Library Should You Use?

OpenBLAS is a sensible default in most cases.

This is why it is installed by default with numpy by most package managers.

Performance gains come from the specific algorithms used and how well they can use multiple CPU cores and CPU-specific instructions to perform linear algebra operations.

MKL might offer better performance on Windows machines that have Intel CPUs.

Accelerate (vecLib) will likely offer better performance on macOS machines that have M1 CPUs.

Building the library from source during installation is preferred as it will likely result in better performance than installing a pre-compiled binary.

Multiple threads in a BLAS library may not be used until a matrix or vector reaches a minimum size.

GPUs may be used in some library implementations.

Library performance will vary by hardware and by specific operations. If you know what hardware you have and what operations you’re likely to perform the most, you may be able to choose a library that offers overall performance.

If in doubt, install each popular BLAS library in turn and benchmark the code you want to run.

The following provides some helpful benchmarks of BLAS libraries on different multicore hardware:

- Large BLAS Benchmarks (BLIS vs MKL vs OpenBLAS vs others)
- Small BLAS Benchmarks (BLIS vs MKL vs OpenBLAS vs others)
- Benchmarking BLAS libraries, 2018.
- BLAS libraries benchmarks, 2016.

**Overwheled by the python concurrency APIs?**

Find relief, download my FREE Python Concurrency Mind Maps

## Takeaways

You now know how to install NumPy with a BLAS library in Python.

**Do you have any questions?**

Ask your questions in the comments below and I will do my best to answer.

## Leave a Reply