🤖

Commands

2 notes  •  IoT & AI

Set Up a GPU-Accelerated Python Environment on Ubuntu

This guide sets up a Conda-based Python environment for GPU-accelerated deep learning on Ubuntu, installing TensorFlow, PyTorch, and common data science libraries.

Prerequisites

  • Ubuntu server with an NVIDIA GPU
  • NVIDIA drivers installed (nvidia-smi should show the GPU)
  • Miniconda installed (see below if not yet installed)

Install Miniconda

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh
bash miniconda.sh
conda config --set auto_activate_base false

Install CUDA Toolkit

apt-get install nvidia-cuda-toolkit

Create the Environment and Install Packages

# Create and activate environment
conda create -n aiml_env python=3.10 -y
conda activate aiml_env

# Install TensorFlow with CUDA support
conda install tensorflow[and-cuda]

# Install data science packages
conda install numpy pandas matplotlib scikit-learn jupyter

# Install PyTorch with CUDA 11.8
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 \
  -c pytorch -c nvidia

Verify GPU Access

# Check GPU is visible
lspci | grep -i nvidia

# Verify PyTorch sees the GPU
python -c "import torch; print(torch.cuda.is_available(), torch.cuda.get_device_name(0))"

Launch Jupyter Notebook

conda activate aiml_env
jupyter notebook --ip=0.0.0.0 --no-browser

Notes

  • Accept the Anaconda terms of service if prompted: conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/main
  • Match the pytorch-cuda version to your installed CUDA version (nvcc --version).
  • For a deep learning-focused environment, use Python 3.9 with pip: conda create -n deeplearning python=3.9 -y && pip install tensorflow[and-cuda] notebook

Create Directories on Boot with systemd-tmpfiles

For services that need runtime directories (e.g. Unix sockets for ML API workers), systemd-tmpfiles is the modern approach to creating them at boot — replacing ad-hoc init scripts or rc.local hacks.

Steps

sudo nano /etc/tmpfiles.d/ml-apis.conf

Add a line for each directory you need. Format: type path mode user group age

# d = create directory
# 0770 = rwxrwx--- (user+group full access, others none)
# azureuser = directory owner
# www-data = group owner (allows Nginx to read/write)
# - = no age limit (don't auto-delete)
d /run/ml_api       0770 azureuser www-data -
d /run/annotationapi 0770 azureuser www-data -

Apply Without Rebooting

sudo systemd-tmpfiles --create

Verify

ls -la /run/ml_api /run/annotationapi

Both directories should exist with the specified ownership and permissions.

Notes

  • Directories under /run are ephemeral — they are recreated at every boot by systemd-tmpfiles automatically.
  • This is how packages like screen manage their runtime dirs (/etc/tmpfiles.d/screen.conf).
  • To delete old files automatically, set the age field (e.g. 10d for 10 days).