DEV Community

matias yoon
matias yoon

Posted on

로컬 LLM 셋업 가이드 (v39)

로컬 LLM 셋업 가이드 (v39)

로컬에서 LLM을 실행하는 것은 데이터 프라이버시, 성능, 비용 절감의 이점을 제공하지만, 복잡한 설치 과정과 모델 선택, 최적화가 필요한 실전 기술이 필요합니다. 이 가이드는 실용적인 접근 방식으로 로컬 LLM 셋업을 단계별로 안내합니다.


1. 개요 및 사전 요구사항

대상: Linux 환경에서 로컬 LLM을 실행하고 싶은 개발자

필수 하드웨어:

  • GPU: NVIDIA GPU (CUDA 지원) 권장 (RTX 30xx 이상 권장)
  • RAM: 최소 16GB, 32GB 이상 권장
  • 저장공간: 모델 용량의 2~3배 이상 (예: 7B 모델은 14~21GB 필요)
  • OS: Ubuntu 20.04 이상 또는 Debian 11 이상

필요한 도구들:

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

2. 프레임워크 비교

프레임워크 장점 단점 추천 사용 사례
llama.cpp 최소 의존성, C++ 기반, 빠른 시작 사용자 인터페이스 제한 실시간 API 서버, 개발 테스트
Ollama 쉬운 설치 및 관리, GUI 지원 확장성 제한, 복잡한 설정 불가 빠른 프로토타이핑, 데스크탑 사용
vLLM 높은 성능, 다중 GPU 지원 설치 복잡, 모델 제한 빠른 추론, 대규모 서비스
LocalAI 다양한 API 호환성 (OpenAI API) 리소스 사용량 높음 기존 API 통합 필요 시

3. 권장 설정: llama.cpp + systemd + Q4_K_M 모델

llama.cpp 설치:

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

모델 다운로드:

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

기본 실행 명령:

./main -m models/llama-3-8b-instruct.Q4_K_M.gguf -p "Qwen은 어떤 모델인가요?" --temp 0.7
Enter fullscreen mode Exit fullscreen mode

API 서버 실행:

./server -m models/llama-3-8b-instruct.Q4_K_M.gguf -c 2048 --host 0.0.0.0 --port 1234
Enter fullscreen mode Exit fullscreen mode

4. 모델 선택 가이드

모델 파라미터 수 추천 사용 사례 용량
Llama-3 8B 8B 일반적인 추론, 개발 테스트 ~4.5GB
Llama-3 70B 70B 고성능, 대규모 작업 ~14GB (Q4_K_M)
Mistral 7B 7B 빠른 추론, 저사양 환경 ~4GB
Phi-3 Mini 3.8B 가벼운 로컬 모델, 컨트롤러 ~2GB

실전 예시:

# Phi-3 Mini 실행 (가벼운 모델)
./main -m models/phi-3-mini.Q4_K_M.gguf -p "Hello, world!" --temp 0.2
Enter fullscreen mode Exit fullscreen mode

5. 양자화 유형 설명

양자화 설명 성능 용량
Q4_K_M 최적화된 4비트 양자화 높음 최소
Q5_K_M 5비트 최적화, 정밀도 높음 중간 중간
Q6_K 6비트, 정밀도 높음 낮음 많음
F16 반정밀도 (float16) 가장 높음 가장 많음

예시 명령:

# 모델 변환 (Q4_K_M)
python convert.py models/llama-3-8b-instruct.gguf --outtype q4_k_m --outfile llama-3-8b-instruct.Q4_K_M.gguf
Enter fullscreen mode Exit fullscreen mode

6. API 설정 및 도구 통합

OpenAI API 호환 서버:

# OpenAI API 호환 서버 실행
./server -m models/llama-3-8b-instruct.Q4_K_M.gguf --host 0.0.0.0 --port 1234 --api-key your_api_key
Enter fullscreen mode Exit fullscreen mode

Python API 테스트:

import openai

client = openai.OpenAI(
    base_url="http://localhost:1234/v1",
    api_key="sk-1234"
)

response = client.chat.completions.create(
    model="llama3",
    messages=[{"role": "user", "content": "Hello, world!"}],
    temperature=0.7
)

print(response.choices[0].message.content)
Enter fullscreen mode Exit fullscreen mode

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

서비스 파일 생성:

sudo nano /etc/systemd/system/llama.service
Enter fullscreen mode Exit fullscreen mode

내용:

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

[Service]
User=your_user
WorkingDirectory=/home/your_user/llama.cpp
ExecStart=/home/your_user/llama.cpp/server -m /home/your_user/llama.cpp/models/llama-3-8b-instruct.Q4_K_M.gguf --host 0.0.0.0 --port 1234
Restart=always

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

서비스 시작:

sudo systemctl enable llama.service
sudo systemctl start llama.service
sudo systemctl status llama.service
Enter fullscreen mode Exit fullscreen mode

8. 성능 모니터링 및 최적화

리소스 사용률 확인:

# GPU 메모리 확인
nvidia-smi

# CPU/메모리 사용률
htop

# 로그 확인
journalctl -u llama.service -f
Enter fullscreen mode Exit fullscreen mode

최적화 팁:

  • --ctx-size로 컨텍스트 길이 조절 (예: 2048)
  • --threads로 CPU 스레드 수 조절
  • --n-gpu-layers로 GPU 레이어 수 조절 (GPU 사용)

예시 명령:

./server -m models/llama-3-8b-instruct.Q4_K_M.gguf -c 2048 --threads 8 --n-gpu-layers 30 --host 0.0.0.0 --port 1234
Enter fullscreen mode Exit fullscreen mode

9. 성능 벤치마크

llama.cpp 벤치마크 실행:

# 1000개 토큰 추론
./main -m models/llama-3-8b-instruct.Q4_K_M.gguf -p "Hello world!" --temp 0.0 --n-predict 1000
Enter fullscreen mode Exit fullscreen mode

대략 성능 결과 (RTX 4090 기준):

  • Q4_K_M: 150~200 토큰/초
  • Q5_K_M: 130~180 토큰/초
  • F16: 80~100 토큰/초

API 성능 테스트:

curl -X POST http://localhost:1234/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
  "model": "llama3",
  "messages": [{"role": "user", "content": "Hello"}],
  "temperature": 0.7
}'
Enter fullscreen mode Exit fullscreen mode

결론

로컬 LLM 셋업은 데이터 프라이버시와 성능을 동시에 충족시키는 실용적인 솔루션입니다. llama.cpp 기반으로 Q4_K_M 모델을 사용하면 CPU나 GPU


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

Top comments (0)