{
  "slug": "Use-GPU-in-Tensorflow-on-Ubuntu-22-04-f033e59cf5cb",
  "title": "Use GPU in Tensorflow on Ubuntu 22.04",
  "subtitle": "This document provides a guide on installing TensorFlow with GPU support on Ubuntu 22.04. It details the necessary steps to install the…",
  "excerpt": "This document provides a guide on installing TensorFlow with GPU support on Ubuntu 22.04. It details the necessary steps to install the…",
  "date": "2024-03-30",
  "tags": [
    "Design Rag System",
    "Linux",
    "Machine Learning"
  ],
  "readingTime": "3 min",
  "url": "https://medium.com/@mobinshaterian/use-gpu-in-tensorflow-on-ubuntu-22-04-f033e59cf5cb",
  "hero": "https://cdn-images-1.medium.com/max/800/1*u_JWAeUBZ0p5HFEg4xXubQ.jpeg",
  "content": [
    {
      "type": "heading",
      "level": 2,
      "text": "Use GPU in Tensorflow on Ubuntu 22.04"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*u_JWAeUBZ0p5HFEg4xXubQ.jpeg",
      "alt": "Use GPU in Tensorflow on Ubuntu 22.04",
      "caption": "",
      "width": 640,
      "height": 832
    },
    {
      "type": "paragraph",
      "html": "This document provides a guide on installing TensorFlow with GPU support on Ubuntu 22.04. It details the necessary steps to install the required software components, including Nvidia drivers, CUDA Toolkit, and cuDNN. Additionally, it troubleshoots a common error message encountered during the installation process and offers solutions."
    },
    {
      "type": "paragraph",
      "html": "In this tutorial, I am going to use GPU in the tensorflow framework."
    },
    {
      "type": "paragraph",
      "html": "<a href=\"https://www.cyberciti.biz/faq/ubuntu-linux-install-nvidia-driver-latest-proprietary-driver/\" target=\"_blank\" rel=\"noreferrer noopener\">https://www.cyberciti.biz/faq/ubuntu-linux-install-nvidia-driver-latest-proprietary-driver</a>/"
    },
    {
      "type": "paragraph",
      "html": "The procedure to install proprietary Nvidia GPU Drivers on Ubuntu 16.04 / 17.10 / 18.04 / 18.10 / 20.04 / 22.04 LTS is as follows:"
    },
    {
      "type": "list",
      "ordered": true,
      "items": [
        "Update your system running the apt-get command",
        "You can install Nvidia drivers either using the GUI or CLI method",
        "Open the “<strong>Software and Updates</strong>” app to install the Nvidia driver using GUI",
        "OR type “sudo apt install nvidia-driver-535 nvidia-dkms-535” at the CLI",
        "Reboot the computer/laptop to load the drivers",
        "Verify drivers are working"
      ]
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Finding out information about your GPU"
    },
    {
      "type": "paragraph",
      "html": "Naturally, you can only install Nvidia driver if you have Nvidia GPU in your system. Type the hwinfo command/lshw command to find out info about your GPU<br>&nbsp;<code>hwinfo --gfxcard --short</code><br>&nbsp;OR<br>&nbsp;<code>sudo lshw -C display</code>"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Ubuntu Install Nvidia driver using the CLI method"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "apt-cache search nvidia-driver"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "My computer Nvidia model"
    },
    {
      "type": "paragraph",
      "html": "sudo apt install nvidia-driver-550 nvidia-dkms-550"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Nvidia-smi"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*zkM13d9oBrHWa5D1nTG0tA.png",
      "alt": "Use GPU in Tensorflow on Ubuntu 22.04",
      "caption": "",
      "width": 837,
      "height": 342
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Find bug report"
    },
    {
      "type": "paragraph",
      "html": "<code>nvidia-bug-report.sh</code>"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "/var/log/kern.log:"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "nvidia-settings"
    },
    {
      "type": "paragraph",
      "html": "sudo apt install nvidia-settings"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*kYkCbv67klHEaPCxknEDwg.png",
      "alt": "Use GPU in Tensorflow on Ubuntu 22.04",
      "caption": "",
      "width": 905,
      "height": 485
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Reinstall Nvidia"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "sudo\napt reinstall linux-headers-$(uname -r)\nsudo\napt install nvidia-driver-525"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "lspci | grep -i nvidia"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "CUDA Toolkit 12.4 Downloads"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*Q40AIxk1OYgbEwqfJG1www.png",
      "alt": "Use GPU in Tensorflow on Ubuntu 22.04",
      "caption": "",
      "width": 1284,
      "height": 479
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*4qU-1ZK9OMtMgCIeAj22aw.png",
      "alt": "Use GPU in Tensorflow on Ubuntu 22.04",
      "caption": "",
      "width": 788,
      "height": 578
    },
    {
      "type": "paragraph",
      "html": "Ubuntu users"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Nvidia cuda toolkit"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "sudo\napt-get install nvidia-cuda-toolkit"
    },
    {
      "type": "paragraph",
      "html": "unistall"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "sudo\napt-get remove nvidia-cuda-toolkit"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "How to remove cuda completely from ubuntu?"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "sudo\napt-get purge nvidia*\nsudo\napt-get autoremove\nsudo\napt-get autoclean\nsudo rm -rf /usr/local/cuda*"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Easy install CUDA"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "sudo\napt-get install cuda"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "cuDNN 9.0.0 Downloads"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*Rh9GRoFJqLQQtbf0N7c4PA.png",
      "alt": "Use GPU in Tensorflow on Ubuntu 22.04",
      "caption": "",
      "width": 1254,
      "height": 545
    },
    {
      "type": "heading",
      "level": 2,
      "text": "How to install TensorFlow2 with GPU support on Ubuntu 22.04"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "whereis cudacuda: /usr/lib/cuda /usr/include/cuda"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "python3 -m pip install tensorflow[and-cuda]# Verify the installation:python3 -c\n\"import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))\"2024-03-30 08:45:30.386306:\nW tensorflow/core/common_runtime/gpu/gpu_device.cc:2251] Cannot dlopen some GPU libraries. Please\nmake sure the missing libraries mentioned above are installed properly if you would like to use GPU.\nFollow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the\nrequired libraries for your platform.Skipping registering GPU devices...[]"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "It's just tensorflow can't see the Cuda libraries."
    },
    {
      "type": "code",
      "lang": "text",
      "code": "Instal tensorflow[and-cuda] and add this to your .bashrc or conda activation script. Adjust python\nversion in it according to your setup."
    },
    {
      "type": "code",
      "lang": "text",
      "code": "gedit ~/.bashrc"
    },
    {
      "type": "code",
      "lang": "properties",
      "code": "NVIDIA_PACKAGE_DIR=\"$CONDA_PREFIX/lib/python3.12/site-packages/nvidia\""
    },
    {
      "type": "code",
      "lang": "properties",
      "code": "for dir in $NVIDIA_PACKAGE_DIR/*; do    if [ -d \"$dir/lib\" ]; then        export\nLD_LIBRARY_PATH=\"$dir/lib:$LD_LIBRARY_PATH\"    fidone"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "source ~/.bashrc"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "For example on Ubuntu:"
    },
    {
      "type": "code",
      "lang": "dotenv",
      "code": "N\nV\nI\nD\nI\nA_\nP\nA\nC\nK\nA\nG\nE_\nD\nI\nR=\"$HOME/.local/lib/python3.10/site-packages/nvidia/\"for dir in $NVIDIA_PACKAGE_DIR/*; do    if [ -d\n\"$dir/lib\" ]; then        export\nL\nD_\nL\nI\nB\nR\nA\nR\nY_\nP\nA\nT\nH=\"$dir/lib:$LD_LIBRARY_PATH\"    fidoneexport\nN\nV\nM_\nD\nI\nR=\"$HOME/.nvm\"[ -s \"$NVM_DIR/nvm.sh\" ] && \\. \"$NVM_DIR/nvm.sh\"  # This loads nvm[ -s\n\"$NVM_DIR/bash_completion\" ] && \\. \"$NVM_DIR/bash_completion\"  # This loads nvm bash_completion"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU."
    },
    {
      "type": "paragraph",
      "html": "In this case, you need to set an environment in bashrc."
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "nvcc --versionnvcc: NVIDIA (R) Cuda compiler driverCopyright (c) 2005-2021 NVIDIA CorporationBuilt\non Thu_Nov_18_09:45:30_PST_2021Cuda compilation tools, release 11.5, V11.5.119Build\ncuda_11.5.r11.5/compiler.30672275_0"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Error : ValueError: JAX requires ml_dtypes version 0.4.0 or newer; installed version is 0.3.2."
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "pip install ml_dtypes==0.4.0"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Stackademic 🎓"
    },
    {
      "type": "paragraph",
      "html": "Thank you for reading until the end. Before you go:"
    },
    {
      "type": "list",
      "ordered": false,
      "items": [
        "Please consider <strong>clapping</strong> and <strong>following</strong> the writer! 👏",
        "Follow us <a href=\"https://twitter.com/stackademichq\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>X</strong></a><strong> | </strong><a href=\"https://www.linkedin.com/company/stackademic\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>LinkedIn</strong></a><strong> | </strong><a href=\"https://www.youtube.com/c/stackademic\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>YouTube</strong></a><strong> | </strong><a href=\"https://discord.gg/in-plain-english-709094664682340443\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Discord</strong></a>",
        "Visit our other platforms: <a href=\"https://plainenglish.io\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>In Plain English</strong></a><strong> | </strong><a href=\"https://cofeed.app/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>CoFeed</strong></a><strong> | </strong><a href=\"https://venturemagazine.net/\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Venture</strong></a><strong> | </strong><a href=\"https://blog.cubed.run\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Cubed</strong></a>",
        "More content at <a href=\"https://stackademic.com\" target=\"_blank\" rel=\"noreferrer noopener\"><strong>Stackademic.com</strong></a>"
      ]
    }
  ]
}
