NMSLIB (Non-Metric Space Library) is an efficient cross-platform similarity search library with Python bindings, designed for approximate nearest neighbor search in high-dimensional and non-metric spaces. It is widely used for vector similarity tasks, often in conjunction with embeddings from libraries like Gensim. The current version is 2.1.2, and the library receives updates focused on performance, new features, and broader platform/Python support.
pip install nmslibVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize an HNSW index, add a batch of data points (random float32 vectors), build the index, and perform a k-nearest neighbor query. It uses `numpy` for data generation and `nmslib` for indexing and querying. The `post` parameter in `createIndex` is a common HNSW tuning parameter.
Upgrade to `nmslib>=2.1.1` by running `pip install --upgrade nmslib`.
Install with `pip install --no-binary :all: nmslib`. This requires C++ development tools (e.g., `python3-dev` on Ubuntu, Visual Studio 2019+ on Windows) and potentially CMake for full features.
Review `save_data` and `load_data` parameters to avoid duplication. If `getDistance` is needed, ensure data is either loaded with `load_data=True` or added separately to the loaded index.
If installation fails, try `pip install --use-pep517 nmslib` or install from source directly via `python -m pip install .` from the `python_bindings` directory of the cloned repository.
Ensure you have the necessary C++ build tools (e.g., `python3-dev` on Linux, Xcode command line tools on macOS, Visual Studio 2019 or later on Windows). For Windows, ensure your Visual Studio installation matches the Python version's build requirements. Consider installing from source using `pip install --no-binary :all: nmslib` after installing build tools, or directly from a cloned Git repository.
Update your C++ compiler (e.g., GCC to version 4.7+, Clang to 3.4+, Visual Studio to 2015/VC14 or later). On Linux, ensure `g++` is up-to-date. On macOS, ensure Xcode command-line tools are installed and updated (`xcode-select --install`).
This often requires patching the NMSLIB source or using a specific compiler version. Check the NMSLIB GitHub issues for workarounds or newer releases that address this specific build problem. Building from source with specific compiler flags or a different compiler environment (e.g., via `conda` with a known working C++ toolchain) might be necessary.
Ensure you are using the latest stable version of NMSLIB (2.1.2 or newer) which often includes fixes for Python compatibility. If the issue persists, review GitHub issues for specific patches or temporary workarounds related to your Python version.