# Function to completely remove NVIDIA and CUDA packages and directories
remove_nvidia_cuda() {
    echo "Removing all NVIDIA and CUDA drivers, libraries, and configurations..."

    # Uninstall NVIDIA and CUDA packages
    apt-get remove --purge -y '*nvidia*' '*cuda*' '*cublas*' '*cufft*' '*curand*' '*cusolver*' '*cusparse*' '*nccl*' '*nvinfer*' '*nsight*'
    apt-get remove --purge -y 'libnvidia*' 'libcuda*' 'libcudnn*' 'libnv*' 'cuda-compiler*' 'cuda-libraries*'

    # Remove residual configuration files in standard locations
    rm -rf /usr/local/cuda*
    rm -rf /usr/lib/nvidia*
    rm -rf /etc/nvidia*
    rm -rf /opt/nvidia*
    rm -rf /var/lib/nvidia*
    rm -rf /var/log/nvidia*
    rm -rf /usr/share/nvidia*
    rm -rf /usr/src/cuda*
    rm -rf /usr/src/nvidia*
    rm -rf /usr/include/cuda*
    rm -rf /usr/include/nvidia*
    rm -rf /usr/local/cudnn*
    
    # Clean up any NVIDIA or CUDA-related cached files in /var/cache
    rm -rf /var/cache/apt/archives/*nvidia*
    rm -rf /var/cache/apt/archives/*cuda*
    rm -rf /var/cache/apt/archives/*cudnn*
    
    # Remove system-wide binaries and libraries if linked to NVIDIA or CUDA
    find /usr/local/bin -name '*nvidia*' -exec rm -rf {} \;
    find /usr/local/bin -name '*cuda*' -exec rm -rf {} \;
    find /usr/local/lib -name '*nvidia*' -exec rm -rf {} \;
    find /usr/local/lib -name '*cuda*' -exec rm -rf {} \;

    # Clean package lists and remove unnecessary packages
    apt-get autoremove -y
    apt-get clean
}

# Function to install CUDA Toolkit 12.6.2
install_cuda_toolkit() {
    echo "Installing CUDA Toolkit 12.6.2..."

    # Download and set up the CUDA repository
    wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-ubuntu2204.pin
    mv cuda-ubuntu2204.pin /etc/apt/preferences.d/cuda-repository-pin-600
    wget https://developer.download.nvidia.com/compute/cuda/12.6.2/local_installers/cuda-repo-ubuntu2204-12-6-local_12.6.2-560.35.03-1_amd64.deb
    dpkg -i cuda-repo-ubuntu2204-12-6-local_12.6.2-560.35.03-1_amd64.deb
    cp /var/cuda-repo-ubuntu2204-12-6-local/cuda-*-keyring.gpg /usr/share/keyrings/
    
    # Update package list and install CUDA Toolkit 12.6.2
    apt-get update
    apt-get -y install cuda-toolkit-12-6
    
    if [ $? -ne 0 ]; then
        echo "CUDA Toolkit 12.6.2 installation failed."
        exit 1
    else
        echo "CUDA Toolkit 12.6.2 installed successfully."
    fi
}

# Function to install the NVIDIA driver with a choice of kernel module flavor
install_nvidia_driver() {
    echo "Select the NVIDIA driver kernel module flavor to install:"
    echo "1. Open kernel module (recommended)"
    echo "2. Legacy kernel module"
    read -p "Enter the option (1 or 2): " choice

    if [ "$choice" == "1" ]; then
        apt-get install -y nvidia-open
        if [ $? -ne 0 ]; then
            echo "Open kernel module installation failed."
            exit 1
        else
            echo "NVIDIA Open kernel module installed successfully."
        fi
    elif [ "$choice" == "2" ]; then
        apt-get install -y cuda-drivers
        if [ $? -ne 0 ]; then
            echo "Legacy kernel module installation failed."
            exit 1
        else
            echo "NVIDIA Legacy kernel module installed successfully."
        fi
    else
        echo "Invalid choice. Exiting installation."
        exit 1
    fi
}

# Main execution flow
remove_nvidia_cuda
install_cuda_toolkit
install_nvidia_driver

# Verify installation
echo "Verifying NVIDIA driver and CUDA installation..."
nvidia-smi
nvcc --version
echo "Installation complete."Processing triggers for man-db (2.10.2-1) ..
INVIDIA Open kernel module installed successfully.
Verifying NVIDIA driver and CUDA installation...
Failed to initialize NVML: Driver/library version mismatch
NVMI library version: 565.57
/content /drive/MyDrive/N.sh: line 99: nvcc: command not found
Installation complete.
|/ content# nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2024 NIDIA Corporation
Built on Thu Sep 12_02:18:05 PDT 2024
Cuda compilation tools, release 12.6, V12.6.77
Build cuda_12.6.r12.6/compiler.34841621_0 |/content# nvidia-smi
Sat Nov
9 04:23:41 2024
+ -
-24
NVIDIA-SMI 535.104.05
Driver Version: 535.104.05
CUDA Version: 12.2