Install CUDA Toolkit 10.2 on Ubuntu 18.04.3

Official Document [link]

In fact, the official document is elaborated, however, somehow I failed to install it several times. So I recorded exact steps what I have done, FYI.

Installation

Step 1. Pre-installation Actions

In this step, please check whether your system is cuda-supported.

Note:

  • gcc version is important, please refer to Table 1 in the link. Recommend: gcc-7.
  • See Error case 1 to check and set default gcc version.
  • When downloading cuda-toolkit, recommend to use runfile(local), because for some reason, I tried deb(local) and deb(network), both failed.

Step 2. Package Manager Installation

If you use runfile(local), then there is no need to Install repository meta-data and Installing the CUDA public GPG key. You can directly execute:

1
2
sudo apt-get update 
sudo apt-get install cuda

If the installation is broken, try:

1
sudo apt --fix-broken install

Step 3. Check installation

You can check whether you have successfully installed cuda by:

1
nvidia-smi

Then you will get something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 440.33.01 Driver Version: 440.33.01 CUDA Version: 10.2 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 GeForce RTX 2060 On | 00000000:01:00.0 On | N/A |
| 45% 35C P8 10W / 160W | 1062MiB / 5931MiB | 2% Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
| 0 1174 G /usr/lib/xorg/Xorg 40MiB |
| 0 1309 G /usr/bin/gnome-shell 51MiB |
| 0 1536 G /usr/lib/xorg/Xorg 511MiB |
| 0 1679 G /usr/bin/gnome-shell 174MiB |
| 0 2165 G ...quest-channel-token=1797818128468897323 160MiB |
| 0 3936 G ...uest-channel-token=17969104812798294633 118MiB |
+-----------------------------------------------------------------------------+

Step 4 Set environment variables

The PATH variable needs to include /usr/local/cuda-10.2/bin and /usr/local/cuda-10.2/NsightCompute-. refers to the version of Nsight Compute that ships with the CUDA toolkit, e.g. 2019.1.

To add this path to the PATH variable:

1
$ export PATH=/usr/local/cuda-10.2/bin:/usr/local/cuda-10.2/NsightCompute-2019.1${PATH:+:${PATH}}

In addition, when using the runfile installation method, the LD_LIBRARY_PATH variable needs to contain /usr/local/cuda-10.2/lib64 on a 64-bit system, or /usr/local/cuda-10.2/lib on a 32-bit system

  • To change the environment variables for 64-bit operating systems:

    1
    2
    $ export LD_LIBRARY_PATH=/usr/local/cuda-10.2/lib64\
    ${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
  • To change the environment variables for 32-bit operating systems:

    1
    2
    $ export LD_LIBRARY_PATH=/usr/local/cuda-10.2/lib\
    ${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

Error cases

1. Modify default gcc version

Step 1. Install different gcc versions using apt-get, for example:

1
sudo apt-get install gcc-4.8
1
sudo apt-get install gcc-7

Step 2. Check if gcc is installed successfully:

1
gcc --verison

or

1
gcc-7 --version

Step 3. Check priority of gcc

1
sudo update-alternatives --config gcc

You can see something like this:

1
2
3
4
5
6
7
8
There are 2 choices for the alternative gcc (providing /usr/bin/gcc).

Selection Path Priority Status
---------------------------------------------------------
* 0 /usr/bin/gcc-7 200 auto mode
1 /usr/bin/gcc-4.8 100 manual mode
2 /usr/bin/gcc-7 200 manual mode

Step 4. Modify priority of gcc

1
sudo update-alternative --install /usr/bin/gcc gcc /usr/bin/gcc-7 100

Then you can see the priority of gcc-7 is set to be 100. You can check using the command in Step 3.

2. Sub-process /usr/bin/dpkg returned an error code (1)

1
2
3
4
5
6
7
8
cd /var/lib/dpkg/
sudo mv info/ info_bak # rename infor
sudo mkdir info # mkdir new info
sudo apt-get update # update
sudo apt-get -f install # fix
sudo mv info/* info_bak/ # remove new files to old
sudo rm -rf info # remove old
sudo mv info_bak info # rename new to info

(reference)