AI Basics · Lesson 2 · 25 min

Run your own AI on Linux

Chatbots usually run on someone else's giant computers. But smaller AI models can run on your own Linux machine: no account, no internet once they're downloaded, and nothing leaves your computer. Let's set one up.

You will learn

  • What “running a model locally” means, and what hardware you need
  • How to install Ollama and chat with a model
  • How to call the model from the command line and from a Python script
  • The Python setup difference between Rocky and Ubuntu (it trips up almost everyone)
You'll need a real machine for this one

AI models need several gigabytes of memory, so this lesson can't run in the practice terminal. Use a Linux computer, a virtual machine, or WSL on Windows. The practice terminal at the bottom lets you rehearse the Python setup part.

What you need

Check what you have (same on both):

Same on both
free -h      # memory: look at the "total" column
df -h ~      # free disk space in your home folder
nproc        # number of CPU cores

Step 1: install Ollama

Ollama is a free tool that downloads AI models and runs them for you. Its installer is the same on both families:

Same on both
curl -fsSL https://ollama.com/install.sh | sh

Wait, remember the red flag from the last lesson? curl … | sh runs a script from the internet without anyone reading it. Ollama is a well-known project, but the habit of reading first is what keeps you safe. Here's the careful version:

Same on both (the careful way)
curl -fsSL https://ollama.com/install.sh -o install-ollama.sh
less install-ollama.sh     # skim it: what does it download, where does it put things? (q to quit)
sh install-ollama.sh

What the flags mean: -f fail on errors, -s silent, -S still show errors, -L follow redirects, -o save to a file.

The installer asks for your sudo password, and it notices whether you're on Rocky or Ubuntu by itself. It also sets up Ollama as a service. That's Linux Basics, lesson 14 knowledge!

Same on both
systemctl status ollama

If the installer complains that a tool is missing, install it (sudo dnf install NAME on Rocky, sudo apt install NAME on Ubuntu) and run it again.

Step 2: chat with a model

Same on both
ollama run llama3.2

The first time, this downloads the model (about 2 GB for the 3-billion-parameter llama3.2), then gives you a >>> prompt. Ask it anything, for example “explain what cd .. does to a 10-year-old.” Type /bye to leave.

CommandWhat it does
ollama listShow the models you've downloaded
ollama pull NAMEDownload a model without starting a chat
ollama psShow which models are loaded in memory right now
ollama rm NAMEDelete a model to free disk space

Only 8 GB of RAM? Try the smaller llama3.2:1b (about 1.3 GB). It's faster, but less clever. Bigger models are smarter and need more memory. Picking one is always that trade-off.

Remember lesson 1

Small local models make mistakes more often than big online ones. Everything from the last lesson applies double: check commands with man before you run them.

Step 3: talk to it from the command line

Ollama runs a small web service on your own machine, on port 11434. Programs talk to it with plain web requests, and you can too, using curl:

Same on both
curl http://localhost:11434/api/generate -d '{
  "model": "llama3.2",
  "prompt": "In one sentence, what does the Linux ls command do?",
  "stream": false
}'

You get back JSON (structured text), with the answer in the "response" field. This is exactly how apps talk to AI models, including the big cloud ones.

Security

By default Ollama only listens on localhost, so other computers can't reach it. Keep it that way. There's no password on port 11434, so don't open it in your firewall.

Step 4: script it with Python

Now the fun part: your own AI program. First, set up Python. This is where the families differ:

Rocky / RHEL
sudo dnf install -y python3-pip

Rocky's python3 already includes venv (virtual environments), so you only add pip.

Ubuntu / Debian
sudo apt update
sudo apt install -y python3-venv python3-pip

Ubuntu splits venv into its own package. Without it, python3 -m venv fails with an “ensurepip is not available” error.

Next, make a virtual environment. It's a private folder for the Python packages of one project, so they can't clash with the system's own Python. On Ubuntu it's required: if you try pip install outside one, you get an externally-managed-environment error. On Rocky it's just good practice.

Same on both
python3 -m venv ~/ai-env           # create it (once)
source ~/ai-env/bin/activate       # switch it on; your prompt now starts with (ai-env)
pip install ollama                 # the official Ollama Python library

Now create a file called ask.py (with nano ask.py) and paste this in:

import sys
import ollama

question = " ".join(sys.argv[1:]) or "What does the ls command do?"

reply = ollama.chat(
    model="llama3.2",
    messages=[
        {"role": "system", "content": "You are a friendly Linux tutor for high school students. Keep answers short. Always mention if a command differs between Rocky Linux and Ubuntu."},
        {"role": "user", "content": question},
    ],
)
print(reply["message"]["content"])
Run it
python3 ask.py "how do I install tree?"

That system message is your AI's personality and rules. Change it and see how the answers change. That's the core idea behind every AI app.

Bonus project: the error explainer

Remember pipes from Linux Basics, lesson 12? Make explain.py. It reads whatever is piped into it and asks the AI to explain it:

import sys
import ollama

text = sys.stdin.read()
reply = ollama.chat(
    model="llama3.2",
    messages=[{"role": "user", "content": "Explain this Linux output to a beginner, and say if anything looks wrong:\n\n" + text}],
)
print(reply["message"]["content"])
Use it
ls /root 2>&1 | python3 explain.py
systemctl status sshd 2>&1 | python3 explain.py

2>&1 means “send error messages down the pipe too.” Normally errors skip the pipe and go straight to the screen.

When you're done, type deactivate to leave the virtual environment.

Rehearse the Python setup

The practice terminal can't run AI models, but it does copy the Python differences, error messages included. Try it on both families.

Where to go next

Quick check

1. On Ubuntu, python3 -m venv ai-env fails with “ensurepip is not available.” The fix?

2. Why should you keep Ollama's port 11434 closed in your firewall?

3. What's the benefit of running a model locally instead of using an online chatbot?

Finished the missions and the quiz? Mark it done to track your progress.

Next up: AWS basics · Linux in the cloud, starting with “The cloud & AWS: getting started”.