DEV Community

matias yoon
matias yoon

Posted on

로컬 LLM 셋업 가이드 (v14)

로컬 LLM 셋업 가이드 (v14)

1. 개요 및 사전 요구사항

로컬 LLM은 GPU 없이도 실행 가능하지만 성능을 위해 GPU가 권장됩니다. 최소 RAM 요구사항:

  • GPU 없이: 16GB RAM (최소)
  • GPU 포함: 8GB VRAM (권장), 16GB RAM

지원 OS: Ubuntu 20.04+, CentOS 8+, Arch Linux

# 시스템 정보 확인
lscpu
free -h
nvidia-smi  # GPU가 있다면
Enter fullscreen mode Exit fullscreen mode

2. 프레임워크 비교

프레임워크 장점 단점 추천 사용场景
llama.cpp 최소 의존성, C++ 기반, 모든 시스템 호환 GPU 지원 제한 가벼운 로컬 개발
Ollama 간단한 CLI, 자동 모델 다운로드 메모리 효율성 낮음 빠른 프로토타입
vLLM 높은 추론 성능 복잡한 설치 고성능 서버
LocalAI API 호환성, 여러 모델 지원 리소스 소모 큼 API 기반 애플리케이션

선택 기준: 빠른 프로토타입 → Ollama / 높은 성능 → vLLM

3. 권장 설정: llama.cpp 설치

# 1. 의존성 설치
sudo apt update
sudo apt install git cmake build-essential

# 2. llama.cpp 클론
git clone https://github.com/ggerganov/llama.cpp
cd llama.cpp

# 3. 컴파일
make clean
make -j$(nproc)

# 4. 모델 다운로드
mkdir models
cd models
wget https://huggingface.co/TheBloke/Mistral-7B-v0.1-GGUF/resolve/main/mistral-7b-v0.1.Q4_K_M.gguf

# 5. 로컬 서버 실행
cd ..
./server -m models/mistral-7b-v0.1.Q4_K_M.gguf -c 2048 --host 0.0.0.0 --port 8080
Enter fullscreen mode Exit fullscreen mode

4. 모델 선택 가이드

모델 용량 성능 사용 사례
Mistral-7B-Q4_K_M 3.5GB 높음 일반 텍스트 생성
Llama-2-7B-Q4_K_M 3.5GB 중간 챗봇
Phi-3-mini-Q4_K_M 2.5GB 빠름 빠른 응답
Gemma-2B-Q4_K_M 1.2GB 낮음 메모리 제한 환경

5. 양자화 타입 설명

# 양자화 옵션 비교
./llama.cpp/quantize models/phi-3-mini.gguf models/phi-3-mini.Q4_K_M.gguf Q4_K_M
./llama.cpp/quantize models/phi-3-mini.gguf models/phi-3-mini.Q5_K_M.gguf Q5_K_M
./llama.cpp/quantize models/phi-3-mini.gguf models/phi-3-mini.Q2_K.gguf Q2_K

# 양자화 타입별 특징
# Q2_K: 2.56 bits, 최대 압축
# Q4_K_M: 4.31 bits, 균형 잡힌 품질
# Q5_K_M: 5.17 bits, 높은 품질
Enter fullscreen mode Exit fullscreen mode
타입 비트 수 크기 품질
Q2_K 2.56 1.2GB 낮음
Q4_K_M 4.31 2.5GB 중간
Q5_K_M 5.17 3.0GB 높음

6. API 설정 및 통합

# API 서버 실행
./server -m models/mistral-7b-v0.1.Q4_K_M.gguf --host 0.0.0.0 --port 8080

# curl 테스트
curl http://localhost:8080/completion \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "Hello, my name is",
    "n_predict": 50,
    "temperature": 0.7
  }'

# Python 클라이언트 예제
python3 <<EOF
import requests
response = requests.post('http://localhost:8080/completion', 
    json={'prompt': 'Why is the sky blue?', 'n_predict': 100})
print(response.json()['content'])
EOF
Enter fullscreen mode Exit fullscreen mode

7. Systemd 서비스 설정

# 서비스 파일 생성
sudo nano /etc/systemd/system/llm.service

# 서비스 내용
[Unit]
Description=Local LLM Server
After=network.target

[Service]
Type=simple
User=ubuntu
WorkingDirectory=/home/ubuntu/llama.cpp
ExecStart=/home/ubuntu/llama.cpp/server -m /home/ubuntu/llama.cpp/models/mistral-7b-v0.1.Q4_K_M.gguf -c 2048 --host 0.0.0.0 --port 8080
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

# 서비스 시작
sudo systemctl daemon-reload
sudo systemctl enable llm
sudo systemctl start llm

# 상태 확인
sudo systemctl status llm
Enter fullscreen mode Exit fullscreen mode

8. 모니터링 및 성능 튜닝

# CPU/메모리 모니터링
htop
nvidia-smi -l 1  # GPU 사용량

# 성능 테스트
ab -n 100 -c 10 http://localhost:8080/completion

# 최적화 명령어
./server -m models/mistral-7b-v0.1.Q4_K_M.gguf \
  -c 2048 \
  -p 512 \
  --threads 8 \
  --batch-size 512 \
  --host 0.0.0.0 --port 8080

# 메모리 제한 설정
ulimit -v 4000000  # 4GB 제한
Enter fullscreen mode Exit fullscreen mode

9. 실전 예제

# 1. 간단한 챗봇 API
curl -X POST http://localhost:8080/completion \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "<|user|>\nHello, how are you?\n<|assistant|>",
    "n_predict": 100,
    "temperature": 0.7,
    "stop": ["<|user|>"]
  }'

# 2. 에이전트 프로토콜
curl -X POST http://localhost:8080/completion \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "System: You are a helpful assistant.\nUser: What is 2+2?\nAssistant:",
    "n_predict": 20,
    "temperature": 0.3
  }'
Enter fullscreen mode Exit fullscreen mode

10. 성능 기준

설정 추론 시간 (100토큰) 메모리 사용량
Q4_K_M, 8코어 3.5초 2.8GB
Q5_K_M, 16코어 3.0초 3.2GB
Q2_K, 8코어 4.2초 1.8GB

11. 문제 해결

# 메모리 부족 시
./server -m model.gguf -c 1024 --threads 4

# 오류 로그 확인
journalctl -u llm -f

# 포트 충돌 확인
sudo netstat -tulnp | grep :8080
Enter fullscreen mode Exit fullscreen mode

이 가이드를 따라하면 8GB VRAM이 있는 시스템에서 최대한 빠르고 효율적인 로컬 LLM 환경을 구축할 수 있습니다. 메모리 제한 환경에서는 Q2_K 또는 Q4_K_M 양자화를 사용하여 최적화하세요.


📥 Get the full guide on Gumroad: https://gumroad.com/l/auto ($7)

Top comments (0)