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

> Install the Segment Anything Model SAM in one command using pip. Follow our complete guide for a quick setup of this powerful image segmentation tool from Meta AI.

- Repository: [Meta Research/segment-anything](https://github.com/facebookresearch/segment-anything)
- Tags: getting-started
- Published: 2026-03-07

---

**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`](https://github.com/facebookresearch/segment-anything/blob/main/segment_anything/predictor.py) and the underlying PyTorch implementation in [`segment_anything/modeling/sam.py`](https://github.com/facebookresearch/segment-anything/blob/main/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`](https://github.com/facebookresearch/segment-anything/blob/main/README.md):

- **Python** ≥ 3.8
- **PyTorch** ≥ 1.7 with matching **torchvision** ≥ 0.8

Install PyTorch following the [official installation guide](https://pytorch.org/get-started/locally/) 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:

```bash
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:

```bash
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`](https://github.com/facebookresearch/segment-anything/blob/main/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`](https://github.com/facebookresearch/segment-anything/blob/main/setup.py) under `extras_require["all"]`:

```bash
pip install opencv-python pycocotools matplotlib onnxruntime onnx

```

For Jupyter notebook support, also install:

```bash
pip install jupyter

```

These dependencies enable the automatic mask generator in [`segment_anything/automatic_mask_generator.py`](https://github.com/facebookresearch/segment-anything/blob/main/segment_anything/automatic_mask_generator.py) and the export script at [`scripts/export_onnx_model.py`](https://github.com/facebookresearch/segment-anything/blob/main/scripts/export_onnx_model.py).

## Verify Your Installation

Confirm SAM installed correctly by checking the version attribute:

```python
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:

```python
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`](https://github.com/facebookresearch/segment-anything/blob/main/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`](https://github.com/facebookresearch/segment-anything/blob/main/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.