- Posted on
- • Artificial Intelligence
Learning Artificial Intelligence on Linux from Scratch
- Author
-
-
- User
- linuxbash
- Posts by this author
- Posts by this author
-
Learning Artificial Intelligence on Linux from Scratch (A Bash‑first Guide)
Want to turn your Linux box into a fully capable AI lab without leaving the terminal? Good. Most production AI runs on Linux, and the shell is your superpower. The challenge is knowing where to start without drowning in complicated toolchains or GUI wizards. This guide gives you a clear, minimal, and reproducible path from zero to your first working model—entirely on Linux, entirely from the command line.
What you’ll get:
A clean Linux AI setup using your package manager (apt, dnf, or zypper)
A Python environment for AI/ML you can reproduce and share
A first working model with scikit‑learn and a quick sanity check for PyTorch
Shell-friendly workflows to automate experiments
Why this matters:
Control and reproducibility: Everything is scripted and versioned.
Performance and portability: Linux is the backbone of AI in the wild.
Cost and focus: Learn core concepts with free, open-source tools and no vendor lock‑in.
1) Prepare your Linux system (prereqs you actually need)
Install Python, Git, compilers, and CMake. These cover 90% of what you’ll build in AI/ML and keep Python packages happy.
Debian/Ubuntu (apt):
sudo apt update
sudo apt install -y python3 python3-pip python3-venv git build-essential cmake
Fedora/RHEL/CentOS Stream (dnf):
sudo dnf upgrade -y
sudo dnf groupinstall -y "Development Tools"
sudo dnf install -y python3 python3-pip git cmake
openSUSE Leap/Tumbleweed (zypper):
sudo zypper refresh
sudo zypper install -y python3 python3-pip git cmake
sudo zypper install -y -t pattern devel_basis
Quick checks:
python3 --version
pip3 --version
git --version
gcc --version
cmake --version
2) Create an isolated Python environment and install core AI packages
Use Python’s built‑in venv to avoid polluting your system Python. Then install the scientific stack (NumPy, Pandas, Matplotlib, scikit‑learn) and JupyterLab.
Create and activate a venv:
python3 -m venv ~/.venvs/ai
source ~/.venvs/ai/bin/activate
python -m pip install --upgrade pip
Install core packages:
pip install numpy pandas matplotlib scikit-learn jupyterlab
Verify the install:
python - <<'PY'
import numpy, pandas, sklearn, matplotlib
print("OK:", numpy.__version__, pandas.__version__, sklearn.__version__, matplotlib.__version__)
PY
Tip: Keep things reproducible.
pip freeze --local > requirements.txt
# Later (or on another machine):
# python3 -m venv ~/.venvs/ai && source ~/.venvs/ai/bin/activate
# pip install -r requirements.txt
3) Choose a framework: start with CPU PyTorch (simple and cross‑distro)
GPU is great later; start with CPU to learn the basics quickly and avoid driver issues. You can switch to GPU once you’re comfortable.
Install CPU‑only PyTorch:
pip install --index-url https://download.pytorch.org/whl/cpu torch torchvision torchaudio
Sanity check:
python - <<'PY'
import torch
print("Torch:", torch.__version__)
print("CUDA available?", torch.cuda.is_available())
PY
If you do have an NVIDIA GPU and want acceleration later, follow your distro’s official NVIDIA/CUDA guides and then reinstall PyTorch per the official selector. Starting CPU‑only avoids setup friction.
4) Your first end‑to‑end model in minutes (CLI only)
We’ll train a simple classifier on the classic Iris dataset using scikit‑learn, save the model, and run a prediction—all from the shell.
Create a script named iris_train.py:
cat > iris_train.py <<'PY'
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
from sklearn.metrics import accuracy_score
import joblib
X, y = load_iris(return_X_y=True, as_frame=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42, test_size=0.2)
pipe = make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000))
pipe.fit(X_train, y_train)
pred = pipe.predict(X_test)
acc = accuracy_score(y_test, pred)
print(f"Test accuracy: {acc:.3f}")
joblib.dump(pipe, "iris_model.joblib")
print("Saved model to iris_model.joblib")
PY
Install joblib if needed and run:
pip install joblib
python iris_train.py
Make a quick prediction script:
cat > iris_predict.py <<'PY'
import sys, joblib
import numpy as np
# Expect 4 floats: sepal_len sepal_wid petal_len petal_wid
if len(sys.argv) != 5:
print("Usage: python iris_predict.py 5.1 3.5 1.4 0.2")
sys.exit(1)
x = np.array([[float(a) for a in sys.argv[1:]]])
model = joblib.load("iris_model.joblib")
print("Predicted class:", model.predict(x)[0])
PY
python iris_predict.py 5.1 3.5 1.4 0.2
Real‑world angle: This pattern—train, evaluate, serialize—mirrors production: train offline, ship a model artifact, load it to serve predictions.
5) Workflows that feel native to Linux (Jupyter, Bash, and automation)
- Use JupyterLab for exploration (still runs on localhost, launched from your shell):
jupyter lab
- Automate runs with Bash to compare seeds or hyperparameters:
cat > run_exp.sh <<'SH'
#!/usr/bin/env bash
set -euo pipefail
mkdir -p results
SEEDS="${*:-0 1 2 3 4}"
for s in $SEEDS; do
echo "[INFO] Running seed $s"
python - <<PY > "results/run_${s}.log"
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.metrics import accuracy_score
import numpy as np, random, os
seed = ${s}
np.random.seed(seed); random.seed(seed)
X, y = load_iris(return_X_y=True)
Xtr, Xte, ytr, yte = train_test_split(X, y, random_state=seed, test_size=0.2)
model = make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000))
model.fit(Xtr, ytr)
print("seed", seed, "acc", round(accuracy_score(yte, model.predict(Xte)), 4))
PY
done
echo "[INFO] Logs in ./results"
SH
chmod +x run_exp.sh
./run_exp.sh
- Lock environments for teammates:
pip freeze --local > requirements.txt
git init && git add iris_*.py run_exp.sh requirements.txt && git commit -m "Minimal Linux AI starter"
- Bonus: quick dataset handling with wget/curl, environment variables, and Makefiles all integrate nicely with this approach.
Optional: distro package refreshers (when you add tools later)
You’ll often install utilities like wget, curl, or graphviz. Here’s how to grab them quickly.
Debian/Ubuntu (apt):
sudo apt update
sudo apt install -y wget curl graphviz
Fedora/RHEL/CentOS Stream (dnf):
sudo dnf install -y wget curl graphviz
openSUSE Leap/Tumbleweed (zypper):
sudo zypper install -y wget curl graphviz
Conclusion and next steps (CTA)
You’ve built a Linux‑native AI workflow: clean system deps, an isolated Python environment, a trained model, and a shell‑driven automation pattern. From here:
Extend the example to a real dataset (CSV via wget, then Pandas).
Try a small PyTorch model on CPU to learn training loops and tensors.
Capture your environment in requirements.txt and share your repo.
When you’re comfortable, add GPU acceleration following your distro’s official NVIDIA/CUDA instructions and PyTorch’s install selector.
Your terminal is now an AI lab. Keep it simple, keep it scripted, and ship models you can reproduce anywhere.