Skip to content

Instantly share code, notes, and snippets.

@garg-aayush
Last active October 29, 2025 09:11
Show Gist options
  • Select an option

  • Save garg-aayush/156ec6ddda3d62e2c0ddad00b7e66956 to your computer and use it in GitHub Desktop.

Select an option

Save garg-aayush/156ec6ddda3d62e2c0ddad00b7e66956 to your computer and use it in GitHub Desktop.
Managing multiple CUDA versions using environment modules in Ubuntu

Steps to manage multiple CUDA environments

This gist contains all the steps required to:

  • Install multiple CUDA versions (e.g., CUDA 11.3 and CUDA 11.8).
  • Manage multiple CUDA environments on Ubuntu using the utility called environment modules.
  • Use this approach to avoid CUDA environment conflicts.

Environment Modules is a package that provides for the dynamic modification of a user's environment via modulefiles. You can find more on it at https://modules.readthedocs.io/en/latest/

1. Install the Compatible NVIDIA Drivers (if required)

  • Add PPA GPU Drivers Repository to the System

    sudo add-apt-repository ppa:graphics-drivers/ppa
  • Check GPU and available drives

    ubuntu-devices drivers
  • Install the compatible driver

    # in my case it is nvidia-driver-530
    sudo apt install nvidia-driver-530
  • Check the installed NVIDIA driver

    nvidia-detector 

Note:

  • You can also auto-install the compatible driver using sudo ubuntu-drivers autoinstall.
  • Additionally, you can also install NVIDIA drivers using the Software & Updates Ubuntu app. Just go to the Additional Drivers tab, choose a driver, and click Apply Changes.

2. Install CUDA 11.3 and CUDA 11.8

  • Go to the https://developer.nvidia.com/cuda-toolkit-archive and select CUDA Toolkit 11.3 from the available options.

  • Choose your OS, architecture, distribution, version, and installer type. For example, in my case:

    Option value
    OS Linux
    Architecture x86_64
    Distribution Linux
    Version 20.04
    Installer type deb(local)
  • Follow the provided installation instructions by copying and pasting the commands into your terminal. This will install CUDA 11.3. Use the following commands:

    wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin
    sudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600
    wget https://developer.download.nvidia.com/compute/cuda/11.3.0/local_installers/cuda-repo-ubuntu2004-11-3-local_11.3.0-465.19.01-1_amd64.deb
    sudo dpkg -i cuda-repo-ubuntu2004-11-3-local_11.3.0-465.19.01-1_amd64.deb
    sudo apt-key add /var/cuda-repo-ubuntu2004-11-3-local/7fa2af80.pub
    sudo apt-get update
    sudo apt-get -y install cuda
  • Similarly, install CUDA 11.8 using the following commands:

    wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin
    sudo mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600
    wget https://developer.download.nvidia.com/compute/cuda/11.8.0/local_installers/cuda-repo-ubuntu2004-11-8-local_11.8.0-520.61.05-1_amd64.deb
    sudo dpkg -i cuda-repo-ubuntu2004-11-8-local_11.8.0-520.61.05-1_amd64.deb
    sudo cp /var/cuda-repo-ubuntu2004-11-8-local/cuda-*-keyring.gpg /usr/share/keyrings/
    sudo apt-get update
    sudo apt-get -y install cuda
  • Make sure to copy and execute the commands above in your terminal to install CUDA 11.3 and CUDA 11.8 on your system.

3. Install cuDNN library

  • Go to https://developer.nvidia.com/cudnn and download the cuDNN library for CUDA 11.x. Note that you might need to create a developer's account first.

  • Untar the downloaded file using the following command:

    tar -xvf cudnn-linux-x86_64-8.9.2.26_cuda11-archive.tar.xz
  • Copy the cuDNN files to the CUDA toolkit files:

    # for CUDA 11.3
    sudo cp include/cudnn*.h /usr/local/cuda-11.3/include
    sudo cp lib64/libcudnn* /usr/local/cuda-11.3/lib64
    
    # for CUDA 11.8
    sudo cp include/cudnn*.h /usr/local/cuda-11.8/include
    sudo cp lib64/libcudnn* /usr/local/cuda-11.8/lib64
  • Make the files executable:

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

Note: Strictly speaking, you are done with the CUDA setup. You can use it by adding the CUDA bin and library path to the PATH and LD_LIBRARY_PATH environment variables. For example, you can set up CUDA 11.8 by adding the following lines in the ~/.bashrc:

PATH=/usr/local/cuda-11.3/bin:$PATH
LD_LIBRARY_PATH=/usr/local/cuda-11.8/extras/CUPTI/lib64:$LD_LIBRARY_PATH
LD_LIBRARY_PATH=/usr/local/cuda-11.8/lib64:$LD_LIBRARY_PATH

Similarly, you can set up CUDA 11.3. However, manually changing the paths every time can be cumbersome!

4. Manage multipe CUDA versions using environment modules

a) Install the environment modules utility:

  • Run the following commands:
        sudo apt-get update
        sudo apt-get install environment-modules
  • Check the installation:
    # Check the installation by running
    module list

You should see a list of default installed modules like git and maybe their versions displayed when you run the command module list. This confirms that the environment modules utility has been successfully installed on your system.

b) Create modulefiles for CUDA distributions

