How to Install the Segment Anything Model (SAM): A Complete Guide

Install SAM in one command with pip install git+https://github.com/facebookresearch/segment-anything.git after ensuring Python ≥ 3.8 and PyTorch ≥ 1.7 are available.

The Segment Anything Model (SAM) from Meta AI’s facebookresearch/segment-anything repository delivers state-of-the-art image segmentation through a promptable interface. Installing SAM correctly provides access to the high-level SamPredictor API defined in segment_anything/predictor.py and the underlying PyTorch implementation in segment_anything/modeling/sam.py.

Prerequisites: Python and PyTorch

Before installing SAM, ensure your environment meets the base requirements specified in the repository’s README.md:

  • Python ≥ 3.8
  • PyTorch ≥ 1.7 with matching torchvision ≥ 0.8

Install PyTorch following the official installation guide for your specific CUDA or CPU configuration. SAM relies on PyTorch for both the image encoder and the mask decoder operations.

Installation Methods

You can install SAM either directly from the GitHub repository for immediate use, or in editable mode for development and source modification.

Quick Install via pip

The fastest method installs the package directly from the main branch without cloning:

pip install git+https://github.com/facebookresearch/segment-anything.git

This command pulls the latest stable version, including the sam_model_registry and model classes defined in the repository root.

Editable Install for Development

For modifying the source code or contributing to facebookresearch/segment-anything, clone the repository and install in editable mode:

git clone https://github.com/facebookresearch/segment-anything.git
cd segment-anything
pip install -e .

This creates a local installation where changes to files like segment_anything/predictor.py reflect immediately without reinstallation.

Optional Dependencies

To run the example notebooks, export models to ONNX, or handle COCO-style masks, install the additional packages defined in setup.py under extras_require["all"]:

pip install opencv-python pycocotools matplotlib onnxruntime onnx

For Jupyter notebook support, also install:

pip install jupyter

These dependencies enable the automatic mask generator in segment_anything/automatic_mask_generator.py and the export script at scripts/export_onnx_model.py.

Verify Your Installation

Confirm SAM installed correctly by checking the version attribute:

import segment_anything
print(segment_anything.__version__)   # should print "1.0"

If this imports without errors, the core package is ready.

Load a Model and Run Inference

Download a model checkpoint (e.g., sam_vit_h_4b8939.pth) from the repository releases, then load it using the registry:

from segment_anything import SamPredictor, sam_model_registry
import numpy as np
from PIL import Image

# Initialize model

model_type = "vit_h"
checkpoint_path = "path/to/sam_vit_h_4b8939.pth"
sam = sam_model_registry[model_type](checkpoint=checkpoint_path)
predictor = SamPredictor(sam)

# Process an image

image = np.array(Image.open("path/to/image.jpg"))
predictor.set_image(image)

# Predict with a point prompt

point = np.array([[100, 150]])  # (X, Y) coordinates

label = np.array([1])           # 1 = foreground

masks, scores, low_res = predictor.predict(
    point_coords=point,
    point_labels=label,
    multimask_output=False
)
print("Mask shape:", masks.shape)

The SamPredictor class handles image embedding caching and prompt-based mask prediction as implemented in the source.

Summary

  • SAM requires Python ≥ 3.8 and PyTorch ≥ 1.7 before installation can proceed.
  • Install via pip install git+https://github.com/facebookresearch/segment-anything.git for immediate use, or use pip install -e . after cloning for development.
  • Optional dependencies (opencv-python, pycocotools, onnx, etc.) unlock full functionality including ONNX export and notebook examples.
  • Verify installation by importing segment_anything and checking the version string.
  • Core classes like SamPredictor reside in segment_anything/predictor.py and provide the main interface for segmentation tasks.

Frequently Asked Questions

Can I install SAM without internet access?

Yes. Clone the repository on a connected machine using git clone https://github.com/facebookresearch/segment-anything.git, transfer the folder to your offline environment, and run pip install -e . from the local directory. Ensure you have downloaded the PyTorch wheels separately if they are not cached.

Does SAM require a GPU to install?

No. SAM installs on CPU-only PyTorch versions. However, inference speed is significantly faster with CUDA. The installation process itself does not require a GPU, but you should install the CUDA-enabled PyTorch version if you plan to use GPU acceleration.

How do I install specific optional features only?

Instead of installing all optional dependencies, you can select specific packages from the extras_require["all"] list in setup.py. For example, install only opencv-python and matplotlib for basic visualization, or add onnxruntime and onnx solely for model export purposes without pulling the full dependency set.

Which model checkpoint should I download after installation?

SAM provides three model sizes in the registry: vit_h (largest, most accurate), vit_l, and vit_b (smallest, fastest). Download the corresponding .pth file from the GitHub releases page. The vit_h checkpoint offers the best segmentation quality but requires the most memory, while vit_b suits resource-constrained environments.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →