Set-up · 20 minutes
Set up your AI security lab
- Create the lab folder for the course
- Reuse or create the virtual environment with the pinned library versions
- Prove with a check script that the lab works
The lab folder as it stands after this lesson. Unzip it in ~/krlab.
More
The zip holds every file that the course has printed up to here. Type the code yourself first: you learn more.
What you need
This course follows four others. Take them first, or know what they teach.
| Course | What this course uses from it |
|---|---|
| Python | Reading short programs: lists, dictionaries, f-strings and list comprehensions (the Python course, lessons 1 to 3) |
| Security fundamentals | Assets, threats, risk, and the threat model with STRIDE |
| How AI works | Training a model with scikit-learn, accuracy, recall, overfitting |
| How LLMs and agents work | Prompts, tools, and running a model on your own machine |
You also need a computer with Linux, macOS or Windows (WSL), a terminal and a text editor. You need no GPU and no administrator rights. Python 3.12 or newer is required, because the pinned libraries need it. About 500 MB of free disk space and an internet connection are needed for the install. After that, every lesson runs offline.
Lesson 5 also needs a small language model that runs on your own machine (the program Ollama and the model llama3.2:3b, from the course "How LLMs and agents work"). The other lessons do not need it. Everything runs on your own computer. Never point a tool of this course at a system that is not yours.
Set it up
1. Make the lab folder
mkdir -p (make directory) creates a folder and the folders above it. cd (change directory) moves you into it. pwd (print working directory) shows where you are.
mkdir -p ~/krlab/ai-security-fundamentals
cd ~/krlab/ai-security-fundamentals
pwd/home/you/krlab/ai-security-fundamentals2. Look for the environment of "How AI works"
A virtual environment is a private folder of Python packages, so that the packages of a course cannot break the Python of your system. The lessons of this course use the one from "How AI works", in ~/krlab/how-ai-works/.venv. ls lists a folder. If it lists a folder, go on to step 4.
ls ~/krlab/how-ai-works/.venvbin include lib lib64 pyvenv.cfgIf you see No such file or directory, you did not take that course. Do step 3.
3. Create the environment (only if step 2 found none)
python3 -m venv .venv makes the environment. A pinned version is an exact one, written with ==. Pinning matters because a model is numbers, and the numbers can change between versions of a library. With the same versions, you get the same numbers as the author, and the answers that the lessons check are the same for you. The lines between <<'EOF' and EOF are written into the file requirements.txt (a heredoc). pip install -r installs everything that the file lists (pip is the installer of Python packages). source runs the activation script in your shell; step 4 says more.
mkdir -p ~/krlab/how-ai-works
cd ~/krlab/how-ai-works
python3 -m venv .venv
cat > requirements.txt <<'EOF'
scikit-learn==1.9.1
numpy==2.5.5
scipy==1.18.1
joblib==1.6.0
EOF
source .venv/bin/activate
pip install -r requirements.txtSuccessfully installed cloudpickle-3.1.3 joblib-1.6.0 narwhals-2.29.0 numpy-2.5.5 scikit-learn-1.9.1 scipy-1.18.1 threadpoolctl-3.8.04. Switch the environment on
source runs the activation script in your current shell. From then on python means the Python of the environment. Run the source line at the start of every lesson that uses scikit-learn (lessons 2 and 3). Those two lessons use the command python. The other lessons use only the standard library, so python3 is enough there.
source ~/krlab/how-ai-works/.venv/bin/activate
cd ~/krlab/ai-security-fundamentals
python --versionPython 3.14.45. Write the check script
The script labcheck.py asks four questions. It checks the Python version, that scikit-learn loads with the pinned major and minor number (the first two parts of its version), and that the digits data loads (1,797 small pictures of handwritten digits). Last, it asks whether a model server answers on your machine. That answer is only information, because only lesson 5 needs it, so it does not count in the total. sys.version_info is the version of the running Python, sklearn.__version__ is the version text of a library, and urllib.request.urlopen fetches an address (here, your own machine at port 11434, the port of Ollama). sys.exit ends the program with a status: 0 means success.
Create labcheck.py in the lab folder.
labcheck.py
import json
import sys
import urllib.request
results = []
def report(ok, text):
results.append(ok)
print(("ok " if ok else "FAIL ") + text)
v = sys.version_info
report(v >= (3, 12), f"python {v.major}.{v.minor}.{v.micro} (3.12 or later)")
try:
import sklearn
major_minor = ".".join(sklearn.__version__.split(".")[:2])
report(major_minor == "1.9", f"scikit-learn {sklearn.__version__} (the lessons use 1.9.x)")
except ImportError:
report(False, "scikit-learn is not installed in this environment")
try:
from sklearn.datasets import load_digits
report(load_digits().data.shape == (1797, 64), "digits data loads: 1797 pictures")
except Exception as error:
report(False, "digits data did not load: " + str(error))
try:
with urllib.request.urlopen("http://127.0.0.1:11434/api/tags", timeout=3) as reply:
models = [m["name"] for m in json.load(reply)["models"]]
state = "is" if "llama3.2:3b" in models else "is NOT"
print(f"info a model server answers; llama3.2:3b {state} installed (lesson 5 needs it)")
except Exception:
print("info no model server answers (only lesson 5 needs one)")
print(f"lab ready: {sum(results)} of {len(results)}")
sys.exit(0 if all(results) else 1)Check your lab
python runs a Python file.
python labcheck.pyok python 3.14.4 (3.12 or later)
ok scikit-learn 1.9.1 (the lessons use 1.9.x)
ok digits data loads: 1797 pictures
info a model server answers; llama3.2:3b is installed (lesson 5 needs it)
lab ready: 3 of 3Your Python version can differ, and so can the info line: without a model server it says no model server answers. That is fine until lesson 5. The last line must say 3 of 3. Type the last line of your own output, and the version of scikit-learn that it printed.
The course page has a link "Lab files": a zip with every file of the course. In the unzipped folder, sh kr-lab.sh LESSON FOLDER builds the lab folder as it stands after a lesson, for example sh kr-lab.sh 10-build-the-security-file ~/krlab/ai-security-fundamentals. You need it only if you lose your files.
If it fails
Read the first line that starts with FAIL. Fix that one, and run python labcheck.py again.
| The script says | What it means | What to do |
|---|---|---|
FAIL python ... | Python is older than 3.12. | Install a newer Python from your package manager, then repeat step 3 with that python3. |
FAIL scikit-learn is not installed | The environment is not switched on, or it has no packages. | Run the source line of step 4. If it still fails, run pip install -r requirements.txt in ~/krlab/how-ai-works. |
FAIL scikit-learn 1.8.0 ... | A different version is installed. | In ~/krlab/how-ai-works, run pip install -r requirements.txt again. It replaces the version with the pinned one. |
FAIL digits data did not load | The library is damaged, or an old version is installed. | Reinstall with pip install --force-reinstall -r requirements.txt. |
python: command not found | The environment is not switched on. | Run the source line of step 4. On some systems use python3. |
info no model server answers | Ollama is not running. | Not an error for lessons 1 to 4 and 6 to 11. Before lesson 5, start Ollama and install the model, as in "How LLMs and agents work", lesson 3. |
If pip cannot find the pinned versions on your system, use the newest versions that it offers. The script will then print FAIL for the version line, and the page will not accept 3 of 3. Some numbers in the lessons can then differ in the last digit. A difference in the first two digits means an error in your code.