Detailed tutorial on installing CUDA9.0 on Ubuntu16.04

Detailed tutorial on installing CUDA9.0 on Ubuntu16.04

Preface:

This article is based on the experience of installing CUDA 9.0. CUDA 9.0 currently supports Ubuntu 16.04 and Ubuntu 17.04, as shown in the following figure (the installation method at the bottom is the first one, that is, the runfile method):


You can download the CUDA file first, but it is best not to rush to install it. You must first carefully read the official guide provided by NVIDIA, and then find a few good blogs to read to get a general understanding of the CUDA installation process. You should have a general understanding of the problems that may arise during the installation process. Do not reinstall the system unless it is absolutely necessary.

Installation suggestions:

1) When you download CUDA from the official website, be sure to find a copy of the official installation document and read it carefully. Follow the steps step by step as much as possible and don't be lazy. At the same time, find a few good blogs for reference so that you can be confident before installation.

2) Before installation, be sure to check your computer configuration (single graphics card or dual graphics cards), whether the type of graphics card meets the CUDA installation requirements, and whether the system meets the installation requirements.

3) During the installation process, try to check whether each operation is successful.

Installation process:

1. Install and familiarize yourself with the Ubuntu 16.04 system

Before installing the software, it is best to have some basic understanding of Ubuntu's command line, such as sudo, cd, ls, nona, cat, chmod, etc., which can save a lot of unnecessary trouble during the installation process. (I recommend you to search for Mofan Python on Baidu. He has videos about Ubuntu commands. Each episode is very concise and well explained.)

2. Check whether your computer environment has the conditions for installing CUDA

1) Verify that your computer has a CUDA-capable GPU

You can find the specific model of the graphics card in the computer's configuration information. If you have a dual system, you can also find detailed information about the graphics card in the device manager under Windows.
You can also enter the command in the Ubuntu terminal: $ lspci | grep -i nvidia, which will display your NVIDIA GPU version information, but it is not very detailed.

My display is (GeForceGT630M):

01:00.0 3D controller: NVIDIA Corporation GF117M [GeForce 610M/710M/810M/820M / GT 620M/625M/630M/720M] (rev a1)

Then go to the CUDA official website to check whether your GPU version is in the CUDA support list.

2) Verify that your Linux version supports CUDA (Ubuntu 16.04 is fine)

Enter the command:

