Run Phi-4 locally on a Linux L40S server
Serve Microsoft’s original BF16 Phi-4 on one owned NVIDIA L40S with vLLM 0.31.0, a 2,048-token context and one active sequence. The guide pins model files, keeps the endpoint on loopback, and includes verification and removal steps. Documented setup; not executed or benchmarked by OpenTools.
- Hardware
- One NVIDIA L40S in a Linux x86_64 server
- Runtime
- vLLM 0.31.0+cu129 · CUDA
- Model format
- safetensors · BF16 · None; original BF16 weights
- Workload
- 2,048 token context · 1 concurrent
Based on source documentation; this setup has not been executed by OpenTools. Memory: unknown. No compatible measurement available.
Read the complete setup guide
Before you start
- An already provisioned Linux x86_64 host with one NVIDIA L40S (manufacturer-labeled 48 GB dedicated memory); no claim that this is a tested minimum.
- Python 3.12 with virtual-environment support, curl, sha256sum, outbound HTTPS to the official package/model hosts, and a working CUDA-compatible NVIDIA driver.
- The full repository download is about 29.33 GB. Allow additional disk for the virtual environment, package cache and runtime files; complete disk and RAM minima have not been measured.
- A trusted single-user deployment: this guide binds vLLM to 127.0.0.1. Do not publish port 8000 or treat the API key as protection for every vLLM route.
Exact model and requirements
- Artifact:
- Microsoft / Phi-4 · checkpoint microsoft/phi-4
- Publisher revision:
- 2db69c1c3e91a05d2c64a3185acfbaf36f744e25
- Artifact digest:
- The publisher exposes per-file hashes, not one digest for the complete multi-file artifact. Exact weight SHA256 and auxiliary Git blob hashes are listed below.
- Download size:
- 27.31 GiB
- Memory requirement (unknown):
- Unknown — Complete GPU footprint has not been measured: model weights, KV cache, runtime allocations, compilation and workspace must all fit.
- System RAM / disk:
- Unknown / Unknown
- Placement:
- single device. One GPU; no tensor or pipeline sharding and no pooling of GPU and host RAM. No CPU or disk weight offload is enabled.
- KV cache:
- BF16 via --kv-cache-dtype auto with BF16 model dtype
- Workload:
- 2,048 token context; Unknown; Unknown; concurrency 1; batch 1; cache unknown; repetitions Unknown.
- Tokenizer:
- microsoft/phi-4 · 2db69c1c3e91a05d2c64a3185acfbaf36f744e25
- Sampling / reasoning:
- Verification uses temperature 0 and max_tokens 64. The service uses --generation-config vllm; this is not a throughput protocol. Original Phi-4 checkpoint, not a Phi-4 reasoning variant; no special reasoning parser or budget is configured.
- Runtime prerequisites:
- Linux x86_64; Python 3.12; Working NVIDIA driver R580 or newer; CUDA 12.x applications remain supported by newer drivers through backward compatibility. Verify the selected environment with nvidia-smi and torch.cuda.is_available()..
The publisher declares MIT. Read the pinned LICENSE and retain its copyright and permission notice when redistributing covered material.
- model-00001-of-00006.safetensors · 4.59 GiB · revision 2db69c1c3e91a05d2c64a3185acfbaf36f744e25
- model-00002-of-00006.safetensors · 4.61 GiB · revision 2db69c1c3e91a05d2c64a3185acfbaf36f744e25
- model-00003-of-00006.safetensors · 4.57 GiB · revision 2db69c1c3e91a05d2c64a3185acfbaf36f744e25
- model-00004-of-00006.safetensors · 4.44 GiB · revision 2db69c1c3e91a05d2c64a3185acfbaf36f744e25
- model-00005-of-00006.safetensors · 4.44 GiB · revision 2db69c1c3e91a05d2c64a3185acfbaf36f744e25
- model-00006-of-00006.safetensors · 4.64 GiB · revision 2db69c1c3e91a05d2c64a3185acfbaf36f744e25
- added_tokens.json · 0 GiB · revision 2db69c1c3e91a05d2c64a3185acfbaf36f744e25
- config.json · 0 GiB · revision 2db69c1c3e91a05d2c64a3185acfbaf36f744e25
- generation_config.json · 0 GiB · revision 2db69c1c3e91a05d2c64a3185acfbaf36f744e25
- merges.txt · 0 GiB · revision 2db69c1c3e91a05d2c64a3185acfbaf36f744e25
- model.safetensors.index.json · 0 GiB · revision 2db69c1c3e91a05d2c64a3185acfbaf36f744e25
- special_tokens_map.json · 0 GiB · revision 2db69c1c3e91a05d2c64a3185acfbaf36f744e25
- tokenizer.json · 0 GiB · revision 2db69c1c3e91a05d2c64a3185acfbaf36f744e25
- tokenizer_config.json · 0 GiB · revision 2db69c1c3e91a05d2c64a3185acfbaf36f744e25
- vocab.json · 0 GiB · revision 2db69c1c3e91a05d2c64a3185acfbaf36f744e25
- The six weight files total 29,319,042,992 bytes; that is a download measurement from publisher metadata, not the runtime GPU-memory requirement.
- The selected L40S is labeled 48 GB by the vendor. Dedicated GPU memory and system RAM are different pools and are not summed.
- No measured peak memory, completed-run evidence, tokens per second or latency result is supplied. A documented runtime architecture match does not prove this exact deployment fits.
One-time setup and hardware
- Hardware purchase
- Unknown — No dated hardware purchase quote is available; existing ownership is not treated as a zero purchase price.
- No dated hardware purchase quote is available; existing ownership is not treated as a zero purchase price. Tax: unknown.
- Initial setup work
- Unknown — Initial setup time is not priced; no labour rate or duration has been measured.
- Initial setup time is not priced; no labour rate or duration has been measured. Tax: unknown.
Recurring costs
- Electricity
- Unknown — Whole-system measured wall power, powered-on hours and the reader’s electricity tariff are unknown. GPU maximum board power is not a measured system draw.
- Whole-system measured wall power, powered-on hours and the reader’s electricity tariff are unknown. GPU maximum board power is not a measured system draw. Tax: unknown.
- Operations and maintenance
- Unknown — Ongoing patching and operation effort is not measured or priced.
- Ongoing patching and operation effort is not measured or priced. Tax: unknown.
- Persistent storage
- Unknown — The owned host storage arrangement and any recurring backup or storage bill are unknown; no monthly amount is asserted.
- The owned host storage arrangement and any recurring backup or storage bill are unknown; no monthly amount is asserted. Tax: unknown.
- Data transfer
- Unknown — The reader’s internet or network plan and traffic volume are unknown; loopback requests alone do not establish total network costs.
- The reader’s internet or network plan and traffic volume are unknown; loopback requests alone do not establish total network costs. Tax: unknown.
Install the runtime
1. Check the existing GPU host
Use an already provisioned Linux x86_64 server with one L40S, Python 3.12 with venv support, curl, sha256sum, and a working NVIDIA R580-or-newer driver. Driver provisioning is a server-administrator prerequisite; this guide does not replace the host driver. Keep the machine on a trusted network and ensure port 8000 is unused.
Commandnvidia-smi python3.12 --version df -h "$HOME"Expected result: The L40S is visible, Python reports 3.12, and there is enough free space for the 29.33 GB model download plus the Python environment, package cache and runtime workspace.
2. Create an isolated environment and install pinned vLLM
Run in a new shell on the GPU host. The exact CUDA 12.9 vLLM wheel is pinned by release and publisher-provided SHA256. Dependencies resolve inside this fresh environment; do not mix the AMI or host Python environment into it. The system CUDA toolkit version need not equal the bundled wheel runtime. If python3.12 -m venv is unavailable, install your OS vendor’s Python 3.12 venv package first.
Commandmkdir -p "$HOME/phi4-hosting" cd "$HOME/phi4-hosting" python3.12 -m venv .venv source .venv/bin/activate python -m pip install --upgrade pip python -m pip install --extra-index-url https://download.pytorch.org/whl/cu129 'https://github.com/vllm-project/vllm/releases/download/v0.31.0/vllm-0.31.0%2Bcu129-cp38-abi3-manylinux_2_28_x86_64.whl#sha256=5176ab746f4e736c4a354a11d0620abf0a941de287cad937ac34039293ca6f5f' python -m pip check vllm --version python -c 'import torch; print("CUDA available:", torch.cuda.is_available()); print("GPU:", torch.cuda.get_device_name(0) if torch.cuda.is_available() else "not detected")'Expected result: pip check reports no broken requirements; vllm reports version 0.31.0 (the wheel may include +cu129); CUDA available is True and the GPU is NVIDIA L40S. This is an expected check for the reader, not a result observed by OpenTools.
Download the model
1. Download the exact Microsoft snapshot
Run after installing the runtime so its Hugging Face dependency is available. The full commit pins weights, configuration and tokenizer together; this does not follow the moving main branch. The official repository was ungated when checked. No model-provider API subscription is needed for this download.
Commandcd "$HOME/phi4-hosting" source .venv/bin/activate python - <<'PY' from huggingface_hub import snapshot_download snapshot_download(repo_id="microsoft/phi-4", revision="2db69c1c3e91a05d2c64a3185acfbaf36f744e25", local_dir="./model") PYExpected result: The command completes and ./model contains config.json, tokenizer files, the safetensors index and all six weight shards from the pinned revision.
2. Verify every weight shard
The SHA256 values come from the official repository’s file metadata. Stop if any shard is missing or mismatched; do not proceed with a mixed or partial download.
Commandcd "$HOME/phi4-hosting" sha256sum --check <<'SHA256' cafbe72210c39b19270cbfce27996afc4383c8a487087bcb91a6290d5ceb8f18 model/model-00001-of-00006.safetensors 9f87a5897162ed988cfb0fac13879634b4bab3da5a756b4f9dd2437ef8145e20 model/model-00002-of-00006.safetensors 19537df78a263aa0c0b2dc12dbce5908021e30a25e528699b08642c3999eeb05 model/model-00003-of-00006.safetensors d4f17100a39c19d352f2e8108eef6fb975afb86808e50d78a075e162f1edfc93 model/model-00004-of-00006.safetensors a72666b6001d76e7a9dc3ebe0d05a2c3316f546a8c6779c87343f296bdf82e50 model/model-00005-of-00006.safetensors a9bfeeea354e31503d09a864e6cd5bd0f1230664065b6a2988b05e22b3228d77 model/model-00006-of-00006.safetensors SHA256Expected result: All six listed safetensors files report OK.
Configure
1. Create a private local service key
Run on the GPU host. This writes a fresh random key only when the file is absent, keeps it mode 600 and exports it without printing it. Treat the key file as private. vLLM API-key checks do not cover every route, so keep the loopback binding used below.
Commandcd "$HOME/phi4-hosting" source .venv/bin/activate umask 077 python - <<'PY' import secrets from pathlib import Path key = Path(".vllm-api-key") if not key.exists(): key.write_text(secrets.token_urlsafe(32)) key.chmod(0o600) PY export VLLM_API_KEY="$(cat .vllm-api-key)"Expected result: A private .vllm-api-key file exists in the dedicated directory; no secret is printed. The shell environment contains VLLM_API_KEY.
Start the model
1. Start the private Phi-4 endpoint
Keep this foreground process in its own terminal. ./model is the verified pinned snapshot, so both weights and tokenizer are loaded from it. BF16 weights and automatic BF16 KV cache are used; there is no quantization or weight offload. One sequence is scheduled at a time. Startup can require compilation and workspace; do not infer success until the verification requests pass.
Commandcd "$HOME/phi4-hosting" source .venv/bin/activate export VLLM_API_KEY="$(cat .vllm-api-key)" vllm serve ./model \ --served-model-name phi4-local \ --host 127.0.0.1 --port 8000 \ --dtype bfloat16 --kv-cache-dtype auto \ --max-model-len 2048 --max-num-seqs 1 \ --tensor-parallel-size 1 --pipeline-parallel-size 1 \ --generation-config vllmExpected result: The process remains running and announces an HTTP listener on 127.0.0.1:8000 after model initialization. If it exits, use the troubleshooting checks before making any success or performance claim.
Connect to your endpoint
http://127.0.0.1:8000/v1
Run clients and verification on the GPU host. Only that host loopback is bound. Remote clients need a separately controlled SSH tunnel; do not change --host to a public interface for convenience.
Access: loopback. VLLM_API_KEY is a reader-generated local service key for /v1, /v2 and /inference paths. Other paths may remain unauthenticated; the loopback boundary is essential. Other users on the same host can reach loopback, so use a trusted single-user machine.
Verify the setup
1. Check the model identity
Open a second terminal on the same GPU host. Load the local key without displaying it and call the authenticated models endpoint.
Commandcd "$HOME/phi4-hosting" export VLLM_API_KEY="$(cat .vllm-api-key)" curl --fail --silent --show-error http://127.0.0.1:8000/v1/models \ -H "Authorization: Bearer $VLLM_API_KEY"Expected result: The JSON response contains the served model id phi4-local. The endpoint responds successfully only after startup completes.
2. Request a short response
In that second terminal, send a small chat request. This checks response generation; it is not a benchmark and provides no throughput or latency guarantee.
Commandcurl --fail --silent --show-error http://127.0.0.1:8000/v1/chat/completions \ -H "Authorization: Bearer $VLLM_API_KEY" \ -H 'Content-Type: application/json' \ -d '{"model":"phi4-local","messages":[{"role":"user","content":"Explain what a GPU does in one sentence."}],"max_tokens":64,"temperature":0}'Expected result: A successful JSON response includes a nonempty choices[0].message.content. Wording varies; no exact answer or speed is guaranteed.
3. Verify the access boundary
Check that the listener is only on 127.0.0.1:8000 and that the /v1 route rejects a missing key. For AWS, keep the security group closed to inbound 8000 and use the SSH tunnel for remote access.
Commandss -ltn | grep '127.0.0.1:8000' curl --silent --output /dev/null --write-out '%{http_code}\n' http://127.0.0.1:8000/v1/modelsExpected result: The listener is on loopback and the unauthenticated /v1/models request is denied (normally HTTP 401). This denial does not prove that every server path is authenticated.
Performance evidence
No compatible measurement available. Documented support and estimated memory do not establish response speed.
Limits and troubleshooting
- This is a documented setup assembled from official model, hardware, runtime and cloud documentation. OpenTools has not executed these commands or verified runtime fit on the selected hardware.
- The model supports a maximum 16,384-token context in its configuration; this recipe deliberately sets a smaller combined prompt-and-output context. Raising context or concurrency changes the configuration and memory demand.
- Original Phi-4 can produce incorrect or unsafe output. It is not the Phi-4 mini or reasoning checkpoint. Apply the model publisher’s intended-use and limitation guidance.
- Dependency packages installed with the pinned vLLM wheel are resolved at install time; capture pip freeze with your run report when reproducibility beyond the pinned runtime wheel is required.
- Loopback is accessible to other users on the same machine. Use a trusted host. A publicly reachable production service needs a separate authenticated gateway and access design outside this single-user recipe.
- nvidia-smi fails or PyTorch reports CUDA unavailable
- Check the physical/virtual GPU and host driver before downloading weights. For the AWS path verify the exact supported G6e AMI. Use a fresh environment and the pinned CUDA 12.9 wheel; do not mix a pre-existing torch installation into it.
- Installation or download runs out of disk space
- Check the filesystem containing $HOME/phi4-hosting and package caches. The model download alone is about 29.33 GB; add space for the environment and runtime files. Resume the same pinned snapshot and repeat all shard checks.
- Model loading or runtime initialization runs out of GPU or host memory
- Stop other GPU workloads and inspect nvidia-smi. Complete memory fit is unknown. Lowering context, changing precision or moving to different hardware creates a different configuration; do not report that as a successful run of this exact recipe.
- Port 8000 is already used
- Stop the conflicting service you own or choose another loopback port consistently in the start command, checks and SSH tunnel. Do not expose a public interface to work around a local port conflict.
- A /v1 request returns 401
- In the client terminal load the same private .vllm-api-key file used by the server and send Authorization: Bearer. Restart the server after deliberately rotating the key.
Stop the model
1. Stop the model process
Press Ctrl-C in the terminal running vllm and wait for shutdown. Close any SSH forwarding process with Ctrl-C. Then check from the host terminal that the endpoint no longer answers. Stopping only the process does not stop an EC2 bill.
Commandcurl --connect-timeout 3 http://127.0.0.1:8000/v1/modelsExpected result: The request cannot connect after the listener has stopped. Confirm no other process has taken the same port before assuming the host is idle.
Uninstall
1. Remove only this guide’s environment and files
After stopping the server, preserve any needed outputs, then remove the dedicated $HOME/phi4-hosting directory you created, including its virtual environment, downloaded model and private key. Check the path before deleting; do not remove shared model caches or another project. Remove any copied client key as well. The host NVIDIA driver is not removed.
Commanddeactivate unset VLLM_API_KEYExpected result: The dedicated directory is removed when you choose to delete it; the shell no longer uses the virtual environment or service key. Shared host configuration remains separate.
Sources and verification
Sources checked Oct 15, 2026. Verification source
- https://huggingface.co/microsoft/phi-4/raw/2db69c1c3e91a05d2c64a3185acfbaf36f744e25/README.mdpublisher · retrieved Oct 15, 2026 · revision 2db69c1c3e91a05d2c64a3185acfbaf36f744e25
- https://www.nvidia.com/en-us/data-center/l40s/manufacturer · retrieved Oct 15, 2026
- https://docs.vllm.ai/en/v0.31.0/models/supported_models/runtime maintainer · retrieved Oct 15, 2026 · revision v0.31.0
- https://docs.vllm.ai/en/v0.31.0/cli/serve/runtime maintainer · retrieved Oct 15, 2026 · revision v0.31.0
- https://huggingface.co/api/models/microsoft/phi-4?blobs=truepublisher · retrieved Oct 15, 2026 · revision 2db69c1c3e91a05d2c64a3185acfbaf36f744e25
- https://huggingface.co/microsoft/phi-4/resolve/2db69c1c3e91a05d2c64a3185acfbaf36f744e25/config.jsonpublisher · retrieved Oct 15, 2026 · revision 2db69c1c3e91a05d2c64a3185acfbaf36f744e25
- https://api.github.com/repos/vllm-project/vllm/releases/latestruntime maintainer · retrieved Oct 15, 2026 · dated Oct 5, 2026 · revision v0.31.0
- https://docs.vllm.ai/en/v0.31.0/getting_started/installation/gpu/runtime maintainer · retrieved Oct 15, 2026 · revision v0.31.0
- https://docs.vllm.ai/en/v0.31.0/getting_started/quickstart/runtime maintainer · retrieved Oct 15, 2026 · revision v0.31.0
- https://docs.nvidia.com/deploy/cuda-compatibility/minor-version-compatibility.htmlmanufacturer · retrieved Oct 15, 2026
- https://huggingface.co/docs/huggingface_hub/guides/downloadofficial · retrieved Oct 15, 2026