Install CUDA 9.2 on Ubuntu 18

CUDA is a parallel computing platform and application programming interface (API) model created by Nvidia. I am supposed to install this package in my system.

Download Packages

The official downloading page for CUDA is https://developer.nvidia.com/cuda-downloads. Currently, the latest version of the CUDA is 9.2. We can chose the corresponding packages.

Since there is no package for 18.04, I tried to use 17.10, hope it works fine.

Install Packages

$ ll
total 8
drwxr-xr-x 2 yu yu 54 Sep 17 16:42 ./
drwxr-xr-x 18 yu yu 4096 Sep 17 15:26 ../
-rw-rw-r-- 1 yu yu 2818 Sep 17 16:17 cuda-repo-ubuntu1710_9.2.148-1_amd64.deb
$ sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1710/x86_64/7fa2af80.pub
Executing: /tmp/apt-key-gpghome.MKJ6VPC7sN/gpg.1.sh --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1710/x86_64/7fa2af80.pub
gpg: requesting key from 'https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1710/x86_64/7fa2af80.pub'
gpg: key F60F4B3D7FA2AF80: public key "cudatools " imported
gpg: Total number processed: 1
gpg: imported: 1
$ sudo dpkg -i cuda-repo-ubuntu1710_9.2.148-1_amd64.deb
... (a lot of logs)
$ sudo apt-get update
... (a lot of logs)
$ # sudo apt-get install cuda -y # this command may cause some error as follow
$ sudo apt-get -o Dpkg::Options::="--force-overwrite" install cuda -y

Error and Solution

An error comes here:

Unpacking nvidia-396 (396.44-0ubuntu1) ...
dpkg: error processing archive /var/cache/apt/archives/nvidia-396_396.44-0ubuntu1_amd64.deb (--unpack):
trying to overwrite '/usr/lib/x86_64-linux-gnu/libGLX_indirect.so.0', which is also in package libglx-mesa0:amd64 18.0.5-0ubuntu0~18.04.1
Errors were encountered while processing:
/var/cache/apt/archives/nvidia-396_396.44-0ubuntu1_amd64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)

This error message terminated the installing process, and make apt command out of order as follow:

Reading package lists... Done
Building dependency tree
Reading state information... Done
You might want to run 'apt --fix-broken install' to correct these.
The following packages have unmet dependencies:
cuda-drivers : Depends: nvidia-396 (>= 396.37) but it is not installed
libcuda1-396 : Depends: nvidia-396 (>= 396.37) but it is not installed
nvidia-396-dev : Depends: nvidia-396 (>= 396.37) but it is not installed
nvidia-opencl-icd-396 : Depends: nvidia-396 (>= 396.37) but it is not installed
E: Unmet dependencies. Try 'apt --fix-broken install' with no packages (or specify a solution).

Found a solution here

We can fix the problem using command:

sudo apt-get -o Dpkg::Options::="--force-overwrite" install --fix-broken