Lokale KI-/Data-Science-Umgebung
In diesem Artikel wird beschrieben, wie Sie eine vollständige lokale KI- und Data-Science-Entwicklungsumgebung auf Ubuntu 26.04 erstellen.
NVIDIA CUDA Umgebungseinrichtung
Für die Ausführung GPU-beschleunigter KI-Modelle müssen zunächst die Treiber NVIDIA und das Toolkit CUDA installiert werden.
Installieren Sie die NVIDIA-Treiber
# Check the recommended driver version
ubuntu-drivers devices
# Install the recommended driver
sudo ubuntu-drivers autoinstall
# Or manually install the version reported by ubuntu-drivers devices
sudo apt install nvidia-driver-XXX -y
# Reboot
sudo rebootÜberprüfen Sie den Treiber:
nvidia-smiInstallieren Sie das CUDA Toolkit
# Install via apt (Ubuntu repository version)
sudo apt install cuda-toolkit -y
# Verify
nvcc --versionInstallieren Sie für eine bestimmte CUDA-Version aus dem offiziellen Repository von NVIDIA (Ubuntu 26.04 verwendet ubuntu2604):
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2604/x86_64/cuda-keyring_1.1-1_all.deb
sudo dpkg -i cuda-keyring_1.1-1_all.deb
sudo apt update
# List available CUDA Toolkit versions in the repository
apt-cache search cuda-toolkit
# Install a specific version (replace XX-Y with an actual available version)
sudo apt install cuda-toolkit-XX-Y -yUbuntu 26.04 liefert jetzt das NVIDIA CUDA Toolkit in seinen offiziellen Repositories aus, sodass sudo apt install cuda-toolkit direkt funktioniert. Das ubuntu2604-Repository von NVIDIA bleibt nützlich, wenn Sie eine bestimmte CUDA-Version benötigen.
Umgebungsvariablen konfigurieren:
echo 'export PATH=/usr/local/cuda/bin:$PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrccuDNN installieren
sudo apt install libcudnn9-cuda-12 libcudnn9-dev-cuda-12 -yOllama: Führen Sie große Sprachmodelle lokal aus
Mit Ollama können Sie große Sprachmodelle wie Llama, Mistral und Gemma problemlos lokal ausführen.
Installieren Sie Ollama
curl -fsSL https://ollama.com/install.sh | shGrundlegende Verwendung
# Run a model (downloads automatically on first use)
ollama run llama3.2
# Run other models
ollama run mistral
ollama run gemma2
ollama run qwen2.5
ollama run deepseek-r1
# List downloaded models
ollama list
# Delete a model
ollama rm llama3.2
# View running models
ollama psOllama-API
Ollama stellt eine REST-API bereit, die standardmäßig unter http://localhost:11434 ausgeführt wird:
# Generate text
curl http://localhost:11434/api/generate -d '{
"model": "llama3.2",
"prompt": "Explain what machine learning is"
}'
# Chat conversation
curl http://localhost:11434/api/chat -d '{
"model": "llama3.2",
"messages": [
{"role": "user", "content": "Hello"}
]
}'Zur Verwendung mit Open WebUI
Open WebUI bietet eine ChatGPT-ähnliche Weboberfläche für Ollama:
docker run -d -p 3000:8080 \
--add-host=host.docker.internal:host-gateway \
-v open-webui:/app/backend/data \
--name open-webui \
ghcr.io/open-webui/open-webui:mainGreifen Sie unter http://localhost:3000 darauf zu.
Jupyter-Notizbuch / Labor
Jupyter ist die beliebteste interaktive Entwicklungsumgebung in den Bereichen Datenwissenschaft und maschinelles Lernen.
Installieren Sie JupyterLab
# Ubuntu 26.04 blocks system-wide pip installs (PEP 668)
# Method 1: install as a standalone app with pipx
sudo apt install -y pipx
pipx ensurepath
pipx install jupyterlab
# Method 2: install inside a virtual environment
python3 -m venv ~/jupyter-env
source ~/jupyter-env/bin/activate
pip install jupyterlab notebook
# Method 3: use conda
conda install -c conda-forge jupyterlabStarten Sie JupyterLab
jupyter labIhr Browser öffnet automatisch http://localhost:8888.
Installieren Sie Common Kernels
# Install R kernel
sudo apt install r-base -y
R -e "install.packages('IRkernel'); IRkernel::installspec()"
# Install a kernel for a specific virtual environment
python -m ipykernel install --user --name myenv --display-name "Python (myenv)"Empfohlene Jupyter-Erweiterungen
# Install in the same virtual environment as JupyterLab (or inject via pipx)
# pipx users can run: pipx inject jupyterlab jupyterlab-lsp python-lsp-server jupyterlab-git
pip install jupyterlab-lsp python-lsp-server
pip install jupyterlab-gitPyTorch
PyTorch ist derzeit das beliebteste Deep-Learning-Framework.
PyTorch (GPU) installieren
# Install inside a virtual environment (PEP 668)
python3 -m venv ~/torch-env
source ~/torch-env/bin/activate
# pip install torch installs the wheel for the current CUDA by default;
# older drivers can pin a lower CUDA, e.g. cu126
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126
# Using conda
conda install pytorch torchvision torchaudio pytorch-cuda=12.6 -c pytorch -c nvidiaPyTorch (CPU) installieren
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpuÜberprüfen Sie die PyTorch-GPU-Unterstützung
import torch
print(f"PyTorch version: {torch.__version__}")
print(f"CUDA available: {torch.cuda.is_available()}")
print(f"CUDA version: {torch.version.cuda}")
print(f"GPU device: {torch.cuda.get_device_name(0)}")
# Simple test
x = torch.rand(3, 3).cuda()
print(x)TensorFlow
Installieren Sie TensorFlow
# GPU version (auto-detects CUDA)
pip install tensorflow
# Verify
python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"TensorFlow GPU-Überprüfung
import tensorflow as tf
print(f"TensorFlow version: {tf.__version__}")
print(f"GPU devices: {tf.config.list_physical_devices('GPU')}")
# Simple test
with tf.device('/GPU:0'):
a = tf.constant([[1.0, 2.0], [3.0, 4.0]])
b = tf.constant([[5.0, 6.0], [7.0, 8.0]])
c = tf.matmul(a, b)
print(c)Gemeinsame Data Science-Pakete
# Activate a virtual environment first (PEP 668: no pip into the system Python)
python3 -m venv ~/ds-env && source ~/ds-env/bin/activate
pip install numpy pandas matplotlib seaborn scikit-learn scipy
pip install plotly bokeh altair
pip install polars # High-performance alternative to pandas
pip install xgboost lightgbm catboost # Gradient boosting frameworksVollständiges Beispiel für eine KI-Entwicklungsumgebung
Hier ist ein empfohlener Arbeitsablauf für die Einrichtung der Projektumgebung:
# Create project directory
mkdir ~/ai-project && cd ~/ai-project
# Create conda environment (pip is safe inside a conda environment)
conda create -n ai python=3.13 -y
conda activate ai
# Install core libraries (current CUDA by default; older drivers can pin cu126)
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126
pip install transformers datasets accelerate
pip install jupyterlab
pip install numpy pandas matplotlib scikit-learn
# Launch Jupyter
jupyter labVerwendung von Hugging Face Transformers
pip install transformers datasets acceleratefrom transformers import pipeline
# Text generation
generator = pipeline("text-generation", model="gpt2")
result = generator("The future of artificial intelligence is", max_length=50)
print(result)
# Sentiment analysis
classifier = pipeline("sentiment-analysis")
result = classifier("This product is amazing")
print(result)Überwachen Sie die GPU-Nutzung
# Real-time monitoring
watch -n 1 nvidia-smi
# Use nvitop (more visually appealing) -- install as a standalone CLI with pipx
pipx install nvitop
nvitop
# Use gpustat
pipx install gpustat
gpustat -i 1Wenn Ihre lokale KI-Umgebung eingerichtet ist, können Sie verschiedene große Sprachmodelle und Deep-Learning-Aufgaben auf Ubuntu 26.04 ausführen.