Skip to content

Instantly share code, notes, and snippets.

@amir-saniyan
Last active November 12, 2024 12:21
Show Gist options
  • Save amir-saniyan/b3d8e06145a8569c0d0e030af6d60bea to your computer and use it in GitHub Desktop.
Save amir-saniyan/b3d8e06145a8569c0d0e030af6d60bea to your computer and use it in GitHub Desktop.

Ubuntu 22.04 for Deep Learning

In the name of God

This gist contains steps to setup Ubuntu 22.04 for deep learning.


Install Ubuntu 22.04

  • Computer name: Name-PC
  • Name: Name
  • User name: name
  • Password: ********

Update Ubuntu

$ sudo apt update
$ sudo apt full-upgrade --yes
$ sudo apt autoremove --yes
$ sudo apt autoclean --yes
$ reboot

Create Update Script

  • Create a file (~/full-update.sh) with the following lines:
#!/usr/bin/env bash

if [ "$EUID" -ne 0 ]
  then echo "Error: Please run as root."
  exit
fi

clear

echo "################################################################################"
echo "Updating list of available packages..."
echo "--------------------------------------------------------------------------------"
apt update
echo "################################################################################"
echo

echo "################################################################################"
echo "Upgrading the system by removing/installing/upgrading packages..."
echo "--------------------------------------------------------------------------------"
apt full-upgrade --yes
echo "################################################################################"
echo

echo "################################################################################"
echo "Removing automatically all unused packages..."
echo "--------------------------------------------------------------------------------"
apt autoremove --yes
echo "################################################################################"
echo

echo "################################################################################"
echo "Clearing out the local repository of retrieved package files..."
echo "--------------------------------------------------------------------------------"
apt autoclean --yes
echo "################################################################################"
echo

Change Settings

  • Review Ubuntu Settings

Change Software & Updates

  • Review Software & Updates

Update Ubuntu

  • $ sudo ~/full-update.sh

  • $ sudo apt install ./google-chrome-stable_current_amd64.deb

Install Development Tools

  • $ sudo apt install build-essential pkg-config cmake ninja-build

Install Python 3

  • $ sudo apt install python3 python3-venv python3-pip python3-dev python3-setuptools python3-wheel

Install Git

$ sudo apt install git
$ git config --global user.name "Name"
$ git config --global user.email "[email protected]"
$ git config --global core.editor "gedit -s"
  • Copy your own SSH keys to ~/.ssh/

Install NVIDIA Drivers for Deep Learning

Check Display Hardware:

  • $ sudo lshw -C display

Install NVIDIA GPU Driver:

  • Install from GUI: Software & Updates > Additional Drivers > NVIDIA

Try $ sudo ubuntu-drivers autoinstall if NVIDIA drivers are disabled.

You can also install it from the terminal: $ sudo apt install nvidia-driver-535.

Check TensorFlow and CUDA Compatibilities:

