Install Nvidia GPU on Google Compute Instance

Here is a step by step on how to fix this ERROR: Unable to find the kernel source tree for the currently running kernel. For me this error happens every-time the instance is shutdown and started after a while.

  1. Update your system
sudo apt update && sudo apt upgrade

2. Install required packages (kernel headers and development packages)

sudo apt install build-essential dkms linux-headers-$(uname -r)

3. Generate Initramfs

sudo update-initramfs -u

4. Reboot to apply changes

sudo reboot

5. Download install script

curl https://raw.githubusercontent.com/GoogleCloudPlatform/compute-gpu-installation/main/linux/install_gpu_driver.py --output install_gpu_driver.py

6. Install driver

sudo python3 install_gpu_driver.py

7. Check if driver installed

sudo nvidia-smi