Ubuntu Builds Tenorflow-gpu Edition Environment

Posted by simonp on Wed, 02 Oct 2019 23:51:49 +0200

  1. Install graphics card driver
  2. Installation of cuda 10.1.2
    cuda official website: cuda-10.1
    Installation Based on Installation Procedure on Official Website
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pin

sudo mv cuda-ubuntu1804.pin /etc/apt/preferences.d/cuda-repository-pin-600

wget http://developer.download.nvidia.com/compute/cuda/10.1/Prod/local_installers/cuda-repo-ubuntu1804-10-1-local-10.1.243-418.87.00_1.0-1_amd64.deb

sudo dpkg -i cuda-repo-ubuntu1804-10-1-local-10.1.243-418.87.00_1.0-1_amd64.deb

sudo apt-key add /var/cuda-repo-10-1-local-10.1.243-418.87.00/7fa2af80.pub

sudo apt-get update

sudo apt-get -y install cuda

Set environment variables after installation, and input them at the terminal

sudo gedit ~/.bashrc

Add the last line in the file

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

Testing for installation success (takes a long time):

cd /usr/local/cuda-10.1/samples
sudo make

Continue typing after compilation is complete

cd /usr/local/cuda-10.1/samples/1_Utilities/deviceQuery
sudo make
./deviceQuery

If the display of "Result = PASS" indicates successful installation, I will restart the system before displaying "Result = PASS".

3. Install cuDNN
cudnn:cudnn
cudnn downloads need to be logged in. Register an account and download again.

Select cuDNN Library for Linux to download

Decompression cudnn

tar zxvf cudnn-10.1-linux-x64-v7.6.3.30.tgz

When the decompression is complete, you get a cuda file

Copy the files of include and lib64 folders to the corresponding directories, and set the specific path according to your own situation.

Enter the cuda file at the terminal and enter

sudo cp ./include/cudnn.h /usr/local/cuda-10.1/targets/x86_64-linux/include
sudo cp ./lib64/libcudnn* /usr/local/cuda-10.1/targets/x86_64-linux/lib/
sudo chmod a+r /usr/local/cuda-10.1/targets/x86_64-linux/include/cudnn.h
sudo chmod a+r /usr/local/cuda-10.1/targets/x86_64-linux/lib/libcudnn*
  1. Install tensorflow-gpu version
pip install tensorflow-gpu
  1. Test code
import tensorflow as tf

with tf.device('/cpu:0'):
    a = tf.constant([1.0, 2.0, 3.0], shape=[3], name='a')
    b = tf.constant([1.0, 2.0, 3.0], shape=[3], name='b')
with tf.device('/gpu:1'):
    c = a + b

# Note: allow_soft_placement=True indicates that the computing device can choose on its own, and if it does not have this parameter, it will report an error.
# Because not all operations can be placed on the GPU, an error will be reported if an operation that cannot be placed on the GPU is forcibly assigned to the GPU.
sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True))
# sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
sess.run(tf.global_variables_initializer())
print(sess.run(c))
[2. 4. 6.]

Reference resources:
1.Ubuntu 16.04 Configuring Tensorflow's GPU Deep Learning Environment
2.tensorflow-gpu test code

Topics: sudo Linux Session pip