- Posted on
- • Artificial Intelligence
Artificial Intelligence Virtual Environments
- Author
-
-
- User
- linuxbash
- Posts by this author
- Posts by this author
-
Artificial Intelligence Virtual Environments on Linux: Reproducible, GPU‑Aware, and Bash‑Friendly
Ever installed a single AI package and watched your system Python fall over? Or cloned a project that “works on their machine” but not on yours? AI stacks change fast, and small mismatches (CUDA version, wheel variants, compiler headers) can derail your work. This guide shows you, from the shell, how to build clean, reproducible, GPU‑aware virtual environments for AI on Linux—without polluting your OS or your sanity.
What you’ll get:
A minimal, distro‑agnostic setup that respects your base system
Clear steps to create Python virtual environments (venv or conda/mamba)
How to install the right AI framework build (CPU, CUDA, or ROCm)
Reproducibility tips that survive “it works on my machine”
A realistic, quick-start example you can paste into Bash
Why virtual environments are non‑negotiable for AI
Isolation: Keep per‑project dependencies separated; never
sudo pipsystem directories.Reproducibility: Pin exact versions and rebuild reliably on new machines.
GPU correctness: Match the right wheel to your hardware (CPU, NVIDIA CUDA, AMD ROCm).
Safety and speed: Break one project, not your OS. Cache wheels for offline or CI use.
1) System prerequisites (apt, dnf, zypper)
Install the minimal build tools, Python, and Git. Use your distro’s package manager.
Debian/Ubuntu (apt):
sudo apt update
sudo apt install -y python3 python3-venv python3-pip python3-dev build-essential git cmake pkg-config
Fedora/RHEL (dnf):
sudo dnf -y groupinstall "Development Tools"
sudo dnf -y install python3 python3-pip python3-devel git cmake pkgconf-pkg-config
openSUSE (zypper):
sudo zypper refresh
sudo zypper install -y python3 python3-pip python3-devel git cmake gcc gcc-c++ make pkgconfig
Notes:
Don’t remove your system Python. Always layer your work in user‑space virtual environments.
You can add optional GPU drivers later using vendor docs; this guide focuses on envs and wheels.
2) Create an isolated environment (two solid options)
Option A — Python’s built‑in venv (fast, simple):
mkdir -p ~/ai-labs/hello-venv && cd $_
python3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip setuptools wheel
Option B — Micromamba (Conda‑compatible, no root needed):
# Install micromamba to ~/.local/bin
curl -L https://micro.mamba.pm/api/micromamba/linux-64/latest | tar -xvj bin/micromamba
install -Dm755 bin/micromamba ~/.local/bin/micromamba
echo 'export MAMBA_ROOT_PREFIX="$HOME/.mamba"' >> ~/.bashrc
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
micromamba create -y -n ai python=3.11
micromamba activate ai
When to choose which:
Use venv if you mainly install wheels from PyPI via pip.
Use mamba/conda if you need complex native stacks (e.g., CUDA toolkits, MKL) or per‑project Python builds.
3) Install AI frameworks for your hardware (CPU, CUDA, ROCm)
First, a quick GPU check:
nvidia-smi || echo "No NVIDIA GPU or driver not installed."
Base scientific stack (inside your env):
pip install numpy scipy pandas scikit-learn jupyter matplotlib
PyTorch:
- CPU‑only:
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
- NVIDIA CUDA 12.1 wheels (requires compatible NVIDIA driver):
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
- AMD ROCm 6.0 wheels (requires ROCm runtime/driver):
pip install torch torchvision --index-url https://download.pytorch.org/whl/rocm6.0
TensorFlow:
- CPU‑only:
pip install tensorflow
- NVIDIA GPU (bundled CUDA/cuDNN wheels on Linux):
pip install "tensorflow[and-cuda]"
Verify your install:
python - <<'PY'
import platform
print("Python:", platform.python_version())
try:
import torch
print("Torch:", torch.__version__, "CUDA available:", torch.cuda.is_available())
except Exception as e:
print("Torch check:", e)
try:
import tensorflow as tf
print("TensorFlow:", tf.__version__, "GPU devices:", tf.config.list_physical_devices('GPU'))
except Exception as e:
print("TensorFlow check:", e)
PY
Tips:
Pick one primary framework per env to avoid dependency fights.
For NVIDIA, the driver version must be compatible with the CUDA version of the wheel. Check the framework’s install matrix if unsure.
4) Make it reproducible (pins, locks, and offline wheels)
Simple, quick pin:
pip freeze --exclude-editable > requirements.lock.txt
Deterministic installs with pip‑tools:
pip install pip-tools
printf "%s\n" \
"numpy" \
"pandas" \
"scikit-learn" \
"torch torchvision --index-url https://download.pytorch.org/whl/cpu" \
> requirements.in
pip-compile requirements.in # produces a fully pinned requirements.txt
pip-sync requirements.txt # installs exactly those versions
Offline/CI wheel cache:
mkdir -p wheels
pip download -r requirements.txt -d wheels
pip install --no-index --find-links=wheels -r requirements.txt
Conda/mamba equivalent (environment file):
micromamba env export -n ai --from-history > environment.yml
# Recreate:
micromamba env create -f environment.yml
General advice:
Record your Python minor version (e.g., 3.11) in README and/or env files.
Avoid mixing system, pip, and conda packages in the same env unless you know exactly why.
5) A real‑world, Bash‑first mini‑workflow
Create, install PyTorch CPU wheels, and run a tiny training loop:
mkdir -p ~/ai-labs/mnist && cd $_
python3 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu
python - <<'PY'
import torch, torchvision
from torchvision import datasets, transforms
from torch import nn, optim
ds = datasets.MNIST('/tmp/data', train=True, download=True, transform=transforms.ToTensor())
loader = torch.utils.data.DataLoader(ds, batch_size=64, shuffle=True)
model = nn.Sequential(nn.Flatten(), nn.Linear(28*28, 128), nn.ReLU(), nn.Linear(128, 10))
opt = optim.Adam(model.parameters(), lr=1e-3)
for i,(x,y) in enumerate(loader):
opt.zero_grad()
loss = nn.CrossEntropyLoss()(model(x), y)
loss.backward(); opt.step()
if i % 200 == 0: print(f"step {i} loss {loss.item():.4f}")
if i == 400: break
print("Training OK")
PY
pip freeze --exclude-editable > requirements.lock.txt
deactivate
Quality‑of‑life shell helper (add to ~/.bashrc):
ae() { test -d .venv && . .venv/bin/activate || echo "No .venv here."; }
Now in any project with a .venv, just run ae to activate.
Common pitfalls (and easy fixes)
Using
sudo pip: Don’t. Use virtual environments.Wrong wheel channel: Match CPU vs CUDA vs ROCm correctly (see Step 3).
Missing build headers: Ensure the “System prerequisites” step completed for your distro.
Mixing conda and pip haphazardly: Prefer one package manager per env; if you must mix, install conda packages first, pip second.
Conclusion and next steps
A clean virtual environment is the foundation of every reliable AI project on Linux. You now have:
A distro‑native base setup
Two solid env strategies (venv or micromamba)
Hardware‑appropriate framework installs
Reproducibility patterns that scale from laptop to CI
Your call to action:
1) Pick one project you care about and build a fresh env today.
2) Pin it (pip‑tools or freeze), cache the wheels, and document the Python version.
3) Share your requirements.txt or environment.yml with your team and make reproducible builds the default.
If you want a follow‑up, consider containerizing your env (Podman/Docker) with your pinned requirements for bulletproof CI and deployment.