Skip to content

Resolving "Could not build wheels for hnswlib" Error

Problem Statement

When installing Python packages that depend on hnswlib (like embeddinghub, chromadb, or other machine learning libraries), users frequently encounter this build error:

ERROR: Could not build wheels for hnswlib, which is required to install pyproject.toml-based projects

The core issue occurs during the compilation of hnswlib's C++ components. Key error patterns include:

  • macOS: clang: error: the clang compiler does not support '-march=native'
  • Windows: Missing Microsoft C++ build tools
  • Linux: Missing development headers (fatal error: Python.h: No such file or directory) or build tools

This error appears because:

  1. hnswlib requires compiling native C++ extensions
  2. Your system lacks necessary build dependencies
  3. Compiler flags may be incompatible with your architecture

The problem affects macOS (both Intel and Apple Silicon), Windows, and Linux systems, with variations in required solutions.

Platform-Specific Solutions

macOS Solutions

bash
# Set environment variable before installation
export HNSWLIB_NO_NATIVE=1
pip install your-package
bash
# Install command-line developer tools
xcode-select --install

# Verify installation
gcc --version
bash
# Explicitly set architecture for Intel chips
ARCHFLAGS="-arch x86_64" pip install your-package

WARNING

If using an M-series Mac, ensure you're using a Python version compiled for ARM architecture. The error clang: error: unsupported option '-march=native' often appears when compilers target incompatible architectures.

Windows Solutions

  1. Install Microsoft C++ Build Tools:
    • Download from Microsoft's official site
    • During installation:
      • Select "Desktop development with C++" workload
      • Include latest Windows 10/11 SDK
    • Reboot after installation

TIP

For conda users on Windows, this often resolves the issue:

bash
conda install -c conda-forge hnswlib

Linux Solutions (Ubuntu/Debian)

bash
sudo apt install python3.10-dev && sudo apt-get install build-essential -y
bash
sudo apt install python3.11-dev && sudo apt-get install build-essential -y
bash
# Install Python development headers
sudo dnf install python3-devel

# Install development tools
sudo dnf groupinstall "Development Tools"

Alternative Approaches

Using Conda

If pip installations persist in failing, use conda-forge to install precompiled binaries:

bash
conda install -c conda-forge hnswlib

Environment Variable Workarounds

Troubleshoot build issues by disabling native optimizations:

bash
# Windows Command Prompt
set HNSWLIB_NO_NATIVE=1

# PowerShell
$env:HNSWLIB_NO_NATIVE=1

# Linux/macOS
export HNSWLIB_NO_NATIVE=1

Version-Specific Fixes

  1. Python 3.11 Issues: Downgrade to Python 3.10 if encountering compatibility problems
  2. Chromadb Users: Pin to compatible version:
    bash
    pip install chromadb==0.3.25

Explanation of Solutions

The hnswlib package requires a C++ compiler and Python headers to build its optimized native modules. Solutions address:

  1. Missing Development Tools:

    • macOS: xcode-select provides Clang compiler
    • Windows: MSVC handles C++ compilation
    • Linux: build-essential and Python-dev include compiler tools and headers
  2. Architecture Mismatches:

    • HNSWLIB_NO_NATIVE=1 skips incompatible CPU optimizations
    • ARCHFLAGS forces correct architecture targeting
    • Conda provides pre-built binaries avoiding compilation
  3. Dependency Chains: Packages like chromadb require specific hnswlib versions. Using conda-forge resolves dependency conflicts with precompiled binaries.

Legacy Systems Warning

Older Linux distributions (e.g., RHEL 9) may require GCC upgrades. Use sudo dnf install gcc-toolset-12 if standard solutions fail.

Verifying Success

After applying the solution for your platform:

  1. Attempt the installation again
  2. Confirm successful compile:
    Successfully built hnswlib
    Installing collected packages: hnswlib, ...
  3. Validate import in Python:
    python
    import hnswlib
    print(hnswlib.__version__)  # Should return 0.5.2 or higher

If issues persist, consult hnswlib's GitHub issues for system-specific troubleshooting.