Building NumPy from source requires setting up system-level dependencies
(compilers, BLAS/LAPACK libraries, etc.) first, and then invoking a build. The
build may be done in order to install NumPy for local usage, develop NumPy
itself, or build redistributable binary packages. And it may be desired to
customize aspects of how the build is done. This guide will cover all these
aspects. In addition, it provides background information on how the NumPy build
works, and links to up-to-date guides for generic Python build & packaging
documentation that is relevant.
System-level dependencies
NumPy uses compiled code for speed, which means you need compilers and some
other system-level (i.e, non-Python / non-PyPI) dependencies to build it on
your system.
Note
If you are using Conda, you can skip the steps in this section - with the
exception of installing compilers for Windows or the Apple Developer Tools
for macOS. All other dependencies will be installed automatically by the
mambaenvcreate-fenvironment.yml command.
Linux
If you want to use the system Python and pip, you will need:
C and C++ compilers (typically GCC).
Python header files (typically a package named python3-dev or
python3-devel)
BLAS and LAPACK libraries. OpenBLAS
is the NumPy default; other variants include Apple Accelerate,
MKL,
ATLAS and
Netlib (or “Reference”)
BLAS and LAPACK.
pkg-config for dependency detection.
A Fortran compiler is needed only for running the f2py tests. The
instructions below include a Fortran compiler, however you can safely
leave it out.
This command installs whatever is needed to build NumPy, with the
advantage that new dependencies or updates to required versions are
handled by the package managers.
This command installs whatever is needed to build NumPy, with the
advantage that new dependencies or updates to required versions are
handled by the package managers.
This command installs whatever is needed to build NumPy, with the
advantage that new dependencies or updates to required versions are
handled by the package managers.
Arch
To install NumPy build requirements, you can do:
sudopacman-Sgcc-fortranopenblaspkgconf
macOS
Install Apple Developer Tools. An easy way to do this is to
open a terminal window,
enter the command:
xcode-select--install
and follow the prompts. Apple Developer Tools includes Git, the Clang C/C++
compilers, and other development utilities that may be required.
Do not use the macOS system Python. Instead, install Python
with the python.org installer or
with a package manager like Homebrew, MacPorts or Fink.
On macOS >=13.3, the easiest build option is to use Accelerate, which is
already installed and will be automatically used by default.
On older macOS versions you need a different BLAS library, most likely
OpenBLAS, plus pkg-config to detect OpenBLAS. These are easiest to install
with Homebrew:
brewinstallopenblaspkg-
Windows
Compared to macOS and Linux, building NumPy on Windows is a little more
difficult, due to the need to set up compilers. First, install Microsoft
Visual Studio - the 2019 Community Edition or any newer version will work
(see the
Visual Studio download site).
This is needed to ensure you have the Windows Universal C Runtime. The
recommended version of the UCRT is >= 10.0.22621.0.
The MSVC installer does not put the compilers on the system path, and
the install location may change. To query the install location, MSVC
comes with a vswhere.exe command-line utility. And to make the
C/C++ compilers available inside the shell you are using, you need to
run a .bat file for the correct bitness and architecture (e.g., for
64-bit Intel CPUs, use vcvars64.bat).
If you don’t need f2py, MSVC alone is sufficient. For Fortran compiler
setup (needed for f2py), see F2PY and Windows.
Windows on ARM64
In Windows on ARM64, the compiler options available for building NumPy are
limited. GCC and gfortran are not yet supported. Currently, the NumPy build
for Windows on ARM64 is supported with MSVC and LLVM toolchains.
First, install Microsoft Visual Studio - the 2022 Community Edition will
work (see the Visual Studio download site).
Ensure that you have installed necessary Visual Studio components for building NumPy
on WoA from here.
The MSVC installer does not put the compilers on the system path, and
the install location may change. To query the install location, MSVC
comes with a vswhere.exe command-line utility. And to make the
C/C++ compilers available inside the shell you are using, you need to
run a .bat file for the correct bitness and architecture (e.g., for
ARM64-based CPUs, use vcvarsarm64.bat).
If you don’t need f2py, MSVC alone is sufficient. For Fortran compiler
setup (needed for f2py), see .
Building NumPy from source
If you want to only install NumPy from source once and not do any development
work, then the recommended way to build and install is to use pip.
Otherwise, conda is recommended.
Building from source to use NumPy
Conda env
If you are using a conda environment, pip is still the tool you use to
invoke a from-source build of NumPy. It is important to always use the
--no-build-isolation flag to the pipinstall command, to avoid
building against a numpy wheel from PyPI. In order for that to work you
must first install the remaining build dependencies into the conda
environment:
# Either install all NumPy dev dependencies into a fresh conda environmentmambaenvcreate-fenvironment.yml# Or, install only the required build dependenciesmambainstallpythonnumpycythoncompilersopenblasmeson-pythonpkg-config# To build the latest stable release:pipinstallnumpy--no-build-isolation
Virtual env or system Python
# To build the latest stable release:pipinstallnumpy--no-binarynumpy# To build a development version, you need a local clone of the NumPy git repository:gitclonehttps://github.com/numpy/numpy.gitcdnumpygitsubmoduleupdate--initpipinstall.
Building from source for NumPy development
If you want to build from source in order to work on NumPy itself, first clone
the NumPy repository:
Compilers should be on the system path (i.e., the PATH environment
variable should contain the directory in which the compiler executables
can be found) in order to be found, with the exception of MSVC which
will be found automatically if and only if there are no other compilers
on the PATH. You can use any shell (e.g., Powershell, cmd or
Git Bash) to invoke a build.
F2PY and Windows
--
no
-
binary
numpy
# To build a development version, you need a local clone of the NumPy git repository:
git
clone
https
:
//
github
.
com
/
numpy
/
numpy
.
git
cd
numpy
git
submodule
update
--
init
pip
install
.
--
no
-
build
-
isolation
optional
Build NumPy with the spin developer interface.
Step (3) is always the same, steps (1) and (2) are different between conda and
virtual environments:
Conda env
To create a numpy-dev development environment with every required and
optional dependency installed, run:
Create and activate a virtual environment in a new directory named venv (
note that the exact activation command may be different based on your OS and shell
- see “How venvs work”
in the venv docs).
Linux
python-mvenvvenvsourcevenv/bin/activate
macOS
python-mvenvvenvsourcevenv/bin/activate
Windows
Then install the Python-level dependencies from PyPI with:
To build NumPy in an activated development environment, run:
spinbuild
This will install NumPy inside the repository (by default in a
build-install directory). You can then run tests (spintest),
drop into IPython (spinipython), or take other development steps
like build the html documentation or running benchmarks. The spin
interface is self-documenting, so please see spin--help and
spin<subcommand>--help for detailed guidance.