Note: You might need root permissions to create directories and files. Use sudo in that case.

  • Create a directory /usr/share/modules/modulefiles/cuda to hold modulefiles for cuda distributions

    sudo mkdir -p /usr/share/modules/modulefiles/cuda
  • Create a modulefile /usr/share/modules/modulefiles/cuda/11.3 for CUDA 11.3 and add the following lines:

    #%Module1.0
    ##
    ## cuda 11.3 modulefile
    ##
    
    proc ModulesHelp { } {
        global version
        
        puts stderr "\tSets up environment for CUDA $version\n"
    }
    
    module-whatis "sets up environment for CUDA 11.8"
    
    if { [ is-loaded cuda/11.8 ] } {
    module unload cuda/11.8
    }
    
    set version 11.3
    set root /usr/local/cuda-11.3
    setenv CUDA_HOME	$root
    
    prepend-path PATH $root/bin
    prepend-path LD_LIBRARY_PATH $root/extras/CUPTI/lib64
    prepend-path LD_LIBRARY_PATH $root/lib64
    conflict cuda
  • Similarly, create a modulefile /usr/share/modules/modulefiles/cuda/11.3 for CUDA 11.3 and add the following lines:

    #%Module1.0
    ##
    ## cuda 11.8 modulefile
    ##
    
    proc ModulesHelp { } {
        global version
        
        puts stderr "\tSets up environment for CUDA $version\n"
    }
    
    module-whatis "sets up environment for CUDA 11.8"
    
    if { [ is-loaded cuda/11.3 ] } {
    module unload cuda/11.3
    }
    
    set version 11.8
    set root /usr/local/cuda-11.8
    setenv CUDA_HOME	$root
    
    prepend-path PATH $root/bin
    prepend-path LD_LIBRARY_PATH $root/extras/CUPTI/lib64
    prepend-path LD_LIBRARY_PATH $root/lib64
    conflict cuda

c) Make CUDA 11.8 the default cuda version

  • Create a file /usr/share/modules/modulefiles/cuda.version to make CUDA 11.8 the default cuda module:
    #%Module
    set ModulesVersion 11.8

Note: make sure to reload your terminal.

5. Changing and Viewing the CUDA Module

  • To change and view the loaded CUDA module, you can use the following commands:
    # Check the currently loaded module
    module list
    # Check the available modules
    module avail
    
    # Load a specific cuda version
    module load cuda/11.3
    # Unload the currently loaded CUDA module
    module unload cuda
    # Load CUDA 11.8
    module load cuda/11.8
    
    # verify the paths of the loaded CUDA
    nvcc --version # should give the loaded CUDA version
    echo $CUDA_HOME
    echo $PATH
    echo $LD_LIBRARY_PATH

Note: You can add additional CUDA versions or other packages by creating corresponding modulefiles and following the steps outlined in this gist.

@mizeller
Copy link

mizeller commented May 8, 2024

In step 2 I had to use sudo apt-get -y install cuda-11-3 and sudo apt-get -y install cuda-11-8 respectively. The regular apt-get install command defaults to the latest version of CUDA (12.4)...

@garg-aayush
Copy link
Author

@mizeller I had to format my machine yesterday and reinstall CUDA. The above commands for cuda installation worked for me.

It might happen if you don't run the preceding to sudo apt-get -y install cuda given above. In that case, the installer picks up the latest CUDA version.

@omsrisagar
Copy link

Thank you for providing these instructions. I am having the same issue as @mizeller (I have executed all the steps prior to that). It defaults to the latest version which is 12.5 now. This is probably because I have a later version (12.1) already installed on my system and I am now trying to install 11.8

I have tried the method @mizeller mentioned (specify version explicitly such as cuda-11-8) and it worked.

@omsrisagar
Copy link

Unfortunately I hit another roadblock with environment modules. After following the steps (install environment modules and adding cuda modulefiles), module avail does not show the added cuda files. It only shows the default ones like dot, module-git, etc. Running module load cuda/11.8 will result in ERROR: Unable to locate a modulefile for 'cuda/11.8'.

Note that this is a problem with environment modules and not the general solution mentioned here.

@cieske
Copy link

cieske commented Jun 13, 2024

Thanks for grate guide! What is difference from managing multiple CUDA version using sudo update-alternatives --config cuda?

@garg-aayush
Copy link
Author

@cieske I don't use update-alternatives because it requires setting symbolic links, which I try to avoid. Initially, it can be a bit of too much work. Moreover, I am more used to managing multiple packages using module (a bit old school :-)).

If you are familiar with conda, you can use it to manage multiple CUDA environments. It is a lot simpler than these approaches. There is a great video by Jeremy explaining how you can install and manage different CUDA versions using conda: Getting Started With CUDA for Python Programmers.

@garg-aayush
Copy link
Author

@omsrisagar Defaulting to latest current version might happen, if you don't download the correct CUDA version package and set current keyring in Step 2: Install CUDA 11.8 and CUDA 12.1. I too agree, better to mention the package explicitly to be on safer side.

@garg-aayush
Copy link
Author

garg-aayush commented Jun 14, 2024

@omsrisagar ERROR: Unable to locate a modulefile for 'cuda/11.8' is most likely due to incorrect cuda version naming in module files. Best to take chatGPT help ;).

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