Installing NVIDIA drivers and CUDA on Ubuntu 16.04 and 18.04
There are several ways to install NVIDIA drivers. This method using a runfile is the one that works most reliably for me.
-
Determine the driver version and CUDA version you need. The versions will depend on the GPU you have and the code you want to run. For example, specific pytorch and tensorflow versions are tied to specific CUDA versions.
-
Download the driver and CUDA version as a runfile from the NVIDIA website: CUDA Downloads
- Disable the default nouveau drivers. Create a file at
/etc/modprobe.d/blacklist-nouveau.conf
and add the following:blacklist nouveau options nouveau modeset=0
Then regenerate the kernel initramfs:
sudo update-initramfs -u
-
If you already have nvidia installed, uninstall it:
sudo apt-get purge nvidia-*
and reboot your machine. -
Upon restart, drop into the tty terminal with
Ctrl+Alt+F1
and log in. -
Stop running the gui. On Ubuntu 16.04 run
sudo service lightdm stop
, on 18.04 runsudo systemctl stop gdm3.service
. -
Find the runfile you downloaded from step 2 and run it:
sudo sh ./NAME_OF_RUNFILE.run
-
Follow the prompts to install the driver and CUDA. If the installation fails, you will need to check
/var/log/nvidia-installer.log
or/var/log/cuda-installer.log
to find out why. You can test if the installation was successful by runningnvidia-smi
.Check the reported driver and CUDA versions. -
Restart the gui. On Ubuntu 16.04 run
sudo service lightdm start
, on 18.04 runsudo systemctl start gdm3.service
. - Leave the tty terminal and return to the gui:
Ctrl+Alt+F7
.
For more detailed instructions, see the NVIDIA CUDA Installation Guide.