How to Locally Deploy Deepseek Large Model (Detailed Tutorial)
The open-source large models released by Deepseek AI have garnered widespread attention due to their excellent performance and openness. Deploying these powerful models locally not only protects data privacy and enables offline use but also allows for customization and optimization according to individual needs, providing great convenience for developers and researchers.
Rendering...
# Introduction
This article provides a detailed tutorial for locally deploying Deepseek large models, covering the three major operating systems: macOS, Windows, and Linux. We will introduce three mainstream deployment methods: **Ollama** (simplest and most user-friendly), **llama.cpp** (excellent performance, GGUF format), and **Hugging Face Transformers** (native Python, highly flexible), helping you choose the most suitable solution based on your needs.
# I. Preparations Before Local Deployment
Before proceeding with deployment, please ensure your system meets the following basic requirements.
## 1.1 Hardware Requirements
Local deployment of large models has certain hardware demands, especially for memory and VRAM.
* **CPU:** 8 cores or more recommended, with a higher clock speed.
* **RAM:** At least 16GB, 32GB or more recommended. Larger models require more memory.
* **GPU (Graphics Card):** **NVIDIA graphics cards are strongly recommended**, with at least 8GB of VRAM; 12GB, 16GB, or higher is recommended. GPUs can significantly accelerate the inference process.
* **NVIDIA Users:** Ensure you have installed the latest graphics driver and CUDA Toolkit.
* **AMD Users:** Some methods (e.g., llama.cpp) support ROCm, but compatibility is not as good as NVIDIA.
* **macOS Users:** Apple Silicon chips (M-series) provide excellent GPU acceleration through the Metal framework.
* **Storage Space:** Reserve sufficient hard drive space (typically tens to hundreds of gigabytes) depending on the model size you choose. SSDs are recommended for faster loading speeds.
## 1.2 Software Requirements (General)
The following software are essential tools for most deployment methods.
* **Operating System:**
* macOS (Monterey 12.0 or later)
* Windows 10/11 (64-bit)
* Linux (Ubuntu 20.04+, Debian 11+, CentOS 7+, etc.)
* **Git:** Used for cloning code repositories.
* **macOS:** Install Xcode Command Line Tools (`xcode-select --install`) or Homebrew (`brew install git`).
* **Windows:** Download and install from the [Git official website](https://git-scm.com/download/win).
* **Linux:** `sudo apt update && sudo apt install git` (Debian/Ubuntu) or `sudo yum install git` (CentOS).
* **Python (3.8+) and pip:** Used for Python-based deployment methods.
* **macOS/Linux:** Usually pre-installed; `pyenv` or `conda` is recommended for environment management.
* **Windows:** Download and install from the [Python official website](https://www.python.org/downloads/windows/), ensuring "Add Python to PATH" is checked.
* **C++ Compiler:** Primarily used for compiling llama.cpp.
* **macOS:** Xcode Command Line Tools (`xcode-select --install`).
* **Windows:** Visual Studio Build Tools or MinGW-w64.
* **Linux:** `build-essential` (`sudo apt install build-essential`).
* **CUDA Toolkit (NVIDIA GPU Users):** Download and install a version compatible with your graphics driver from the [NVIDIA official website](https://developer.nvidia.com/cuda-downloads).
# II. Choosing Deepseek Models
Deepseek offers several open-source models, including:
* **Deepseek-Coder:** Focuses on code generation and understanding.
* **Deepseek-V2:** A general-purpose large model with powerful performance.
You can find officially released Deepseek models on the [Hugging Face Hub](https://huggingface.co/deepseek-ai).
**Model Format Selection:**
* **GGUF format:** Suitable for `ollama` and `llama.cpp`. It is usually quantized, resulting in smaller file sizes and relatively lower hardware requirements, but may sacrifice a small amount of precision.
* **PyTorch format:** Suitable for the Hugging Face `transformers` library. This is the original model format, with larger file sizes and higher hardware requirements, but it offers the best performance and flexibility.
# III. Deployment Method 1: Using Ollama (Recommended, Simplest)
Ollama is a lightweight framework designed to simplify the process of running large models locally. It provides a unified API and command-line interface, supporting various GGUF format models.
## 3.1 Ollama Overview
* **Pros:** Simple installation, easy to use, cross-platform support, built-in API service.
* **Cons:** Model selection is limited to the Ollama library; customization is low.
## 3.2 Installing Ollama
#### macOS
1. **Using Homebrew (Recommended):**
```bash
brew install ollama
```
2. **Or download from the official website:**
Visit the [Ollama official website](https://ollama.com/download), download the macOS installer, and drag it to the Applications folder.
#### Windows
1. Visit the [Ollama official website](https://ollama.com/download) and download the Windows installer.
2. Double-click the installer and follow the prompts to complete the installation.
#### Linux
1. Open a terminal and run the following command:
```bash
curl -fsSL https://ollama.com/install.sh | sh
```
This script will automatically detect your system and install Ollama.
## 3.3 Downloading and Running Deepseek Models
The Ollama community provides GGUF versions of Deepseek models. You can search for Deepseek on the [Ollama official model library](https://ollama.com/library).
Taking `deepseek-coder:latest` as an example:
1. Open a terminal (Windows users use PowerShell or CMD).
2. Run the following command to download and start the model:
```bash
ollama run deepseek-coder:latest
```
If the model is not downloaded, Ollama will start downloading it automatically. After the download is complete, you will enter an interactive chat interface.
```
>>> Send a message (/? for help)
```
3. Type your question, and the model will generate a response.
```
>>> Hello, please introduce yourself.
I am Deepseek-Coder, a large language model trained by Deepseek AI, specializing in code generation, understanding, and question answering.
```
4. Type `/bye` to exit the interactive mode.
## 3.4 Accessing via API
Ollama starts a local API service in the background (default port `11434`), allowing you to interact with the model conveniently via HTTP requests.
**Example (using curl):**
```bash
curl http://localhost:11434/api/generate -d '{
"model": "deepseek-coder:latest",
"prompt": "Write a Python function to calculate the first n terms of the Fibonacci sequence.",
"stream": false
}'
```
You can also use HTTP libraries in Python or other programming languages to call this API.
# IV. Deployment Method 2: Using llama.cpp (Excellent Performance, GGUF Format)
`llama.cpp` is an inference engine written in C/C++ known for its efficient CPU and GPU (including NVIDIA CUDA, AMD ROCm, Apple Metal) inference capabilities, supporting GGUF format models.
## 4.1 llama.cpp Overview
* **Pros:** Excellent performance, low resource utilization, supports various hardware accelerations, active community.
* **Cons:** Requires manual compilation, has certain environment configuration requirements.
## 4.2 Environment Preparation
#### General Steps
1. **Install Git:** Refer to the instructions in "I. Preparations Before Local Deployment."
2. **Install C++ Compiler:**
* **macOS:** `xcode-select --install`
* **Windows:** Install [Visual Studio Build Tools](https://visualstudio.microsoft.com/zh-hans/downloads/) (select the "Desktop development with C++" workload) or [MinGW-w64](https://www.mingw-w64.org/doku.php/download).
* **Linux:** `sudo apt install build-essential` (Debian/Ubuntu) or `sudo yum groupinstall "Development Tools"` (CentOS).
#### GPU Acceleration (Optional)
* **macOS (Apple Silicon):** No additional configuration needed; `llama.cpp` supports Metal by default.
* **NVIDIA GPU:** Ensure you have installed the latest NVIDIA driver and **CUDA Toolkit**.
* **AMD GPU:** Ensure you have installed **ROCm**.
## 4.3 Cloning and Compiling llama.cpp
1. **Clone the repository:**
```bash
git clone https://github.com/ggerganov/llama.cpp.git
cd llama.cpp
```
2. **Compile:**
* **General CPU Compilation:**
```bash
make
```
* **macOS (Apple Silicon Metal Acceleration):**
```bash
make LLAMA_METAL=1
```
* **NVIDIA GPU (CUDA Acceleration):**
```bash
make LLAMA_CUDA=1
```
*If you encounter CUDA path issues, you might need to set the `CUDA_PATH` environment variable or specify it in the `make` command.*
* **AMD GPU (ROCm Acceleration):**
```bash
make LLAMA_ROCM=1
```
After successful compilation, you will find executables like `main` (Linux/macOS) or `main.exe` (Windows) in the `llama.cpp` directory.
## 4.4 Downloading Deepseek GGUF Models
1. Visit the [Hugging Face Hub](https://huggingface.co/models).
2. Search for `deepseek` and `GGUF`. Community members usually convert Deepseek models to GGUF format.
* For example, search for `deepseek-coder-6.7b-instruct GGUF`.
* Find a reliable model uploader (e.g., `TheBloke`), and navigate to their model page.
3. Select the quantization version you need (e.g., `Q4_K_M` is a common choice balancing performance and precision).
4. Download the `.gguf` file.
* **It is recommended to use `wget` or `curl` to download to the `llama.cpp/models` directory:**
```bash
# Example: Download deepseek-coder-6.7b-instruct.Q4_K_M.gguf
mkdir -p models
wget -P models https://huggingface.co/TheBloke/deepseek-coder-6.7B-Instruct-GGUF/resolve/main/deepseek-coder-6.7b-instruct.Q4_K_M.gguf
```
* **Or download manually and then move it to the `llama.cpp/models` directory.**
## 4.5 Running Deepseek Models
1. Ensure you are in the `llama.cpp` directory.
2. Run the model:
```bash
./main -m models/deepseek-coder-6.7b-instruct.Q4_K_M.gguf -p "Hello, please introduce yourself." -n 512 --temp 0.7
```
* `-m <path_to_model>`: Specifies the path to the GGUF model file.
* `-p "<prompt>"`: Your input prompt.
* `-n <tokens>`: Maximum number of tokens to generate.
* `--temp <value>`: Sampling temperature; higher values lead to more random results (0.0-2.0).
* `-t <threads>`: Number of CPU threads to use.
* `-ngl <layers>`: Number of model layers to offload to the GPU (NVIDIA/Metal). For example, `-ngl 30` means loading 30 layers to the GPU, with the rest on the CPU. **This is the key parameter for utilizing GPU acceleration.**
**Example (with GPU acceleration):**
```bash
# macOS (Metal)
./main -m models/deepseek-coder-6.7b-instruct.Q4_K_M.gguf -p "Write a Python function to calculate the first n terms of the Fibonacci sequence." -n 512 --temp 0.7 -ngl 999 # 999 means load as many as possible to GPU
# NVIDIA (CUDA)
./main -m models/deepseek-coder-6.7b-instruct.Q4_K_M.gguf -p "Write a Python function to calculate the first n terms of the Fibonacci sequence." -n 512 --temp 0.7 -ngl 999
```
# V. Deployment Method 3: Using Hugging Face Transformers (Native Python, Highly Flexible)
The Hugging Face `transformers` library is the standard tool for deploying and using various pre-trained large models. It offers maximum flexibility, making it suitable for scenarios requiring model fine-tuning, integration into complex applications, or use of the latest model features.
## 5.1 Transformers Overview
* **Pros:** Official support, widest model selection, easy integration into Python projects, supports PyTorch/TensorFlow/JAX, convenient for fine-tuning.
* **Cons:** Highest hardware requirements (especially VRAM), model files are typically large, may have many dependencies to install.
## 5.2 Environment Preparation
#### General Steps
1. **Install Python (3.8+) and pip:** Refer to the instructions in "I. Preparations Before Local Deployment."
2. **Create and activate a virtual environment (highly recommended):**
```bash
python -m venv deepseek_env
# macOS/Linux
source deepseek_env/bin/activate
# Windows
.\deepseek_env\Scripts\activate
```
3. **Install core dependencies:**
```bash
pip install torch transformers accelerate
```
* `torch`: PyTorch deep learning framework.
* `transformers`: Hugging Face model library.
* `accelerate`: Used to optimize model execution on multiple GPUs or CPUs.
#### GPU Acceleration (Optional)
* **NVIDIA GPU Users:**
* Install the correct command from the [PyTorch official website](https://pytorch.org/get-started/locally/) based on your CUDA Toolkit version.
* **Example (CUDA 12.1):**
```bash
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
```
* After installation, verify CUDA availability by running `python -c "import torch; print(torch.cuda.is_available())"`.
* **macOS (Apple Silicon) Users:**
* Install a PyTorch version that supports Metal Performance Shaders (MPS):
```bash
pip install --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cpu
```
*Note: Sometimes, installing the nightly version is required for the latest MPS support.*
* After installation, verify MPS availability by running `python -c "import torch; print(torch.backends.mps.is_available())"`.
## 5.3 Downloading Deepseek Model Weights
The Hugging Face `transformers` library automatically downloads model weights to the local cache directory the first time you load a model.
Taking `deepseek-ai/deepseek-coder-6.7b-instruct` as an example.
## 5.4 Writing Python Inference Code
Create a Python file (e.g., `deepseek_inference.py`) and add the following code:
```python
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
# 1. Select model path
# Deepseek-Coder-V1.5 6.7B Instruct
model_name = "deepseek-ai/deepseek-coder-6.7b-instruct"
# Deepseek-V2 (Note: V2 models are generally larger and require more VRAM)
# model_name = "deepseek-ai/DeepSeek-V2"
# 2. Set device
# Prioritize CUDA (NVIDIA GPU), then MPS (Apple Silicon), then CPU
if torch.cuda.is_available():
device = "cuda"
elif torch.backends.mps.is_available():
device = "mps"
else:
device = "cpu"
print(f"Using device: {device}")
# 3. Load tokenizer and model
print(f"Loading tokenizer for {model_name}...")
tokenizer = AutoTokenizer.from_pretrained(model_name)
print(f"Loading model {model_name} to {device}...")
# For larger models, you can use load_in_8bit or load_in_4bit for quantized loading to save VRAM
# but this may sacrifice some precision and speed.
# model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, device_map="auto", load_in_8bit=True)
# Or load directly to the specified device
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16).to(device)
model.eval() # Set to evaluation mode
print("Model loaded successfully!")
# 4. Define chat template (Deepseek-Coder-Instruct example)
# Deepseek models usually have specific chat formats; please refer to their Hugging Face pages.
# This example uses Deepseek-Coder-Instruct.
def generate_response(prompt_text):
messages = [
{"role": "user", "content": prompt_text}
]
# Use the tokenizer's apply_chat_template method to format the chat
# add_generation_prompt=True will add a prompt for the model to respond after the user's message
input_ids = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt").to(device)
print(f"\nUser: {prompt_text}")
print("Generating response...")
# 5. Generate text
with torch.no_grad():
outputs = model.generate(
input_ids,
max_new_tokens=512, # Maximum number of tokens to generate
do_sample=True,
temperature=0.7, # Sampling temperature
top_p=0.9, # Top-p sampling
eos_token_id=tokenizer.eos_token_id,
pad_token_id=tokenizer.pad_token_id # Ensure pad_token_id is set
)
# 6. Decode and print the result
# Skip the input part during decoding
response = tokenizer.decode(outputs[0][input_ids.shape[-1]:], skip_special_tokens=True)
print(f"Deepseek: {response.strip()}")
print("-" * 50)
# 7. Interactive chat
while True:
user_input = input("Enter your prompt (or 'quit' to exit): ")
if user_input.lower() == 'quit':
break
generate_response(user_input)
print("Exiting Deepseek inference.")
```
**Running the Python code:**
1. Save the code above as `deepseek_inference.py`.
2. Run it in your activated virtual environment:
```bash
python deepseek_inference.py
```
The first time you run it, model weights will be downloaded automatically. After the download is complete, the program will start loading the model and enter interactive mode.
# VI. Common Issues and Troubleshooting
* **Insufficient Memory/VRAM (OOM - Out Of Memory):**
* **Symptoms:** Program crashes with error messages like "CUDA out of memory," "MPS out of memory," or "killed."
* **Solutions:**
* Choose a smaller model version (e.g., 7B instead of 67B).
* Use more quantized GGUF models (e.g., Q4_K_M instead of Q8_0).
* For Transformers, try using the `load_in_8bit=True` or `load_in_4bit=True` parameters for quantized loading.
* Reduce the `max_new_tokens` parameter.
* Close other programs that consume significant memory/VRAM.
* **Model Download Failed or Slow:**
* Check your network connection.
* For Hugging Face models, you can try setting the environment variable `HF_ENDPOINT=https://hf-mirror.com` to use a mirror site (for users in mainland China only).
* Manually download GGUF files to the specified directory.
* **llama.cpp Compilation Errors:**
* Ensure all dependencies (Git, C++ compiler, CUDA Toolkit, etc.) are correctly installed and environment variables are configured.
* Check the error messages, which usually indicate a missing library or header file.
* Try cleaning and recompiling: `make clean && make`.
* **Python Environment Issues:**
* Ensure the correct virtual environment is activated.
* Check if the Python version meets the requirements.
* Use `pip list` to verify that all dependencies are installed correctly.
* If the PyTorch CUDA version does not match, uninstall and reinstall the correct PyTorch version.
* **Ollama Model Fails to Start:**
* Check if the Ollama service is running (`ollama list` or `ollama ps`).
* Try restarting the Ollama service.
* Verify that the model name is spelled correctly.
# VII. Conclusion and Outlook
This article has detailed three primary methods for locally deploying Deepseek large models on macOS, Windows, and Linux: Ollama, llama.cpp, and Hugging Face Transformers.
* **Ollama** offers the simplest deployment experience, suitable for quick experimentation and daily use.
* **llama.cpp** excels in performance and resource efficiency, making it an ideal choice for GGUF model inference.
* **Hugging Face Transformers** provides the greatest flexibility and control, suitable for in-depth development and customization.
Whether you are a developer, researcher, or AI enthusiast, locally deploying Deepseek large models will open new doors to exploring the potential of large models. As the open-source community continues to evolve, more efficient and user-friendly tools and models will emerge in the future. We hope this tutorial helps you take your first step smoothly and enjoy the convenience and fun of local large models!Comments
Please login to view and post comments
Go to Login