Skip to content

Instantly share code, notes, and snippets.

@w2sv
Last active September 16, 2022 13:58
Show Gist options
  • Save w2sv/d96cf1afdc021ff7443b9e0067c69b33 to your computer and use it in GitHub Desktop.
Save w2sv/d96cf1afdc021ff7443b9e0067c69b33 to your computer and use it in GitHub Desktop.
Setting up tensorflow-gpu on Ubuntu 22.04
# $$$$$$$$$$ Pre-Installation $$$$$$$$$$$$$$$
# Ensure presence of cuda-compatible GPU
lspci | grep -i nvidia # should output something like '01:00.0 3D controller: <YOUR_NVIDIA_GPU_KIND> (rev a1)'
# Ensure gcc being installed
gcc --version
# Optional: install the nvidia cuda toolkit
sudo apt-get update
sudo apt-get install nvidia-cuda-toolkit
# $$$$$$$$$$ Installation $$$$$$$$$$
# Head over to https://www.tensorflow.org/install/source#gpu to determine the cuda and cuDNN versions
# which are compatible with your desired tensorflow & python version, and set them as session variables
# for usage by subsequent commands.
cudnn_version={{e.g. 8.1.1.*}} # NOTE: the specified version must comprise the 4 specification levels (or 3 + '.*', respectively)
cuda_version={{e.g. 11.2}}
# 1. Install CUDA
sudo apt-get update
sudo apt-get install cuda-${cuda_version}
# if you've installed the nvidia cuda toolkit, running 'nvcc --version' should now reflect your installed CUDA version
# 2. Install cuDNN, following https://docs.nvidia.com/deeplearning/cudnn/install-guide/index.html#package-manager-ubuntu-install
# Enable repository
OS=ubuntu2204
wget https://developer.download.nvidia.com/compute/cuda/repos/${OS}/x86_64/cuda-${OS}.pin
sudo mv cuda-${OS}.pin /etc/apt/preferences.d/cuda-repository-pin-600
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/${OS}/x86_64/3bf863cc.pub
sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/${OS}/x86_64/ /"
sudo apt-get update
# Install cuDNN library
sudo apt-get install libcudnn8=${cudnn_version}-1+cuda${cuda_version}
# 3. Install tensorflow-gpu in a virtual environment hosted by e.g. conda.
conda create -n tf-gpu python={{PY_VERSION}} -y
conda activate tf-gpu
conda install tensorflow-gpu={{TF_VERSION}} # if desired version not hosted by conda, just install it via pip instead by running:
# pip install tensorflow-gpu=={{TF_VERSION}}
# $$$$$$$$$$ Post-Installation $$$$$$$$$$$$$$$
# verify installation
python -c 'import tensorflow as tf; tf.config.experimental.list_physical_devices("GPU")'
# monitor your GPU via the nvidia system management interface. Note: the CUDA version displayed in the top right corner of the
# generated output may differ from the one you've just installed manually.
nvidia-smi
@w2sv
Copy link
Author

w2sv commented Sep 16, 2022

Those are simply the steps I found to have worked for me. The file ain't actually supposed to be run as a script, but to provide all the necessary command blocks required for the set-up. Particularly the desired cuda, cudnn, python and tf-gpu versions, all marked by double-curly braces, must be manually provided beforehand.

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