DEV Community

matias yoon
matias yoon

Posted on

로컬 LLM 셋업 가이드 (v29)

로컬 LLM 셋업 가이드 (v29)

1. 개요 & 전제 조건

로컬 LLM(대형 언어 모델)은 클라우드 서비스 비용을 절감하고 데이터 프라이버시를 보장하는 실용적인 솔루션입니다. 이 가이드는 소비자 GPU 또는 ARM 기반 ARM64 장치에서 로컬 LLM을 실행하는 방법을 다룹니다.

시스템 요구사항:

  • CPU: 최소 4코어, 권장 8코어 이상
  • RAM: 최소 16GB, 권장 32GB 이상
  • GPU: NVIDIA GPU (CUDA 11.8 이상), 또는 ARM64 기반 장치
  • 저장공간: 최소 100GB (모델 다운로드 및 작업 공간 포함)

2. 프레임워크 비교

프레임워크 장점 단점 최적 사용 사례
llama.cpp 최소 의존성, CPU 및 GPU 모두 지원 API 미포함, 수동 설정 필요 실습 및 개발용
Ollama 간단한 API, CLI 기반 메모리 효율성이 낮음 빠른 프로토타이핑
vLLM 높은 성능, 스케일링 가능 복잡한 설치, GPU 전용 프로덕션 환경
LocalAI REST API, 다양한 프레임워크 지원 리소스 소모 큼 API 기반 애플리케이션

추천: llama.cpp + llamafile 조합 사용. 최소 의존성과 높은 호환성을 제공합니다.

3. 설치 가이드 (llama.cpp 기반)

3.1 필수 패키지 설치

# Ubuntu/Debian
sudo apt update && sudo apt install -y build-essential git cmake

# CentOS/RHEL
sudo yum groupinstall -y "Development Tools"
sudo yum install -y git cmake

# ARM64 시스템 (Orange Pi)
sudo apt update && sudo apt install -y build-essential git cmake libopenblas-dev
Enter fullscreen mode Exit fullscreen mode

3.2 llama.cpp 설치

# 소스 다운로드
git clone https://github.com/ggerganov/llama.cpp
cd llama.cpp

# 빌드 (NVIDIA GPU 사용 시)
make clean
make -j$(nproc) CUDA=1

# CPU 전용 빌드
make clean
make -j$(nproc)
Enter fullscreen mode Exit fullscreen mode

3.3 모델 다운로드 및 변환

# 모델 다운로드 (예: LLaMA-3 8B)
wget https://huggingface.co/llama3-8b/resolve/main/llama3-8b.gguf

# 모델 변환 (필요 시)
python3 convert-hf-to-gguf.py ./llama3-8b
Enter fullscreen mode Exit fullscreen mode

4. 모델 선택 가이드

모델 크기 추천 사용 사례 추천 양자화
LLaMA-3 8B 4.7GB 일반적인 챗봇, 개발 지원 Q4_K_M
Mixtral 8x7B 12GB 복잡한 작업, 코드 생성 Q5_K_M
Mistral 7B 3.8GB 빠른 응답, 일반 텍스트 분석 Q4_K_M
Phi-3 Mini 1.8GB ARM64 장치, 빠른 시작 Q4_K_M

5. 양자화 유형 설명

Q4_K_M: 최적의 성능/크기 균형. 일반적인 사용에 권장.

# Q4_K_M 양자화로 실행
./main -m ./models/llama3-8b-Q4_K_M.gguf -p "Hello, world!" -n 128
Enter fullscreen mode Exit fullscreen mode

Q5_K_M: 더 높은 정확도. 성능이 중요한 경우.

Q8_0: 최대 정확도. 메모리가 충분할 때.

6. API 설정 및 통합

6.1 API 서버 실행

# HTTP API 서버 시작 (포트 1234)
./server -m ./models/llama3-8b-Q4_K_M.gguf -c 2048 --host 0.0.0.0 --port 1234

# 자동 재시작 설정 (예: systemd)
./server -m ./models/llama3-8b-Q4_K_M.gguf -c 2048 --host 0.0.0.0 --port 1234 --log-disable
Enter fullscreen mode Exit fullscreen mode

6.2 클라이언트 통신 예제

# Python API 통신
import requests

response = requests.post("http://localhost:1234/completion", 
                        json={
                            "prompt": "Write a function to calculate Fibonacci numbers",
                            "n_predict": 100
                        })
print(response.json())
Enter fullscreen mode Exit fullscreen mode

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

7.1 서비스 파일 생성

sudo nano /etc/systemd/system/llm-local.service
Enter fullscreen mode Exit fullscreen mode
[Unit]
Description=Local 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/llama3-8b-Q4_K_M.gguf -c 2048 --host 0.0.0.0 --port 1234 --log-disable
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-local.service
sudo systemctl start llm-local.service
sudo systemctl status llm-local.service
Enter fullscreen mode Exit fullscreen mode

8. 모니터링 및 성능 조정

8.1 GPU 및 메모리 모니터링

# GPU 상태 확인 (NVIDIA)
nvidia-smi

# 메모리 사용량 확인
free -h

# CPU 사용량 확인
htop
Enter fullscreen mode Exit fullscreen mode

8.2 성능 벤치마크

# 텍스트 생성 성능 테스트
./main -m ./models/llama3-8b-Q4_K_M.gguf -p "The future of AI is" -n 512 --timing

# API 성능 테스트
ab -n 100 -c 10 http://localhost:1234/completion
Enter fullscreen mode Exit fullscreen mode

8.3 성능 최적화 설정

# 메모리 최적화 옵션
./server -m ./models/llama3-8b-Q4_K_M.gguf \
         -c 2048 \
         -ngl 33 \
         -np 1 \
         --host 0.0.0.0 \
         --port 1234
Enter fullscreen mode Exit fullscreen mode

9. 실용적 명령어 예제

9.1 텍스트 생성

# 간단한 텍스트 생성
./main -m ./models/llama3-8b-Q4_K_M.gguf \
       -p "Explain quantum computing in simple terms" \
       -n 200 \
       --color

# 대화형 모드
./main -m ./models/llama3-8b-Q4_K_M.gguf \
       -n 512 \
       --interactive
Enter fullscreen mode Exit fullscreen mode

9.2 코드 생성

# Python 코드 생성
./main -m ./models/llama3-8b-Q4_K_M.gguf \
       -p "Write a Python function to sort a list of dictionaries by key" \
       -n 300 \
       --color
Enter fullscreen mode Exit fullscreen mode

9.3 텍스트 분석

# 요약 생성
./main -m ./models/llama3-8b-Q4_K_M.gguf \
       -p "Summarize this text in 3 sentences: Machine learning is a subset of artificial intelligence that focuses on algorithms that can learn from data." \
       -n 100
Enter fullscreen mode Exit fullscreen mode

10. 고급 설정 및 트러블슈팅

10.1 메모리 최적화


bash
# 더 적은 메모리 사용 (4GB RAM 시)
./main -m ./models/llama3-8b-Q4_K_M.gguf \
       -c 1024 \
       -ngl 0 \
       -np 

---

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

Top comments (0)