$ uname -m && cat /etc/*release

The results show:

x86_64
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04

......

3) Verify whether gcc is installed on the system

Type in the terminal: $ gcc –version

The results show:

gcc (Ubuntu 5.4.0-6ubuntu1~16.04.5) 5.4.0 20160609
......

If it is not installed, please use the following command to install it:

sudo apt-get install build-essential

4) Verify that the kernel header and package development are installed on the system

a. Check the running system kernel version:

Type in the terminal: $ uname –r

The results show:

4.10.0-40-generic

b. Enter in the terminal: $ sudo apt-get install linux-headers-$(uname -r)

You can install the kernel header and package development of the corresponding kernel version

The results show:

......

0 packages upgraded, 0 packages newly installed, 0 packages to be removed, and 4 packages not upgraded.

It means that it is already in the system and there is no need to install it again.

If all the above verification checks meet the requirements, you can proceed to the following formal installation process. If the requirements are not met, you can refer to the official documentation of CUDA, which contains detailed solutions to each problem.

3. Choose the installation method

CUDA provides two installation methods: package manager installation and runfile installation. The package manager installation method is relatively simple, but when I read other people's blogs, I found that choosing this method may cause more problems during the installation process and the probability of failure is higher. In order to reduce unnecessary trouble, I chose the runfile installation method.
Download the cuda installation package: Download from the cuda official website, select the corresponding version according to the system information, and select the runfile file as the last item for runfile installation.

After downloading, use MD5 to check. If the serial number does not match, you have to download it again (because I didn't save it at the time, I used someone else's screenshot here. Note the cuda version number here, which is cuda_8.0)

Enter the command: $ md5sum cuda_9.0.176_linux.run

4. Runfile installs cuda

1) Disable the nouveau driver

Run in the terminal: $ lsmod | grep nouveau. If there is output, it means nouveau is being loaded. We need to manually disable nouveau.

How to disable nouveau in Ubuntu:

a. Create the file blacklist-nouveau.conf in /etc/modprobe.d

Enter the command: $ sudo vi /etc/modprobe.d/blacklist-nouveau.conf (Use the vi editor to edit and save the file)

Enter the following content into the file:

blacklist nouveau
options nouveau modeset=0

b. Execution:

$ sudo update-initramfs –u

c. Execute again:

$ lsmod | grep nouveau

If there is no content output, the disabling is successful. If there is still content output, please check the operation and repeat the above operation.

Note: vi is a commonly used editor in Linux terminals or consoles. The basic operation method is: vi /path/file name. For example: vi /etc/fstab means displaying the contents of the /etc/fstab file. Use the Page Up and Page Down keys on the keyboard to turn pages up and down; press the Insert key, you can see the word "Insert" in the lower left corner of the window, indicating that the current state is insert editing, and the content entered from the keyboard will be inserted into the cursor position; press the Insert key again, there will be the word "Replace" in the lower left corner, indicating that the current state is replace editing, and the content entered from the keyboard will replace the content at the cursor position. After editing the content, press the Esc key, enter ":wq", and press Enter to save and exit.
If you do not want to save and want to exit directly, press the Esc key, enter ":q!", and then press Enter. "wq" means Write and Quit, which means saving and exiting; "q!" means ignoring changes and forcibly exiting.

It is recommended to take a photo with your phone for the following operations. It is recommended to rename the downloaded cuda_9.0.176_384.81_linux.run file to cuda.run and move it to the Home folder (for easy installation)

2) Restart the computer. When you enter the login interface, do not log in to the desktop (otherwise it may fail. If you accidentally enter, please restart the computer). Press Ctrl+Alt+F1 to enter the text mode (command line interface) and log in to your account.

3) Enter $ sudo service lightdm stop to close the graphical interface

4) Switch to the path of the cuda installation file: $ cd Home/

Run $ sudo sh cuda_7.5.18_linux.run
Follow the prompts step by step

Note: a. Be sure to enter the corresponding characters according to the prompts. For example, some require you to enter accept, and some require you to enter yes;

b. When prompted whether to install OpenGL, select no (if your computer has dual display like mine, and the main display is a non-NVIDIA GPU, you need to select no, otherwise you can select yes). For the rest, select yes or the default. (If your computer has dual graphics cards and you selected yes in this step, you will most likely encounter a login screen loop problem after restarting the graphical interface after installing CUDA: after entering the password, it jumps back to the password input interface.

This is because your computer has dual display, and the GPU used for display is not NVIDIA, then OpenGL Libraries should not be installed, otherwise the OpenGL Libraries of the GPU you are using (non-NVIDIA GPU) will be overwritten, and then the GUI will not work. )

If the installation is successful, it will display "installed", otherwise it will display "failed".

5) Enter $ sudo service lightdm start to restart the graphical interface.

Press Alt + ctrl + F7 at the same time to return to the graphical login interface and enter the password to log in.
If you can log in successfully, it means that there will be no problem of cyclic login, which basically means that CUDA is installed successfully.

If you encounter repeated logins, don't rush to reinstall the system. The official tutorial mentions this. The reason is mentioned in the note in the previous step. You may have accidentally selected yes when installing openGL. Please uninstall cuda and then reinstall it.
Uninstall: Since we cannot log in to the Graphical User Interface (GUI), we can enter the Text User Interface (TUI)

In the login interface, press Ctrl + Alt + f1 to enter TUI
implement

$ sudo /usr/local/cuda-8.0/bin/uninstall_cuda_8.0.pl
$ sudo /usr/bin/nvidia-uninstall

Then reboot

$ sudo reboot

Reinstall .run When you install again, please be sure to pay attention. When prompted whether to install OpenGL, if you have dual graphics cards, you should choose n.

6) Restart the computer and check Device Node Verification.

implement

$ ls /dev/nvidia*

There may be two results, a and b. Please choose the one that suits you best.

a. If the result shows

/dev/nvidia0 /dev/nvidiactl /dev/nvidia-uvm

Or similar information is displayed. There should be three (including one similar to /dev/nvidia-nvm), which means the installation is successful.

b. Most of the results may be like this

ls: cannot access/dev/nvidia*: No such file or directory

Or something like this, only

/dev/nvidia0 /dev/nvidiactl

One or both of a, but no /dev/nvidia-num, that is, the file display is incomplete.

Don't worry or rush to reinstall the system (this is what happened when I was installing it). The official guide has a detailed solution, but my method is slightly different from the official one.

First, you need to add a startup script (there are two ways to add a startup script. I use the most direct method. The other method is to create a file first and then move it to the startup folder through mv. You can search Baidu by yourself)
implement

$ sudo vi /etc/rc.local

If you are opening this file for the first time, it should be empty (except for lines and lines of # comments). The first line of this file is

#!/bin/sh -e

Remove -e (this step is very important, otherwise it will not load the content of this text)
Then copy the following contents except #!/bin/bash into it (before exit 0 ), save the file and exit.

#!/bin/bash

/sbin/modprobe nvidia

if [ "$?" -eq 0 ]; then
# Count the number of NVIDIA controllers found.
NVDEVS=`lspci | grep -i NVIDIA`
N3D=`echo "$NVDEVS" | grep "3D controller" | wc -l`
NVGA=`echo "$NVDEVS" | grep "VGA compatible controller" | wc -l`

N = `expr $N3D + $NVGA - 1`
for i in `seq 0 $N`; do
mknod -m 666 /dev/nvidia$ic 195 $i
done

mknod -m 666 /dev/nvidiactl c 195 255

else
exit 1
fi

/sbin/modprobe nvidia-uvm

if [ "$?" -eq 0 ]; then
# Find out the major device number used by the nvidia-uvm driver
D=`grep nvidia-uvm /proc/devices | awk '{print $1}'`

mknod -m 666 /dev/nvidia-uvm c $D 0
else
exit 1
fi

Next time you reboot, you should be able to see the three nvidia files directly in the /dev directory. Type: $ ls /dev/nvidia*
The result shows: /dev/nvidia0 /dev/nvidiactl /dev/nvidia-uvm

success!

7) Set environment variables.

Enter $ sudo gedit /etc/profile in the terminal
At the end of the opened file, add the following two lines.

64-bit Systems:

export PATH=/usr/local/cuda-9.0/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-9.0/lib64\
                          ${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

32-bit systems:

export PATH=/usr/local/cuda-9.0/bin${PATH:+:${PATH}}

export LD_LIBRARY_PATH=/usr/local/cuda-9.0/lib\
                          ${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}


Save the file and reboot. Because source /etc/profile is only effective temporarily, it will take effect permanently only after restarting the computer.

This is slightly different from the official installation documentation and needs to be explained:
The official documentation says that you only need to run the above two export statements in the terminal, but if you do not write them into the /etc/profile file, such environment variables will disappear after you exit the terminal and will not work, so writing them is a permanent approach.

8) Restart the computer and check whether the above environment variables are set successfully.

a. Verify the driver version

Knock in

$ cat /proc/driver/nvidia/version

Results

NVRM version: NVIDIA UNIX x86_64 Kernel Module 384.81 Sat Sep 2 02:43:11 PDT 2017
GCC version: gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.5)

b. Verify CUDA Toolkit

Knock in

$ nvcc -V will output CUDA version information

If it is like this:

The program 'nvcc' is currently not installed. You can install it by typing:
sudo apt-get install nvidia-cuda-toolkit

It may be that the environment configuration was not successful, please repeat the above step 7).

5. Try to compile the examples provided by cuda

1) Open the terminal and enter: $ cd /home/xxx/NVIDIA_CUDA-9.0_Samples, where xxx is your own user name. Use the cd command to enter the NVIDIA_CUDA-9.0_Samples directory.

Then enter in the terminal: $ make

The system will automatically enter the compilation process. The whole process will take about ten to twenty minutes. Please wait patiently. If an error occurs, the system will immediately report an error and stop.

The first time you run it, an error may be reported, and the error message may be that there is no gcc in the system.

The solution is to reinstall gcc through the command. Enter in the terminal: $ sudo apt-get install gcc After installing gcc, make will work normally.

If the compilation is successful, Finished building CUDA samples will be displayed at the end, as shown in the following figure.

2) Run the compiled binary file.
The compiled binary files are stored in NVIDIA_CUDA-9.0_Samples/bin by default.
Then enter in the previous terminal: $ cd /home/lxxx/NVIDIA_CUDA-9.0_Samples/bin/x86_64/linux/release where xxx is your own user name and then enter in the terminal: $ ./deviceQuery

The result is as shown in the figure below: If you see a display similar to the following picture, it means that CUDA is installed and configured successfully, where Result = PASS means success, and Result = FAIL if it fails

3) Finally, check the connection between the system and the CUDA-Capable device. Enter in the terminal: $ ./bandwidthTest
If you see a display similar to the following picture, it means success


Finally, I wish everyone a happy installation and use of CUDA

This is the end of this article about the detailed tutorial on installing CUDA9.0 on Ubuntu16.04. For more information about installing CUDA9.0 on Ubuntu16.04, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed solution to the problem of "Unable to start the program, the system cannot find the specified file" when writing C program or CUDA program in VS2019
  • Installation of CUDA10.0 and problems in Ubuntu
  • Detailed configuration of win10+VS2017+Cuda10.0 environment
  • Detailed explanation of the correspondence between different Tensorflow version requirements and CUDA and CUDNN versions
  • Implementation of installing and uninstalling CUDA and CUDNN in Ubuntu
  • Steps to install cuda10.1 on Ubuntu 20.04 (graphic tutorial)
  • Ubuntu 20.04 CUDA & cuDNN Installation Method (Graphical Tutorial)
  • Manjaro installation CUDA implementation tutorial analysis

<<:  React+Typescript implements countdown hook method

>>:  Detailed explanation of MySQL foreign key constraints

Recommend

How to use MySQL stress testing tools

1. MySQL's own stress testing tool - Mysqlsla...

js realizes 3D sound effects through audioContext

This article shares the specific code of js to ac...

Basic implementation method of cross-component binding using v-model in Vue

Hello everyone, today we will talk about how to u...

javascript input image upload and preview, FileReader preview image

FileReader is an important API for front-end file...

Detailed explanation of HTML basics (Part 2)

1. List The list ul container is loaded with a fo...

Detailed explanation of common methods of JavaScript arrays

Table of contents Common array methods pop() unsh...

How to customize Docker images using Dockerfile

Customizing images using Dockerfile Image customi...

MySQL Series II Multi-Instance Configuration

Tutorial Series MySQL series: Basic concepts of M...

How to delete special character file names or directories in Linux

Delete a file by its inode number First use ls -i...

9 ways to show and hide CSS elements

In web page production, displaying and hiding ele...

Several methods of calling js in a are sorted out and recommended for use

We often use click events in the a tag: 1. a href=...

Front-end state management (Part 2)

Table of contents 1. Redux 1.1. Store (librarian)...

A detailed guide to custom directives in Vue

Table of contents 1. What is a custom instruction...