DEV Community

matias yoon
matias yoon

Posted on

로컬 LLM 셋업 가이드 (v4)

로컬 LLM 셋업 가이드 (v4)

1. 개요 및 사전 준비

로컬 LLM은 인터넷 연결 없이도 AI 추론을 할 수 있는 방식입니다. 이 가이드는 Linux 기반 시스템에서 실행 가능한 최적의 로컬 LLM 설정 방법을 안내합니다.

사전 요구사항:

  • OS: Ubuntu 20.04 이상 (권장: Ubuntu 22.04)
  • CPU: 최소 8코어 이상 (16코어 이상 권장)
  • RAM: 최소 16GB (32GB 이상 권장)
  • GPU: NVIDIA RTX 30xx/40xx 시리즈 이상 (메모리 8GB 이상)
  • 저장공간: 최소 50GB (모델별로 다름)

GPU 확인 명령:

nvidia-smi
lspci | grep -i nvidia
Enter fullscreen mode Exit fullscreen mode

2. 프레임워크 비교

프레임워크 장점 단점 사용 사례
llama.cpp 가볍고 빠르며, 직접 컴파일 가능 GUI 없음, 모델 변환 필요 서버/스마트폰 앱용
Ollama 쉬운 설치 및 관리 GPU 가속 제한 개발 테스트
vLLM 높은 추론 속도 복잡한 설치 고성능 서버
LocalAI REST API 제공, 다양한 모델 지원 리소스 소모 많음 API 기반 앱

권장: llama.cpp + llamafile (가장 간단하고 효율적)

3. 설치 단계

3.1 기본 패키지 설치:

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

3.2 llama.cpp 설치:

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

3.3 모델 다운로드:

mkdir -p models
cd models
wget https://huggingface.co/TheBloke/Llama-3.2-3B-Instruct-GGUF/resolve/main/llama-3.2-3b-instruct.Q4_K_M.gguf
Enter fullscreen mode Exit fullscreen mode

3.4 실행:

cd ../
./llama.cpp/main -m models/llama-3.2-3b-instruct.Q4_K_M.gguf -p "Qwen: Hello World" -n 100
Enter fullscreen mode Exit fullscreen mode

4. 모델 선택 가이드

4.1 일반 용도:

  • Llama-3.2-3B-Instruct: 빠르고 효율적, 문서 분석용
  • Phi-4: 가볍고 빠른 추론

4.2 복잡한 문서 처리:

  • LLaVA-1.5-7B: 이미지/텍스트 혼합 처리 (시각 모델)
  • Qwen-VL: 중국어 기반, 문서 이해력 뛰어남

4.3 고성능:

  • Mixtral-8x22B: 대용량 모델, 추론 속도 저하 시 Q4_K_M로 변환

5. 양자화 타입 설명

타입 메모리 사용량 정확도 사용 사례
Q4_K_M ~5GB 높음 일반 추론
Q5_K_M ~6GB 매우 높음 정밀 문서 분석
Q6_K ~7GB 고정 높은 정확도 필요
Q8_0 ~10GB 최고 테스트 및 개발
# 양자화 예시 (llama.cpp 내장)
./llama.cpp/convert-hf-to-gguf.py --model-path TheBloke/Llama-3.2-3B-Instruct-GGUF --output-path llama-3.2-3b-instruct.q4_k_m.gguf
Enter fullscreen mode Exit fullscreen mode

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

6.1 FastAPI 서버 설정:

pip install fastapi uvicorn
Enter fullscreen mode Exit fullscreen mode

6.2 API 서버 코드 (server.py):

from fastapi import FastAPI
import subprocess
import json

app = FastAPI()

@app.post("/chat")
async def chat(prompt: str):
    result = subprocess.run([
        "./llama.cpp/main",
        "-m", "models/llama-3.2-3b-instruct.Q4_K_M.gguf",
        "--prompt", prompt,
        "-n", "100",
        "--no-display-prompt"
    ], capture_output=True, text=True)

    return {"response": result.stdout}
Enter fullscreen mode Exit fullscreen mode

6.3 실행 명령:

uvicorn server:app --reload --host 0.0.0.0 --port 8000
Enter fullscreen mode Exit fullscreen mode

6.4 기존 앱 통합 예시:

curl -X POST http://localhost:8000/chat \
  -H "Content-Type: application/json" \
  -d '{"prompt": "Explain quantum computing in simple terms"}'
Enter fullscreen mode Exit fullscreen mode

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

7.1 서비스 파일 생성:

sudo nano /etc/systemd/system/llm-server.service
Enter fullscreen mode Exit fullscreen mode

7.2 서비스 설정:

[Unit]
Description=Local LLM Server
After=network.target

[Service]
Type=simple
User=your_user
WorkingDirectory=/home/your_user/llama.cpp
ExecStart=/usr/bin/python3 /home/your_user/llama.cpp/server.py
Restart=always
RestartSec=10

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

7.3 서비스 시작:

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

8. 모니터링 및 성능 튜닝

8.1 성능 모니터링:

# GPU 메모리 사용량
watch -n 1 nvidia-smi

# CPU/메모리 사용량
htop
Enter fullscreen mode Exit fullscreen mode

8.2 추론 성능 테스트:

# 테스트 명령
./llama.cpp/main -m models/llama-3.2-3b-instruct.Q4_K_M.gguf -p "Test prompt" -n 100 --timing
Enter fullscreen mode Exit fullscreen mode

8.3 자동 확장 설정 (예시):

# CPU 사용량 초과 시 알림
sudo apt install sysstat
Enter fullscreen mode Exit fullscreen mode

9. 실전 예제 명령들

9.1 문서 분석 예제:

# 이미지 포함 문서 분석 (LLaVA 모델)
./llama.cpp/main -m models/llava-1.5-7b.Q4_K_M.gguf -p "Describe the image" -i image.jpg
Enter fullscreen mode Exit fullscreen mode

9.2 대화형 추론:

# 채팅 모드
./llama.cpp/main -m models/llama-3.2-3b-instruct.Q4_K_M.gguf -c 2048 --temp 0.7 --repeat_penalty 1.1
Enter fullscreen mode Exit fullscreen mode

9.3 성능 벤치마크:

# 추론 시간 측정
time ./llama.cpp/main -m models/llama-3.2-3b-instruct.Q4_K_M.gguf -p "Benchmark test" -n 100
Enter fullscreen mode Exit fullscreen mode

10. 주의사항 및 문제 해결

10.1 메모리 부족 해결:

# 스왑 공간 생성
sudo fallocate -l 8G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
Enter fullscreen mode Exit fullscreen mode

10.2 GPU 드라이버 업데이트:

sudo apt install nvidia-driver-535
sudo reboot
Enter fullscreen mode Exit fullscreen mode

이 가이드를 따르면 기본적으로 로컬 LLM을 설치하고 실행할 수 있습니다. 다양한 모델과 설정을 통해 실제 엔지니어링 환경에서 사용할 수 있는 로컬 AI 서비스를 구축할 수 있습니다.


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

Top comments (0)