Install CUDA Toolkit (CUDA 11.8):

  1. Install prerequisites:
    • $ sudo apt install linux-headers-$(uname -r)
  2. Download CUDA 11.8 (https://developer.nvidia.com/cuda-toolkit-archive)
    • $ wget https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda_11.8.0_520.61.05_linux.run
  3. Install CUDA 11.8: $ sudo ./cuda_11.8.0_520.61.05_linux.run --override (without Driver)
  4. Set up the development environment by modifying the PATH and LD_LIBRARY_PATH variables (Add following lines to ~/.bashrc):
    • export PATH=$PATH:/usr/local/cuda-11.8/bin
    • export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-11.8/lib64:/usr/local/cuda-11.8/extras/CUPTI/lib64
  5. Check that GPUs are visible using the following command (A reboot may be required):
    • $ nvidia-smi

Install cuDNN v8.6, for CUDA 11.8:

Reboot:

  • $ reboot

Machine Learning Environment

$ python3 -m venv ~/venvs/ml
$ source ~/venvs/ml/bin/activate
(ml) $ pip install --upgrade pip
(ml) $ pip install --upgrade numpy scipy matplotlib ipython jupyter pandas sympy scikit-learn
(ml) $ deactivate

See Also:


Computer Vision Environment

$ python3 -m venv ~/venvs/cv
$ source ~/venvs/cv/bin/activate
(cv) $ pip install --upgrade pip
(cv) $ pip install --upgrade opencv-python opencv-contrib-python dlib pillow scikit-image imgaug
(cv) $ deactivate

See Also:


Deep Learning Environment (TensorFlow-CPU)

$ python3 -m venv ~/venvs/tfcpu
$ source ~/venvs/tfcpu/bin/activate
(tfcpu) $ pip install --upgrade pip
(tfcpu) $ pip install --upgrade tensorflow-cpu tensorboard keras
(tfcpu) $ deactivate

See Also:


Deep Learning Environment (TensorFlow-GPU)

$ python3 -m venv ~/venvs/tfgpu
$ source ~/venvs/tfgpu/bin/activate
(tfgpu) $ pip install --upgrade pip
(tfgpu) $ pip install --upgrade tensorflow tensorboard keras
(tfgpu) $ deactivate

Verification:

$ source ~/venvs/tfgpu/bin/activate
(tfgpu) $ python
>>> from tensorflow.python.client import device_lib
>>> device_lib.list_local_devices()
>>> exit()
(tfgpu) $ deactivate

See Also:


Deep Learning Environment (PyTorch-CPU)

$ python3 -m venv ~/venvs/torchcpu
$ source ~/venvs/torchcpu/bin/activate
(torchcpu) $ pip install --upgrade pip
(torchcpu) $ pip install --upgrade torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cpu
(torchcpu) $ deactivate

See Also:


Deep Learning Environment (PyTorch-GPU)

$ python3 -m venv ~/venvs/torchgpu
$ source ~/venvs/torchgpu/bin/activate
(torchgpu) $ pip install --upgrade pip
(torchgpu) $ pip install --upgrade torch torchvision torchaudio
(torchgpu) $ deactivate

Verification:

$ source ~/venvs/torchgpu/bin/activate
(torchgpu) $ python
>>> import torch
>>> torch.cuda.is_available()
>>> exit()
(torchgpu) $ deactivate

See Also:


Deep Learning Environment (FastAI)

$ python3 -m venv ~/venvs/fastai
$ source ~/venvs/fastai/bin/activate
(fastai) $ pip install --upgrade pip
(fastai) $ pip install --upgrade fastai fastbook
(fastai) $ deactivate

See Also:


Deep Learning Environment (Ultralytics YOLO)

$ python3 -m venv ~/venvs/ultralytics
$ source ~/venvs/ultralytics/bin/activate
(ultralytics) $ pip install --upgrade pip
(ultralytics) $ pip install --upgrade ultralytics
(ultralytics) $ deactivate

See Also:


ONNX Runtime Environment (CPU)

$ python3 -m venv ~/venvs/onnxcpu
$ source ~/venvs/onnxcpu/bin/activate
(onnxcpu) $ pip install --upgrade pip
(onnxcpu) $ pip install --upgrade onnx onnxruntime
(onnxcpu) $ deactivate

See Also:


ONNX Runtime Environment (GPU)

$ python3 -m venv ~/venvs/onnxgpu
$ source ~/venvs/onnxgpu/bin/activate
(onnxgpu) $ pip install --upgrade pip
(onnxgpu) $ pip install --upgrade onnx onnxruntime-gpu
(onnxgpu) $ deactivate

Verification:

$ source ~/venvs/onnxgpu/bin/activate
(onnxgpu) $ python
>>> import onnxruntime as ort
>>> ort.get_device()
>>> exit()
(onnxgpu) $ deactivate

See Also:


QT Environment (PySide)

$ python3 -m venv ~/venvs/qt
$ source ~/venvs/qt/bin/activate
(qt) $ pip install --upgrade pip
(qt) $ pip install --upgrade PySide6
(qt) $ deactivate

See Also:


Create Update Environments Script

  • Create a file (~/venvs-update.sh) with the following lines:
#!/usr/bin/env bash

if [ "$EUID" -eq 0 ]
  then echo "Error: Please do not run as root."
  exit
fi

clear

echo "################################################################################"
echo "Updating Machine Learning Environment..."
echo "--------------------------------------------------------------------------------"
python3 -m venv ~/venvs/ml
source ~/venvs/ml/bin/activate
pip install --upgrade pip
pip install --upgrade numpy scipy matplotlib ipython jupyter pandas sympy nose scikit-learn
deactivate
echo "################################################################################"
echo

echo "################################################################################"
echo "Updating Computer Vision Environment..."
echo "--------------------------------------------------------------------------------"
python3 -m venv ~/venvs/cv
source ~/venvs/cv/bin/activate
pip install --upgrade pip
pip install --upgrade opencv-python opencv-contrib-python dlib pillow scikit-image imgaug
deactivate
echo "################################################################################"
echo

echo "################################################################################"
echo "Updating Deep Learning Environment (TensorFlow-CPU)..."
echo "--------------------------------------------------------------------------------"
python3 -m venv ~/venvs/tfcpu
source ~/venvs/tfcpu/bin/activate
pip install --upgrade pip
pip install --upgrade tensorflow-cpu tensorboard keras
deactivate
echo "################################################################################"
echo

echo "################################################################################"
echo "Updating Deep Learning Environment (TensorFlow-GPU)..."
echo "--------------------------------------------------------------------------------"
python3 -m venv ~/venvs/tfgpu
source ~/venvs/tfgpu/bin/activate
pip install --upgrade pip
pip install --upgrade tensorflow tensorboard keras
deactivate
echo "################################################################################"
echo

echo "################################################################################"
echo "Updating Deep Learning Environment (PyTorch-CPU)..."
echo "--------------------------------------------------------------------------------"
python3 -m venv ~/venvs/torchcpu
source ~/venvs/torchcpu/bin/activate
pip install --upgrade pip
pip install --upgrade torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cpu
deactivate
echo "################################################################################"
echo

echo "################################################################################"
echo "Updating Deep Learning Environment (PyTorch-GPU)..."
echo "--------------------------------------------------------------------------------"
python3 -m venv ~/venvs/torchgpu
source ~/venvs/torchgpu/bin/activate
pip install --upgrade pip
pip install --upgrade torch torchvision torchaudio
deactivate
echo "################################################################################"
echo

echo "################################################################################"
echo "Updating Deep Learning Environment (FastAI)..."
echo "--------------------------------------------------------------------------------"
python3 -m venv ~/venvs/fastai
source ~/venvs/fastai/bin/activate
pip install --upgrade pip
pip install --upgrade fastai fastbook
deactivate
echo "################################################################################"
echo

echo "################################################################################"
echo "Updating Deep Learning Environment (Ultralytics YOLO)..."
echo "--------------------------------------------------------------------------------"
python3 -m venv ~/venvs/ultralytics
source ~/venvs/ultralytics/bin/activate
pip install --upgrade pip
pip install --upgrade ultralytics
deactivate
echo "################################################################################"
echo

echo "################################################################################"
echo "Updating ONNX Runtime Environment (CPU)..."
echo "--------------------------------------------------------------------------------"
python3 -m venv ~/venvs/onnxcpu
source ~/venvs/onnxcpu/bin/activate
pip install --upgrade pip
pip install --upgrade onnx onnxruntime
deactivate
echo "################################################################################"
echo

echo "################################################################################"
echo "Updating ONNX Runtime Environment (GPU)..."
echo "--------------------------------------------------------------------------------"
python3 -m venv ~/venvs/onnxgpu
source ~/venvs/onnxgpu/bin/activate
pip install --upgrade pip
pip install --upgrade onnx onnxruntime-gpu
deactivate
echo "################################################################################"
echo

echo "################################################################################"
echo "Updating QT Environment (PySide)..."
echo "--------------------------------------------------------------------------------"
python3 -m venv ~/venvs/qt
source ~/venvs/qt/bin/activate
pip install --upgrade pip
pip install --upgrade PySide6
deactivate
echo "################################################################################"
echo

Install Miniconda

Install:

$ wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
$ chmod +x Miniconda3-latest-Linux-x86_64.sh

$ ./Miniconda3-latest-Linux-x86_64.sh

$ # Do you wish the installer to initialize Miniconda3
  # by running conda init?
  # yes

$ source ~/miniconda3/bin/activate 
$ conda config --set auto_activate_base false
$ conda deactivate

Activate and Deactivate:

$ conda activate
(base) $ conda deactivate

Managing conda:

(base) $ conda info
(base) $ conda update conda

Managing environments:

(base) $ conda info --envs

(base) $ conda create --name snakes python=3.5
(base) $ conda info --envs

(base) $ conda activate snakes

(snakes) $ python --version

(snakes) $ conda search beautifulsoup4

(snakes) $ conda install beautifulsoup4
(snakes) $ conda list

(snakes) $ conda update beautifulsoup4

(snakes) $ conda uninstall beautifulsoup4
(snakes) $ conda list

(snakes) $ conda deactivate

(base) $ conda remove --name snakes --all

(base) $ conda info --envs

See Also:


Install Visual Studio Code

Install Python extension for Visual Studio Code:

Recommended Extensions:

Other Recommended Extensions:


Install PyCharm

Enable GPU support for PyCharm Projects:

  • Edit Configurations...
  • Environment variables:
    • PATH=$PATH:/usr/local/cuda-11.8/bin
    • LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-11.8/lib64:/usr/local/cuda-11.8/extras/CUPTI/lib64

See Also:


Install Docker Engine & Docker Compose

Nvidia Container Toolkit:

Make sure you have installed the NVIDIA driver and Docker engine for your Linux distribution Note that you do not need to install the CUDA Toolkit on the host system, but the NVIDIA driver needs to be installed.

$ distribution=$(. /etc/os-release;echo $ID$VERSION_ID) \
      && curl -fsSL https://nvidia.github.io/libnvidia-container/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-container-toolkit-keyring.gpg \
      && curl -s -L https://nvidia.github.io/libnvidia-container/$distribution/libnvidia-container.list | \
            sed 's#deb https://#deb [signed-by=/usr/share/keyrings/nvidia-container-toolkit-keyring.gpg] https://#g' | \
            sudo tee /etc/apt/sources.list.d/nvidia-container-toolkit.list

$ sudo apt update

$ sudo apt-get install --yes nvidia-container-toolkit

$ sudo nvidia-ctk runtime configure --runtime=docker

$ sudo systemctl restart docker

# NOTE: --runtime=nvidia
$ docker container run --rm --runtime=nvidia nvidia/cuda:11.8.0-base-ubuntu22.04 nvidia-smi
# OR
$ docker container run --rm --gpus all nvidia/cuda:11.8.0-base-ubuntu22.04 nvidia-smi

TensorFlow Docker:

# CPU
$ docker run -it tensorflow/tensorflow:latest bash

# GPU
$ docker run --gpus all -it tensorflow/tensorflow:latest-gpu bash

PyTorch Docker:

$ docker run --gpus all --rm -ti --ipc=host pytorch/pytorch:latest

Running ARM Docker Containers:

$ sudo apt install qemu-system qemu-user-static binfmt-support
$ # Host Architecture: x86_64
$ uname -m
$ # ARM Container Architecture: armv7l
$ docker run --rm arm32v7/debian uname -m

Install Additional Tools

$ sudo apt install ubuntu-restricted-extras
$ sudo apt install nano openssl curl wget uget tar zip unzip rar unrar p7zip-full p7zip-rar
$ sudo apt install ffmpeg vlc imagemagick gimp
$ sudo apt install libreoffice
$ sudo apt install virtualbox virtualbox-dkms virtualbox-ext-pack virtualbox-guest-additions-iso
$ sudo apt install kdiff3
  • Ubuntu Software > System Load Indicator

Install Additional Fonts

  • $ mkdir ~/.fonts
  • Copy fonts to ~/.fonts
@KeithHanson
Copy link

Not all heroes wear capes :D 🦸 Thanks for this, @amir-saniyan! I've done this at least 3-4 times now and pulling together all the google results was annoying - you've saved me lots of time!

@gabrielnespoli
Copy link

Check that GPUs are visible using the following command:

    $ nvidia-smi

before running nvidia-smi successfully I had to reboot the machine.
Otherwise, it was giving me the error :

NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed and running.

@amir-saniyan
Copy link
Author

before running nvidia-smi successfully I had to reboot the machine.

Thanks, gist updated.

@jeffkayser2
Copy link

Thank you! Your doc saved me weeks of floundering.

I also added a few Python environments, to make it easier to work through the fastai book.

$ python3 -m venv ~/venvs/fastaigpu
$ source ~/venvs/fastaigpu/bin/activate
(fastaigpu) $ pip install --upgrade pip setuptools wheel
(fastaigpu) $ pip install --upgrade opencv-python opencv-contrib-python
(fastaigpu) $ pip install --upgrade torch torchvision torchaudio
(fastaigpu) $ pip install --upgrade numpy scipy matplotlib ipython jupyter pandas sympy nose
(fastaigpu) $ pip install --upgrade scikit-learn scikit-image
(fastaigpu) $ pip install --upgrade fastai fastbook
(fastaigpu) $ deactivate

I made a fastaicpu environment also, so I could compare CPU vs GPU performance.

Using the fastaigpu environment, I can clone the book, and run it via Jupyter notebooks, and it works great, and leverages the GPU.

I can't thank you enough for this article. You literally saved me weeks of floundering, and the result is 10 times better that what I would have come up with on my own.

Thank you so much!

@amir-saniyan
Copy link
Author

I also added a few Python environments, to make it easier to work through the fastai book.

Thanks, gist updated.

@david-sanchezperez
Copy link

On 22.04, does it work with any kernel version?

$ uname -r
6.2.0-36-generic

Thanks!

@amir-saniyan
Copy link
Author

On 22.04, does it work with any kernel version?

$ uname -r 6.2.0-36-generic

Thanks!

I tested it with Ubuntu 22.04 and everything was working.

@miningnome
Copy link

Yeah I can't get CUDA to set up correctly. Running nvidia-smi after foregoing the installation of the driver just yields NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed and running., as is to be expected I guess

Did you rebooted after install? It's not mentioned but required.

Great guide, thank you @amir-saniyan

@miningnome
Copy link

On 22.04, does it work with any kernel version?
$ uname -r 6.2.0-36-generic
Thanks!

I tested it with Ubuntu 22.04 and everything was working.

Everything you wrote on your guide works like a charm on Ubuntu 22.04.3 LTS, I can confirm!
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment