Installing Tensorflow for Nvidia GPU with CUDA and all on Ubuntu 22.10

After none of the instructions on how to setup Tensorflow on Ubuntu 22.x worked for me I created the setup below. It is based on some instructions that I had created a few years ago for Ubuntu 18. (https://github.com/OliverMaerz/deep-learning-etc/blob/master/setup-fastai-pytorch-tensorflow-ubuntu-nvidia-gpu.md). So far it works really well for me.

Remove Nvidia drivers and stuff

If you have already installed some Nvidia drivers, CUDA etc. via then you remove them with:

sudo apt --purge remove "*nvidia*"
sudo apt --purge remove "*cublas*" "cuda*" "nsight*"
sudo apt autoremove

If you have them installed via some other method (e.g. download binary/installed) then follow the uninstall instrutions for that method.

The fresh install

Now let's do a fresh install of the Nvidia drivers

sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt update && sudo apt upgrade 
sudo apt install ubuntu-drivers-common
sudo ubuntu-drivers autoinstall
sudo reboot

Don't forget the reboot! Otherwise you will get some errors about libcuda and kernel not reporting the same CUDA version.

Install Anaconda

First install some required libraries:

sudo apt install libgl1-mesa-glx libegl1-mesa libxrandr2 libxrandr2 libxss1 libxcursor1 libxcomposite1 libasound2 libxi6 libxtst6

Now download the latest version. Check page and get the download link: https://www.anaconda.com/products/distribution#linux

For examle at the time I wrote this post https://repo.anaconda.com/archive/Anaconda3-2022.10-Linux-x86_64.sh So I donwloaded and installed it with:

wget https://repo.anaconda.com/archive/Anaconda3-2022.10-Linux-x86_64.sh
bash ~/Downloads/Anaconda3-2022.10-Linux-x86_64.sh

Now either log out and back in, open a new shell, or if you use bash can also run source ~/.bashrc.

Create a new Anaconda environment for TensorFlow:

conda create -n tf-gpu python=3.9
# and activate
conda activate tf-gpu
# install tf and some other useful packages (as of today this installs Tensorflow 2.10.0)
conda install -c conda-forge tensorflow-gpu pandas matplotlib jupyterlab scipy scikit-learn-intelex opencv

Finally verify it is working with:

python

and then in python run import tensorflow as tfand tf.config.list_physical_devices('GPU'). At the end you should get a message like [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]. The whole should look like:

[GCC 11.2.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
2022-11-11 11:11:1.11111: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudart.so.10.1
>>> tf.config.list_physical_devices('GPU')
... a bunch of log messages ...
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]

Et voilà!

Share