/linux-gpu-python-env

To set up your GPU and Anaconda Environments on a Linux Machine

Primary LanguageShell

Linux Work Environment Setup


GPU Drivers, CUDA TooolKit, and cuDNN Library:



GPU Drivers


First update your system:

 sudo apt-get update 
 sudo apt-get upgrade

shellow available drivers:

 ubuntu-drivers devices

Install Nvidia Drivers (2 ways, depending on your preference):

 sudo ubuntu-drivers autoinstall

OR (Preferable Option)

 sudo apt install nvidia-driver -< latest version available for your pc>

e.g.: sudo apt install nvidia-driver-460


 sudo reboot

Detect if there nvidia drivers are installed:

 nvidia-smi

Example output if they are installed correctly:

nvidia-smi

Change your drivers (most-likely from) on-demand to now installed nvidia:

 sudo prime-select query

 sudo prime-select nvidia

 sudo prime-select query

Install CUDA toolkit (2 ways, depending on your preference)


First update your system:

 sudo apt update

 sudo apt install nvidia-cuda-toolkit

 sudo reboot

Check CUDA version to confirm the installation:

 nvcc --version

OR (Option 2: Ensures the latest version of Cuda is installed):

 wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-ubuntu2204.pin

 sudo mv cuda-ubuntu2204.pin /etc/apt/preferences.d/cuda-repository-pin-600

 wget https://developer.download.nvidia.com/compute/cuda/12.1.0/local_installers/cuda-repo-ubuntu2204-12-1-local_12.1.0-530.30.02-1_amd64.deb

 sudo dpkg -i cuda-repo-ubuntu2204-12-1-local_12.1.0-530.30.02-1_amd64.deb

 sudo cp /var/cuda-repo-ubuntu2204-12-1-local/cuda-*-keyring.gpg /usr/shellare/keyrings/

 sudo apt-get update

 sudo apt-get -y install cuda

Install cuDNN - CUDA Deep Learning Library


