DEV Community

matias yoon
matias yoon

Posted on

로컬 LLM 셋업 가이드 (v15)

로컬 LLM 셋업 가이드 (v15)

DevOps 기반의 로컬 LLM 인프라를 빠르게 구축하는 실전 가이드

1. 개요 및 전제 조건

이 가이드는 로컬에서 LLM을 효율적으로 실행하기 위한 실전 가이드입니다. Linux 기반 머신이 필요하며, GPU 지원이 있으면 더 빠른 성능을 얻을 수 있습니다.

요구사항:

  • RAM: 최소 8GB, 권장 16GB 이상
  • CPU: 64비트 x86_64 (Intel/AMD)
  • GPU: CUDA 지원 (NVIDIA) 또는 ROCm 지원 (AMD)
  • 디스크: 최소 20GB 여유 공간 (모델 다운로드 및 저장)
  • 운영체제: Ubuntu 22.04 LTS 이상 또는 CentOS 9

사전 준비:

sudo apt update && sudo apt upgrade -y
sudo apt install git build-essential cmake python3-pip -y
Enter fullscreen mode Exit fullscreen mode

2. 프레임워크 비교: llama.cpp vs Ollama vs vLLM vs LocalAI

프레임워크 장점 단점 추천 사용 사례
llama.cpp 최소 의존성, 높은 성능, 직접 컴파일 가능 사용자 친화적 UI 부족 고급 사용자, 시스템 수준 최적화 필요
Ollama 간편 설치, Docker 기반, GUI 지원 메모리 사용량 높음 빠른 개발/테스트
vLLM 초고속 추론, 멀티 GPU 지원 복잡한 설정 필요 대규모 배포, 실시간 추론
LocalAI OpenAI API 호환, 다양한 모델 지원 느린 초기 로딩 프로덕션 API 서버 필요

최종 추천: llama.cpp + llama-cpp-python + systemd

3. 추천 설정 설치 (llama.cpp 기반)

3.1 소스 코드 다운로드 및 컴파일

git clone https://github.com/ggerganov/llama.cpp.git
cd llama.cpp
make
Enter fullscreen mode Exit fullscreen mode

3.2 Python 바인딩 설치

pip3 install llama-cpp-python
Enter fullscreen mode Exit fullscreen mode

3.3 기본 실행 예제

./main -m ./models/llama-2-7b-chat.Q5_K_M.gguf -p "Hello, world!" --temp 0.2
Enter fullscreen mode Exit fullscreen mode

4. 모델 선택 가이드

모델 파라미터 추천 사용 사례 추천 quantization
Llama-2 7B 7B 일반 챗봇, 코드 생성 Q5_K_M
Mistral 7B 7B 다중 언어 처리 Q5_K_M
Phi-3 Mini 3.8B 가볍고 빠른 추론 Q4_K_M
Mixtral 8x7B 47B 고급 추론 Q5_K_M

실제 다운로드 예제:

mkdir -p models
cd models
wget https://huggingface.co/TheBloke/Mistral-7B-v0.1-GGUF/resolve/main/mistral-7b-v0.1.Q5_K_M.gguf
Enter fullscreen mode Exit fullscreen mode

5. 양자화 유형 설명

유형 설명 성능 크기
Q4_K_M Kahan summation, 최적화된 메모리 사용 빠름 최소
Q5_K_M Q4 기반 + 높은 정밀도 매우 빠름 중간
Q8_0 8-bit 정밀도 정확도 높음 크기 큼
F16 반정밀도 (FP16) 최고 정확도 가장 큼

양자화 예제:

# 원본 모델을 Q5_K_M로 변환
python3 convert.py ./models/llama-2-7b-chat.bin --outtype q5_k_m
Enter fullscreen mode Exit fullscreen mode

6. API 설정 및 기존 도구와 통합

6.1 API 서버 실행

python3 -m llama_cpp.server --model ./models/mistral-7b-v0.1.Q5_K_M.gguf \
    --host 0.0.0.0 --port 8000 \
    --n_ctx 4096 \
    --n_threads 8
Enter fullscreen mode Exit fullscreen mode

6.2 OpenAI 호환 API 테스트

curl http://localhost:8000/v1/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "mistral-7b",
    "prompt": "Write a function to calculate fibonacci in Python",
    "max_tokens": 200
  }'
Enter fullscreen mode Exit fullscreen mode

6.3 VSCode 연결

{
  "openai.apiKey": "sk-xxx",
  "openai.baseUrl": "http://localhost:8000/v1"
}
Enter fullscreen mode Exit fullscreen mode

7. 24/7 운영을 위한 Systemd 서비스

7.1 서비스 파일 생성

sudo nano /etc/systemd/system/llm.service
Enter fullscreen mode Exit fullscreen mode
[Unit]
Description=Local LLM Server
After=network.target

[Service]
Type=simple
User=ubuntu
WorkingDirectory=/home/ubuntu/llama.cpp
ExecStart=/home/ubuntu/llama.cpp/main -m /home/ubuntu/models/mistral-7b-v0.1.Q5_K_M.gguf \
    --host 0.0.0.0 --port 8000 \
    --n_ctx 4096 \
    --n_threads 8
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

7.2 서비스 시작

sudo systemctl daemon-reload
sudo systemctl enable llm.service
sudo systemctl start llm.service
sudo systemctl status llm.service
Enter fullscreen mode Exit fullscreen mode

8. 모니터링 및 성능 튜닝

8.1 성능 모니터링 스크립트

#!/bin/bash
# monitor.sh
while true; do
    echo "=== Memory ==="
    free -h
    echo "=== CPU ==="
    top -bn1 | grep "Cpu(s)"
    echo "=== Load ==="
    uptime
    sleep 30
done
Enter fullscreen mode Exit fullscreen mode

8.2 성능 최적화 파라미터

# CPU 코어 수 확인
nproc

# 추론 최적화 옵션
./main -m model.gguf \
    --n_threads 8 \
    --n_batch 512 \
    --n_ctx 4096 \
    --rope_freq_base 10000.0 \
    --rope_freq_scale 1.0 \
    --temp 0.2 \
    --repeat_penalty 1.1
Enter fullscreen mode Exit fullscreen mode

8.3 성능 벤치마크

# 테스트 모델 로드 시간
time ./main -m ./models/mistral-7b-v0.1.Q5_K_M.gguf -p "Test" --temp 0.1

# 추론 시간 측정
time ./main -m ./models/mistral-7b-v0.1.Q5_K_M.gguf \
    -p "Explain quantum computing in simple terms" \
    --temp 0.2 \
    --n_predict 100
Enter fullscreen mode Exit fullscreen mode

9. 실전 예제 커맨드

9.1 커스텀 텍스트 생성

./main -m ./models/mistral-7b-v0.1.Q5_K_M.gguf \
    -p "Write a 3-line poem about AI and humans" \
    --temp 0.7 \
    --n_predict 50
Enter fullscreen mode Exit fullscreen mode

9.2 JSON 파싱 도우미

./main -m ./models/mistral-7b-v0.1.Q5_K_M.gguf \
    -p "Parse this JSON and extract the 'name' field: {\"name\":\"John\", \"age\":30}" \
    --temp 0.1 \
    --n_predict 20
Enter fullscreen mode Exit fullscreen mode

9.3 코드 자동 생성


bash
./main -m ./models/mistral-7b-v0.1.Q5_K_M.gguf \
    -p "Write a Python function to validate email addresses" \
    --temp 0.3 \


---

📥 **Get the full guide on Gumroad**: https://gumroad.com/l/auto ($7)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)