Blog · AI
MLX LM Tutorial: Run Local LLMs on Mac
Install mlx-lm in a venv, generate and chat with local models, pick and quantise models from mlx-community, try LoRA fine-tuning, and keep memory under control.

This MLX LM tutorial shows you how to run local LLMs on a Mac with Apple's mlx-lm package, from a clean install to a quantised model and a small LoRA fine-tune. Every command here comes from the official mlx-lm documentation.
What you need
- A Mac with Apple silicon running macOS 14 or later (macOS 15 or later is needed for mlx-lm's memory wiring of large models).
- A native arm64 Python. At the time of writing, current mlx-lm releases on PyPI require Python 3.11 or newer, so install one with Homebrew rather than relying on the system Python.
- Disk space for models. A 4-bit 7B model is several gigabytes.
New to MLX itself? Start with What Is Apple MLX? for the background.
Install mlx-lm in a virtual environment
A virtual environment keeps MLX and its dependencies away from other projects. In zsh, with Homebrew at /opt/homebrew:
brew install [email protected]
python3.12 -m venv ~/mlx-env
source ~/mlx-env/bin/activate
pip install -U pip mlx-lm
mlx_lm.generate --prompt "Explain unified memory in one paragraph."
The last line downloads the default model (at the time of writing mlx-community/Llama-3.2-3B-Instruct-4bit) and prints a reply. If it works, MLX and Metal are set up correctly. Run source ~/mlx-env/bin/activate in every new terminal before using the commands below.
Generate text from the command line
mlx_lm.generate runs one prompt and exits. Choose a model with --model and cap the output with --max-tokens:
mlx_lm.generate \
--model mlx-community/Mistral-7B-Instruct-v0.3-4bit \
--prompt "Write a haiku about build servers." \
--max-tokens 200
Other useful flags: --temp and --top-p for sampling, --system-prompt, and --seed for repeatable output. Run mlx_lm.generate -h for the full, current list.
Chat with a local model
mlx_lm.chat opens a chat REPL. The conversation context is kept for the life of the session.
mlx_lm.chat --model mlx-community/Mistral-7B-Instruct-v0.3-4bit
Inside the REPL, type q to quit, r to reset the conversation and h for help.
For scripts, use the Python API. This streams tokens as they are generated:
from mlx_lm import load, stream_generate
model, tokenizer = load("mlx-community/Mistral-7B-Instruct-v0.3-4bit")
messages = [{"role": "user", "content": "Summarise MLX in two sentences."}]
prompt = tokenizer.apply_chat_template(messages, add_generation_prompt=True)
for r in stream_generate(model, tokenizer, prompt, max_tokens=256):
print(r.text, end="", flush=True)
print()
Pick and download a quantised model
The mlx-community organisation on Hugging Face hosts thousands of models already converted for MLX. Repo names usually end in -4bit, -8bit or similar. How to choose:
- Start with 4-bit. It is the usual balance of quality and memory for local use. Try 8-bit if you have memory to spare.
- Prefer instruct or chat variants for assistants. Base models are for completion and fine-tuning.
- Size to your memory. A 4-bit model needs roughly 0.5–0.7 GB per billion parameters plus context. On 16GB, 7–8B models are comfortable. On 24GB, up to about 14B. On 36GB, about 30B class. On 96GB, 70B-class models. It depends on context length.
Models download on first use into the Hugging Face cache (~/.cache/huggingface by default; set HF_HOME to move it). To see and clean up what you have:
mlx_lm.manage --scan
mlx_lm.manage --delete --pattern Mistral
Some repos need --trust-remote-code for their tokenizer. Only use it with models you trust.
Convert and quantise a model yourself
If the model you want is not in mlx-community, convert it from its original Hugging Face repo. -q turns on quantisation and --q-bits sets bits per weight:
mlx_lm.convert \
--model mistralai/Mistral-7B-Instruct-v0.3 \
-q --q-bits 4 \
--mlx-path ./mistral-7b-4bit
mlx_lm.generate --model ./mistral-7b-4bit --prompt "hello"
The output is a normal folder you can pass to any mlx-lm command. Add --upload-repo to push the result to your own Hugging Face repo. Some original repos are gated, so you may need to accept the licence and log in to Hugging Face first. mlx_lm.convert --help lists other options, including mixed-bit recipes and --q-group-size.
LoRA fine-tuning basics
mlx-lm supports LoRA, DoRA and full fine-tuning. If --model points to a quantised model, training uses QLoRA, which saves a lot of memory. Install the training extras and create a data folder:
pip install "mlx-lm[train]"
mkdir -p data
The data folder needs a train.jsonl and, optionally, a valid.jsonl for validation loss. Supported formats include chat, completions, text and tools. One chat example per line:
{"messages": [{"role": "user", "content": "What is our refund window?"}, {"role": "assistant", "content": "30 days from delivery."}]}
Train, then test the adapter:
mlx_lm.lora \
--model mlx-community/Mistral-7B-Instruct-v0.3-4bit \
--train --data ./data --iters 600 \
--batch-size 1 --num-layers 8 --grad-checkpoint
mlx_lm.generate \
--model mlx-community/Mistral-7B-Instruct-v0.3-4bit \
--adapter-path adapters \
--prompt "What is our refund window?"
Adapters are saved to adapters/ by default. When you are happy, merge them into a standalone model (saved to fused_model/ by default):
mlx_lm.fuse --model mlx-community/Mistral-7B-Instruct-v0.3-4bit
The full guide, including --mask-prompt, YAML configs and evaluation with --test, is in LORA.md.
Memory tips
- Cap the KV cache on long chats with
--max-kv-size. Smaller values use less memory but lower quality. - Lower the prefill step with
--prefill-step-size(default 2048) to reduce peak memory while reading long prompts. - Quantise the KV cache with
--kv-bits 4or8for long contexts. - For training, lower
--batch-size(default 4), reduce--num-layers(default 16), and add--grad-checkpoint. - Watch for the large model warning. If mlx-lm warns that a model is large for the machine, the docs suggest raising the wired memory limit on macOS 15 or later. Set N above the model size in megabytes but below total memory, and leave room for the system.
mlx_lm.generate --model mlx-community/Mistral-7B-Instruct-v0.3-4bit \
--max-kv-size 4096 --prefill-step-size 512 \
--prompt "Summarise the following notes..."
sudo sysctl iogpu.wired_limit_mb=N
For more on how memory and context interact, see local LLMs on Apple silicon: memory.
Troubleshooting common problems
- pip cannot find a matching version. Check that you are using a native arm64 Python of a supported version. Run
python -c "import platform; print(platform.machine())"inside the venv. It should printarm64. If it printsx86_64, the Python or the terminal is running under Rosetta. - Command not found. The
mlx_lm.*commands live in the virtual environment. Activate it again, or call them by full path, for example~/mlx-env/bin/mlx_lm.generate. - Very slow output or heavy swapping. The model is too big for the memory left after macOS and other apps. Close memory-hungry apps, pick a smaller or lower-bit model, or shorten the context.
- Odd or repeated chat output. Make sure you are using an instruct model with a chat template. Base models do not follow instructions well.
- Download errors. Check the repo name on Hugging Face, and whether it is gated and needs a licence accepted first.
Running mlx-lm on a cloud Mac
Need more memory than your laptop has, or a Mac that keeps running while you are away? Hatmac rents dedicated Mac mini and Mac Studio machines with a full macOS desktop and terminal, so everything in this tutorial works as written. A Mac mini M6 with 24GB suits 7–14B models. A Mac Studio M5 Ultra with 96GB suits 70B-class models. See the Mac mini LLM server page or compare plans and pricing. To serve your model over an API, continue with How to Set Up an MLX Server on Mac.