If you’re working on large-scale similarity search or machine learning tasks involving nearest neighbor search, you’ve probably heard of Faiss (Facebook AI Similarity Search). Developed by Facebook AI Research, Faiss is a powerful open-source library designed for efficient similarity search and clustering of dense vectors, especially when dealing with high-dimensional data.
Whether you’re building a recommendation engine, search engine, or large-scale NLP application, Faiss can be a game-changer. But one common question many developers and data scientists ask is:
“How do I install Faiss on Linux?”
In this comprehensive guide, we’ll walk you through various ways to install Faiss on Linux in 2025 — whether you’re using pip, conda, or building from source. This guide is beginner-friendly, up to date, and includes tips for troubleshooting and optimizing your installation.
What Is Faiss and Why Use It?
Faiss is a library that allows for fast nearest neighbor search in high-dimensional spaces. It is widely used in natural language processing (NLP), computer vision, and recommendation systems.
Key features of Faiss:
- Highly optimized for performance
- Supports both CPU and GPU
- Works with float32 and int8 vector types
- Scales to billions of vectors
- Provides support for quantization and clustering
Now that we understand why Faiss is useful, let’s look at how to get it running on a Linux system.
Pre-Installation Checklist
Before you begin, make sure your Linux system has the following:
- Python 3.7+ (Python 3.8 or newer recommended)
- pip or conda installed
- GCC / G++ (for building from source)
- CUDA Toolkit (if using GPU-enabled Faiss)
- CMake (required for source build)
You can check if Python and pip are installed by running:
python3 --version
pip3 --version
And for conda:
conda --version
If you’re missing any of these, install them using your system’s package manager or download them from the official websites.
Method 1: Installing Faiss with pip (CPU-only)
The simplest way to install Faiss on Linux — especially for CPU-only use — is via pip.
Step-by-Step Instructions
- Create a virtual environment (recommended):
python3 -m venv faiss-env
source faiss-env/bin/activate
- Upgrade pip:
pip install --upgrade pip
- Install Faiss (CPU-only):
pip install faiss-cpu
Pros:
- Easy and fast
- No need to compile anything
Cons:
- Does not support GPU
Method 2: Installing Faiss with Conda (CPU and GPU)
If you want GPU acceleration or need more stability, conda is a better choice. Faiss is available through the official conda-forge
channel.
Step-by-Step Instructions
- Create a new conda environment:
conda create -n faiss-env python=3.9 -y
conda activate faiss-env
- Add the conda-forge channel:
conda config --add channels conda-forge
- Install Faiss (CPU):
conda install faiss-cpu -c conda-forge
Or, for GPU support:
conda install faiss-gpu cudatoolkit=11.3 -c conda-forge
⚠️ Make sure the CUDA version you install matches the version on your system (
nvidia-smi
can help you check).
Pros:
- Supports both CPU and GPU
- Stable and well-maintained builds
- Easier dependency management
Cons:
- Larger installation footprint
- Requires conda
Method 3: Building Faiss from Source (Advanced)
This method is best if you need the latest features from the Faiss GitHub repository or want custom modifications.
Step-by-Step Instructions
- Install system dependencies:
sudo apt update
sudo apt install build-essential cmake libopenblas-dev libomp-dev git python3-dev python3-pip
- Clone the Faiss repository:
git clone https://github.com/facebookresearch/faiss.git
cd faiss
- Set up a Python environment:
python3 -m venv venv
source venv/bin/activate
pip install numpy cython
- Build Faiss (CPU-only):
cmake -B build -DFAISS_ENABLE_PYTHON=ON -DFAISS_ENABLE_GPU=OFF .
make -C build -j
cd build/faiss/python
python setup.py install
- For GPU Support (optional):
Install CUDA (if not already installed), then build with:
cmake -B build -DFAISS_ENABLE_PYTHON=ON -DFAISS_ENABLE_GPU=ON -DCMAKE_CUDA_COMPILER=/usr/local/cuda/bin/nvcc .
make -C build -j
cd build/faiss/python
python setup.py install
Pros:
- Full control over the build
- Access to latest GitHub features
Cons:
- More complex
- Requires compilation and troubleshooting
Post-Installation: Verifying Your Installation
After installing, verify that Faiss is working:
import faiss
print(faiss.__version__)
You should see the installed version without any errors.
You can also run a quick test:
import faiss
import numpy as np
d = 64 # dimension
nb = 1000 # database size
nq = 5 # number of queries
xb = np.random.random((nb, d)).astype('float32')
xq = np.random.random((nq, d)).astype('float32')
index = faiss.IndexFlatL2(d) # L2 distance index
index.add(xb)
D, I = index.search(xq, 5)
print(I) # indices of nearest neighbors
Common Installation Issues and Fixes
Issue: ModuleNotFoundError: No module named 'faiss'
Fix: Double-check your environment and make sure you installed the correct package (faiss-cpu
or faiss-gpu
).
Issue: CUDA not detected or mismatched
Fix: Use nvidia-smi
to verify your installed CUDA version and match it with the one in your conda
or build settings.
Issue: Compilation errors during source build
Fix: Ensure that dependencies like CMake, OpenBLAS, and Python headers are properly installed.
Conclusion
So, how do I install Faiss on Linux? The answer depends on your system setup, performance requirements, and preference for ease versus customization.
Here’s a quick summary:
Installation Method | GPU Support | Difficulty | Recommended For |
---|---|---|---|
pip install faiss-cpu | ❌ | Easy | Beginners, CPU-only projects |
conda install faiss-gpu | ✅ | Moderate | Most users, GPU-enabled apps |
Build from source | ✅ | Advanced | Developers, Custom builds |
No matter which method you choose, Faiss offers blazing-fast performance and versatility for vector similarity search tasks. Now that you have it installed, you’re ready to build applications that scale to millions (or billions) of vectors!