<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Local-First AI on Jamal Hansen</title><link>https://jamalhansen.com/series/local-first-ai/</link><description>Recent content in Local-First AI on Jamal Hansen</description><image><title>Jamal Hansen</title><url>https://jamalhansen.com/social-card.png</url><link>https://jamalhansen.com/social-card.png</link></image><generator>Hugo</generator><language>en-us</language><lastBuildDate>Thu, 09 Apr 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://jamalhansen.com/series/local-first-ai/index.xml" rel="self" type="application/rss+xml"/><item><title>Your Local AI Stack: uv and Ollama in 10 Minutes</title><link>https://jamalhansen.com/blog/local-ai-stack-uv-ollama/</link><pubDate>Fri, 10 Apr 2026 00:00:00 +0000</pubDate><guid>https://jamalhansen.com/blog/local-ai-stack-uv-ollama/</guid><description>How to run a local LLM from a Python script without an API key, a virtual environment, or Docker. You need two tools and one file.</description><content:encoded><![CDATA[<p>How do you run a local LLM from a Python script? Install Ollama, pull a model, install uv, write one file with inline dependencies, and run it. No API key. No virtual environment to activate. No Docker. The whole setup takes under ten minutes.</p>
<h2 id="why-run-local">Why run local</h2>
<p>Three reasons: cost, privacy, and offline access.</p>
<p>Frontier APIs charge per token. For experimentation, prototyping, and batch tasks, those costs add up before you have anything to show. A local model costs nothing per call.</p>
<p>Your data does not leave your machine. For notes, drafts, and anything you would not paste into a public chat, that matters.</p>
<p>And the model works when you are on a plane, in a basement, or anywhere without reliable internet.</p>
<h2 id="install-ollama">Install Ollama</h2>
<p>Ollama runs models locally and exposes a simple API your Python code can call. It works on Mac, Windows, and Linux.</p>
<p>Install it at <a href="https://ollama.com/download">ollama.com/download</a> and follow the instructions for your platform.</p>
<h2 id="pull-a-model">Pull a model</h2>
<p>Once Ollama is running, pull a model from your terminal:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">ollama pull llama3.2:3b
</span></span></code></pre></div><p><code>llama3.2:3b</code> is about 2GB and runs on most laptops. It is a reasonable starting point.</p>
<p>Models range from under 1GB to 70B+. Smaller models are faster but struggle with complex reasoning. Larger models produce better output but need substantially more RAM. A 70B model needs 40GB or more. Ollama&rsquo;s model library at <a href="https://ollama.com/library">ollama.com/library</a> lets you filter by size and see what fits your machine. Check before pulling something large.</p>
<h2 id="install-uv">Install uv</h2>
<p>uv is a Python package and project manager. It is my favorite tool for Python work.</p>
<p>Install it from <a href="https://docs.astral.sh/uv/getting-started/installation/">docs.astral.sh/uv</a>. The page has one-line commands for Mac, Windows, and Linux.</p>
<h2 id="run-a-script">Run a script</h2>
<p>This is where it comes together. You do not need a project directory or an active virtual environment. You can write a single file with its dependencies declared inline.</p>
<p>This is PEP 723, inline script metadata. Add a <code># /// script</code> block at the top of any Python file and uv reads it before running. It installs the listed dependencies into a temporary environment automatically.</p>
<p>Create a file called <code>ask.py</code>:</p>
<!-- test:skip -->
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-python" data-lang="python"><span class="line"><span class="cl"><span class="c1"># /// script</span>
</span></span><span class="line"><span class="cl"><span class="c1"># requires-python = &#34;&gt;=3.11&#34;</span>
</span></span><span class="line"><span class="cl"><span class="c1"># dependencies = [</span>
</span></span><span class="line"><span class="cl"><span class="c1">#     &#34;ollama&#34;,</span>
</span></span><span class="line"><span class="cl"><span class="c1"># ]</span>
</span></span><span class="line"><span class="cl"><span class="c1"># ///</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kn">import</span> <span class="nn">ollama</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="n">response</span> <span class="o">=</span> <span class="n">ollama</span><span class="o">.</span><span class="n">chat</span><span class="p">(</span>
</span></span><span class="line"><span class="cl">    <span class="n">model</span><span class="o">=</span><span class="s2">&#34;llama3.2:3b&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="n">messages</span><span class="o">=</span><span class="p">[{</span><span class="s2">&#34;role&#34;</span><span class="p">:</span> <span class="s2">&#34;user&#34;</span><span class="p">,</span> <span class="s2">&#34;content&#34;</span><span class="p">:</span> <span class="s2">&#34;What is the difference between a list and a tuple in Python?&#34;</span><span class="p">}]</span>
</span></span><span class="line"><span class="cl"><span class="p">)</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="nb">print</span><span class="p">(</span><span class="n">response</span><span class="p">[</span><span class="s1">&#39;message&#39;</span><span class="p">][</span><span class="s1">&#39;content&#39;</span><span class="p">])</span>
</span></span></code></pre></div><p>Run it:</p>
<!-- test:skip -->
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">uv run ask.py
</span></span></code></pre></div><p>uv reads the dependency block, installs <code>ollama</code> into a temporary environment, and runs the script. The first run takes a few seconds while it sets up. Subsequent runs are fast.</p>
<p>You should see the model&rsquo;s response in your terminal. That is a local LLM answering a Python question with no API key and no activated environment.</p>
<h2 id="try-it-yourself">Try It Yourself</h2>
<p>Pull <code>llama3.2:3b</code> and run the script above. Then change the prompt to something from your actual work. See how it handles it.</p>
<p>If <code>llama3.2:3b</code> is too slow on your machine, try something smaller from <a href="https://ollama.com/library">ollama.com/library</a>. If you want better output and have the RAM to spare, pull a larger variant.</p>
<p>The patterns I write about in this series use this exact setup. Getting it running now means you can run every code example as you read it.</p>
<hr>
<h2 id="appendix-building-a-full-project">Appendix: Building a full project</h2>
<p>The single-file method works well for exploration and one-off scripts. When a script grows into something you will maintain, test, or share, use <code>uv init</code> instead.</p>
<!-- test:skip -->
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">uv init my-ai-project
</span></span><span class="line"><span class="cl"><span class="nb">cd</span> my-ai-project
</span></span><span class="line"><span class="cl">uv add ollama
</span></span></code></pre></div><p>This creates a <code>pyproject.toml</code>, a <code>uv.lock</code>, and a managed virtual environment. You add dependencies with <code>uv add</code> and run code with <code>uv run</code>. Dependencies are tracked in <code>pyproject.toml</code> instead of the script header.</p>
<p>The <code>ollama</code> calls are identical either way. Use the single-file approach when you are exploring. Switch to <code>uv init</code> when the project deserves to outlive a single session.</p>
<h2 id="more-info">More info</h2>
<p><strong>Tools</strong></p>
<ul>
<li><a href="https://ollama.com">Ollama</a> &ndash; run large language models locally on Mac, Windows, and Linux</li>
<li><a href="https://docs.astral.sh/uv/">uv</a> &ndash; a fast Python package and project manager</li>
<li><a href="https://github.com/ollama/ollama-python">Ollama Python library</a> &ndash; the official Python client for the Ollama API</li>
<li><a href="https://ollama.com/library">Ollama model library</a> &ndash; browse and filter available models by size and capability</li>
</ul>
<p><strong>Further reading</strong></p>
<ul>
<li><a href="https://peps.python.org/pep-0723/">PEP 723 &ndash; Inline Script Metadata</a> &ndash; the specification that defines the <code># /// script</code> block format used in this post</li>
<li><a href="https://docs.astral.sh/uv/">uv documentation</a> &ndash; full reference for uv commands, project management, and configuration</li>
</ul>
]]></content:encoded></item></channel></rss>