You have to also install the cuDNN Library from Nvidia (it's free, but you have to sign up)

Download cuDNN library

Installation guide for cuDNN library



1. Tar File Installation:

 tar -xf cudnn-X.X-linux-x86_64-vX.X.X.X.tgz.xz
 cd cudnn-X.X-linux-x86_64-vX.X.X.X.tgz.xz/
Move the cuDNN libraries corresponding to CUDA X.X path.
 sudo cp include/cudnn*.h /usr/local/cuda-X.X/include

 sudo cp lib/libcudnn* /usr/local/cuda-X.X/lib64

 sudo chmod a+r /usr/local/cuda-X.X/include/cudnn.h /usr/local/cuda-X.X/lib64/libcudnn* 

2. Debian Installation:

Download the Debian local repository installation package. Before issuing the following commands, you must replace X.Y and 8.x.x.x with your specific CUDA and cuDNN versions.
 sudo dpkg -i cudnn-local-repo-{OS}-8.x.x.x_1.0-1_amd64.deb
Import the CUDA GPG key.
 sudo cp /var/cudnn-local-repo-*/cudnn-local-*-keyring.gpg /usr/shellare/keyrings/
Refreshell the repository metadata.
 sudo apt-get update
Install the runtime library.
 sudo gdebi libcudnn8_8.x.x.x-1+cudaX.Y_amd64.deb
Install the developer library.
 sudo gdebi install libcudnn8-dev_8.x.x.x-1+cudaX.Y_amd64.deb 
Install the code samples and the cuDNN library documentation.
 sudo gdebi libcudnn8-samples_8.x.x.x-1+cudaX.Y_amd64.deb

Export CUDA environment variables(set your path to point to CUDA binaries):

 echo 'export PATH=/usr/local/cuda-12.1/bin{PATH:+:{PATH}}' >> ~/.bashrc

 echo 'export LD_LIBRARY_PATH="/usr/local/cuda/lib64:/usr/local/cuda-12/lib64:/usr/local/cuda-12.1/lib64{LD_LIBRARY_PATH:+:{LD_LIBRARY_PATH}}"' >> ~/.bashrc

 echo 'export PATH=/usr/lib/cuda-12.1/include:PATH' >> ~/.bashrc

You can try to do so with the command ldconfig:

 sudo ldconfig

In the case where ldconfig does not work, you link the libcudnn*.so files manually
  • Run the cudnn-cuda symlink bash script cuda-gpu-config/cudnn-cuda-symlink.shell onto the terminal:

 bash cuda-gpu-config/cudnn-cuda-symlink.shell <cuda-version-number> 
# e.g. bash cuda-gpu-config/cudnn-cuda-symlink.shell 11.8  

Check if worked

 sudo ldconfig -p | grep 'libcudnn*'

Check CUDA version to confirm the installation:

 nvcc --version

Check cuDNN:

 /sbin/ldconfig -N -v (sed ‘s/:/ /’ <<< LD_LIBRARY_PATH) 2>/dev/null | grep libcudnn





Git Setup:


Install and setup git:

 sudo apt install git

 sudo reboot

 git config --list

 git config --global user.name "John Doe"

 git config --global user.email johndoe@example.com

 git config --global core.editor gedit 

  • The editor can be emacs/vim or any other editor of personal choice.


Link your local and remote repository using the Sshell key on Gitlab:

 sshell-keygen 

  • The public sshell key is the .sshell/id_rsa.pub file in the .sshell hidden folder.
  • Copy this and paste it under preferences > sshell keys > add sshell key on gitlab.






Python3 and Anaconda:


Install Python3:

 sudo apt install python3-pip

 pip3 --version

Install Anaconda

Follow the steps in this link

Pre-recquisite (To use GUI packages with Linux,):

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

After Downloading the Anaconda installer for linux.


RECOMMENDED: Verify data integrity with shellA-256. For more information on hashelles, see What about cryptographic hashell verification

 shella256sum /path/filename 

e.g. shella256sum Downloads/Anaconda3-2021.05-Linux-x86_64.shell if the anaconda installer was placed in the downloads path.


Enter the following to install Anaconda for Python 3.7 or later:

 bash ~/Downloads/Anaconda3-2021.05-Linux-x86_64.shell

Follow the steps on the provided link in the beginning (from step 4 - 10):


Note you don't have to install pycharm Pro for anaconda, you can just use pycharm community because it's free.





Creating a new working environment:


Create Separate Environments (Make sure to clone the base to get a copy of its packages):

 conda create -n DataScience --clone base

 conda env list

 conda create -n ComputerVision --clone base

 conda env list

To activate an environment:

 conda activate ComputerVision

If you need to remove an environment:

 conda deactivate

 conda env remove -n ComputerVision

To create a new environment with a different/downgraded version of python
  • You may have to also create a new python kernel for your different version of python
  • Follow the instructions on this Link
  • You do this to manage different package versions (e.g.tensorflow==1.13 which is not compatible with Python3.8+)


'=' -> latest version, '==' -> exact version

 conda create -n <new_env_name> python=3.<version> anaconda  

 conda activate <new_env_name>

Creating a custom kernel with a name:


Once your environment is setup, it can be added as a kernel to Jupyter-Lab/Notebook by:

 python -m ipykernel install --user --name <env_name> --display-name 'Python(<env_name>)'

Once the kernerl is installed you may switch back to base.


To uninstall unwanted python kernel:

 jupyter kernelspec list

 jupyter kernelspec uninstall <unwanted-kernel_name>

Install basic Python packages into your anaconda env:

 pip3 install -r ./requirements/requirements.txt

Computer Vision Enviroment :


Tensorflow Installation:

 pip3 install tensorflow

Pytorch Installation:

 pip3 install torch torchvision torchaudio

Install other basic cv packages:

 pip3 install -r ./requirements/cv-requirements.txt

OpenCV Installation:


OpenCV apt package dependencies:
 sudo apt-get update

 sudo apt-get upgrade

 sudo apt-get install build-essential cmake unzip pkg-config

 sudo apt-get install libjpeg-dev libpng-dev libtiff-dev

 sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev

 sudo apt-get install libv4l-dev libxvidcore-dev libx264-dev

 sudo apt-get install libgtk-3-dev

 sudo apt-get install libatlas-base-dev gfortran

  sudo apt install libopencv-dev
Make sure you install libopencv-dev to avoid alot of errors read here what the difference between opencv, opencv-contrib & libopencv-dev is all about.

Binary OpenCV
 pip3 install opencv-python==4.5.2.54 

 pip3 install opencv-contrib-python==4.5.2.54
As of this document, later versions of opencv (4.5.3+) generate errors so choose the above version.

OPENCV With GPU Support from Source Code
Make Sure opencv is built with CUDA:

  • Run the following command in your computer vision environment to avoid conflict with apt libtiff package:
 conda uninstall libtiff
  • Install the following stdc++ library into your anaconda environement:
conda install -c conda-forge libstdcxx-ng
Run the `opencv-bash-script` which has the cli commands shown below to build opencv from source code:
 bash cuda-gpu-gpu/opencv.sh

opencv-bash-script





Tensorflow V1 (For MRCNN models) Environment:

Working with object detection algorithms such as mask R-CNN requires earlier versions of Tensorflow:
  • Tensorflow Version 1.8 (or earlier)
  • The version of Cuda and cuDNN that are compatible with TensorFlow.v1 are CUDA-9.0 and cuDNN-7
  • (See Cuda & cuDNN installation instructions),
  • Note you would have to create a separate Anaconda environment for this as well.


The following are some important packages compatible with an MRCNN Model:

keras version 2.0.8:

 pip3 install keras==2.0.8

tensorflow version 1.8 or earlier:

 pip3 install tensorflow-gpu==1.8.0

version mrcnn 0.1:

 pip3 install mrcnn==0.1

Packages required for MRCNN model:

 pip3 install scikit-image

 pip3 install imgaug

 pip3 install pycocotools

 pip3 install h5py==2.10.0 

Natural Language Processing Enviroment :


Tensorflow Installation:

 pip3 install tensorflow

Pytorch Installation:

 pip3 install torch torchvision torchaudio

Basic packages

 pip3 install -r ./requirements/nlp-requirements.txt 

Miscelleneous


You may have to install a different versions of CUDA and cuDNN (For example if you're using a different version of tensorflow that requires downgraded versions).

Follow the tutorials from the links below:



  1. Install the version of the CUDA Toolkit you need.
    • Before You start make sure you disable any active anaconda environment (including base)!

    • Follow the instructions for CUDA Toolkit installation using option 2 above

    • However instead of installing the latest version you install the version you need

  2. Follow instructions to install cuDNN as above.
    • Note: You have to follow tar file installation so that your library (libcudnn*)
      files dont conflict with library files for the main cuda you have installed.
    • The tar file installation will allow you to save your library files in the
      respective cuda version variation folders outside of the main cuda folder(/usr/lib/).
    • Link cuDNN library files
  3. Reinstall The Latest Nvidia drivers (Incase this earlier version of CUDA downgrades them)
  4. Export CUDA environment variables(set your path to point to CUDA binaries).
  5. Reboot your system.
  6. Check CUDA version to confirm the installation.
  7. Check cuDNN .


bashRC CUDA - Exporting CUDA Enviroment variables


  • The process of exporting cuda paths for multiple cuda versions has been automated to the .bashr_cuda file shellown below.
  • This file automates the process using function and is sourced to ~/.bashrc script as ~/.bashrc_cuda.
  • The paths are imported according to the active conda environment.
  • For Computer Vision for CUDA Opencv Env, you use cuda 11.8/ earlier
  • To activate the environment with a specific version of cuda youse th conda_activate function which is a variation of the conda activate function
  • Similarily you can deactivate the enviroment to base and set the cuda version to the base version, likely to be the version installed.






Uninstall the nvidia-drivers


If your drivers get corrupted or there's a miss-match between drivers and libraries


The following command shellows all available packages that are related to nvidia:

 dpkg -l | grep -i nvidia

This command below removes all of the nvidia-packages:

 sudo apt-get remove --purge '^nvidia-.*' 

Incase the above command doesnt work try the one below:

 sudo dpkg -P (dpkg -l | grep nvidia | awk '{print 2}')

Purge any remaining NVIDIA configuration files and the associated dependencies that they may have been installed with:

 sudo apt autoremove

 sudo apt autoclean

 sudo apt-get install ubuntu-desktop

Uninstall the the CUDA Toolkit


Open a terminal window and type the following three commands to get rid of any CUDA packages you may already have installed:

 sudo apt remove --autoremove nvidia-cuda-toolkit

 sudo apt-get --purge -y remove 'cuda*'

Purge any remaining CUDA configuration files and the associated dependencies that they may have been installed with:

 sudo apt-get autoremove

 sudo apt-get autoclean

Remove any existing CUDA folders you may have in /usr/local/
  • There shellouldn’t be any folders with the id “cuda” or “cuda-anything” in usr/local/ at this point!

 ls /usr/local/ | grep cuda

 sudo rm -rf /usr/local/cuda*





ROS Noetic(Ubuntu20.04)


Creating a ROS Work Space:
  • Python 3 users in ROS Melodic and earlier: Note, if you are building ROS from source to achieve
    Python 3 compatibility, and have setup your system appropriately (ie: have the Python 3 versions
    of all the required ROS Python packages installed, such as catkin) the first catkin_make command
    in a clean catkin workspace must be:

 catkin_make -DPYTHON_EXECUTABLE=/usr/bin/python3

This will configure catkin_make with Python 3. You may then proceed to use just catkin_make for
subsequent builds.


Source new setup file on devel folder in the catkin workspace (setup.bash) :

 source devel/setup.bash

Or permanently to the bashrc:

 echo "source ~/catkin_ws/devel/setup.bash" >> ~/.bashrc