DEV Community

matias yoon
matias yoon

Posted on

로컬 LLM 셋업 가이드 (v49)

로컬 LLM 셋업 가이드 (v49)

1. 개요 및 사전 요구사항

로컬 LLM 실행은 AI 개발자에게 중요한 기술이 되고 있습니다. 이 가이드는 Linux 기반 시스템에서 로컬 LLM을 효율적으로 구축하고 운영하는 실용적인 방법을 제공합니다.

사전 요구사항:

  • OS: Ubuntu 22.04 LTS 이상
  • CPU: 최소 8코어, 권장 16코어 이상
  • RAM: 최소 32GB, 권장 64GB 이상
  • GPU: NVIDIA RTX 4090 이상 (CUDA 12.x 필요)
  • 스토리지: SSD 최소 200GB (모델 파일 포함)

최소 성능 기준:

# RAM 확인
free -g
# CPU 확인
lscpu | grep "CPU(s):"
# GPU 확인
nvidia-smi
Enter fullscreen mode Exit fullscreen mode

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

프레임워크 장점 단점 적합성
llama.cpp 최소 종속성, 직접 컴파일 가능 복잡한 설정 필요 전문 개발자
Ollama 쉬운 설치 및 관리, Docker 기반 GPU 최적화 제한 개발용, 테스트
vLLM 높은 성능, 멀티 GPU 지원 복잡한 구조, 메모리 요구량 높음 프로덕션
LocalAI 다중 모델 지원, REST API 리소스 소모 큼 실시간 서비스

3. 추천 설정: llama.cpp + Ollama 조합

llama.cpp 설치:

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

# 소스 코드 클론 및 컴파일
git clone https://github.com/ggerganov/llama.cpp.git
cd llama.cpp
make clean
make

# 테스트
./main -m ./models/7B/ggml-model-q4_0.bin -p "Hello, world!"
Enter fullscreen mode Exit fullscreen mode

Ollama 설치:

# Ollama 설치 (Ubuntu)
curl -fsSL https://ollama.com/install.sh | sh

# 서비스 시작
sudo systemctl start ollama
sudo systemctl enable ollama

# 모델 다운로드
ollama pull llama3
ollama pull codellama:7b
Enter fullscreen mode Exit fullscreen mode

4. 모델 선택 가이드

일반 목적 모델:

  • Llama3 8B: 일반적인 작업에 최적화
  • Phi-3 Mini: 빠른 추론 속도

코드 전용 모델:

  • CodeLlama 7B: 코드 생성 및 분석
  • StarCoder 15B: 다양한 프로그래밍 언어 지원

성능 기준:

# 모델 성능 테스트 (llama.cpp)
./main -m ./models/7B/ggml-model-q4_0.bin -p "Write a Python function to reverse a string" -n 200

# Ollama 성능 테스트
ollama run llama3 "Explain quantum computing in simple terms"
Enter fullscreen mode Exit fullscreen mode

5. 양자화 유형 설명

Q4_K_M vs Q5_K_M:

  • Q4_K_M: 4비트 양자화, 50% 메모리 절약
  • Q5_K_M: 5비트 양자화, 25% 메모리 절약 + 높은 정확도

양자화 명령어:

# GGUF 포맷 변환
python convert-hf-to-gguf.py ./models/Llama-3-8B-Instruct/ --outtype q4_k_m --outfile ./models/Llama-3-8B-Instruct-q4_k_m.gguf

# 모델 병렬화
./llama-bench -m ./models/Llama-3-8B-Instruct-q4_k_m.gguf --parallel 8
Enter fullscreen mode Exit fullscreen mode

6. API 설정 및 툴 통합

REST API 서버 실행:

# llama.cpp API 서버
./server -m ./models/Llama-3-8B-Instruct-q4_k_m.gguf -c 2048 --host 0.0.0.0 --port 1234

# Ollama API 통신
curl http://localhost:11434/api/generate -d '{
  "model": "llama3",
  "prompt": "Explain neural networks in simple terms",
  "stream": false
}'
Enter fullscreen mode Exit fullscreen mode

VS Code 통합 설정:

// .vscode/settings.json
{
  "llm.server.url": "http://localhost:1234",
  "llm.model": "Llama-3-8B-Instruct-q4_k_m.gguf"
}
Enter fullscreen mode Exit fullscreen mode

7. Systemd 서비스 설정 (24/7 운영)

서비스 파일 생성:

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

[Service]
Type=simple
User=developer
WorkingDirectory=/home/developer/llama.cpp
ExecStart=/home/developer/llama.cpp/server -m /home/developer/models/Llama-3-8B-Instruct-q4_k_m.gguf -c 2048 --host 0.0.0.0 --port 1234
Restart=always
RestartSec=10

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

서비스 시작:

# 서비스 등록 및 시작
sudo systemctl daemon-reload
sudo systemctl enable llama-server
sudo systemctl start llama-server

# 상태 확인
sudo systemctl status llama-server
Enter fullscreen mode Exit fullscreen mode

8. 모니터링 및 성능 튜닝

성능 모니터링:

# CPU/MEM 사용량
htop

# GPU 사용량
nvidia-smi -l 1

# 로그 모니터링
journalctl -u llama-server -f
Enter fullscreen mode Exit fullscreen mode

성능 최적화:

# 메모리 최적화 실행
./main -m ./models/Llama-3-8B-Instruct-q4_k_m.gguf -c 2048 -n 128 --temp 0.7 --repeat_penalty 1.1

# 성능 벤치마크
./llama-bench -m ./models/Llama-3-8B-Instruct-q4_k_m.gguf -p 512 -n 256 -t 8
Enter fullscreen mode Exit fullscreen mode

9. 실제 사용 예제

코드 생성 예제:

# Python 코드 생성
curl http://localhost:1234/generate -d '{
  "prompt": "Write a Python function to calculate fibonacci numbers",
  "max_tokens": 200
}'
Enter fullscreen mode Exit fullscreen mode

API 연동 예제:

# Python API 연동
import requests

def call_local_llm(prompt):
    response = requests.post('http://localhost:1234/generate', json={
        "prompt": prompt,
        "max_tokens": 500
    })
    return response.json()['response']

# 사용 예시
result = call_local_llm("Explain machine learning in simple terms")
print(result)
Enter fullscreen mode Exit fullscreen mode

10. 성능 벤치마크

테스트 명령어:

# 128토큰 성능 테스트
./llama-bench -m ./models/Llama-3-8B-Instruct-q4_k_m.gguf -p 128 -n 100 -t 4

# 512토큰 성능 테스트
./llama-bench -m ./models/Llama-3-8B-Instruct-q4_k_m.gguf -p 512 -n 50 -t 8

# 메모리 사용량 측정
valgrind --tool=memcheck --leak-check=full ./main -m ./models/Llama-3-8B-Instruct-q4_k_m.gguf -p "test"
Enter fullscreen mode Exit fullscreen mode

예상 성능:

  • Q4_K_M: 128토큰/초 15~20
  • Q5_K_M: 128토큰/초 10~15
  • FP16: 128토큰/초 5~8

이 가이드는 실제 개발 환경에서 안정적으로 로컬 LLM을 운영할 수 있도록 설계되었습니다. 설정 후에는 다양한 작업을 병렬로 실행할 수 있으며, 필요한 경우 서비스 재시작이나 성능 조정을 통해 최적화할 수 있습니다.


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

Top comments (0)