How to install Tensorflow GPU wi
(see https://www.pytorials.com/how-to-install-tensorflow-gpu-with-cuda-9-2-for-python-on-ubuntu/)
Tensorflowis an open source software library developed and used by Google that is fairly common among students, researchers, and developers for deep learning applications such as neural networks. It has both the CPU as well as GPU version available and although the CPU version works quite well, realistically, if you are going for deep learning, you will need GPU. In order to use the GPU version of TensorFlow, you will need anNVIDIA GPU with a compute capability > 3.0. While it is technically possible to install tensorflow GPU version in a virtual machine, you cannot access the full power of your GPU via a virtual machine. So, I recommend doing a fresh install of Ubuntu if you don’t have Ubuntu before starting with the tutorial.
Tensorflow 1.8 with CUDA 9.2 and cuDNN 7.1.4 performs up to 37% faster when compared to earlier versions of Tensorflow. View full resultshere.
Step 1: Update and Upgrade your system:
sudo apt-get update
sudo apt-get upgrade
Step 2: Verify You Have a CUDA-Capable GPU:
lspci | grep -i nvidia
Note GPU model. eg. GeForce 840M
If you do not see any settings, update the PCI hardware database that
Linux maintains by entering update-pciids (generally found in /sbin) at
the command line and rerun the previous lspci command.
If your graphics card is from NVIDIA then gotohttp://developer.nvidia.com/cuda-gpusand verify if listed in CUDA enabled gpu list.
Note down its Compute Capability. eg. GeForce 840M 5.0
Step 3: Verify You Have a Supported Version of Linux:
To determine which distribution and release number you’re running, type the following at the command line:
uname -m && cat /etc/*release
The x86_64 line indicates you are running on a 64-bit system which is supported by cuda 9.1
Step 4: Install Dependencies:
Required to compile from source:
sudo apt-get install build-essential
sudo apt-get install cmake git unzip zip
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python2.7-dev python3.5-dev python3.6-dev pylint
Step 5: Install linux kernel header:
Goto terminal and type:
uname -r
You can get like “4.10.0-42-generic”. Note down linux kernel version.
To install linux header supported by your linux kernel do following:
sudo apt-get install linux-headers-$(uname -r)
Step 6: Install NVIDIA CUDA 9.2:
Remove previous cuda installation:
sudo apt-get purge nvidia*
sudo apt-get autoremove
sudo apt-get autoclean
sudo rm -rf /usr/local/cuda*
Install cuda :
For Ubuntu 16.04 :
sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub
echo "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64 /" | sudo tee /etc/apt/sources.list.d/cuda.list
Please make sure that
- PATH includes /usr/local/cuda-9.2/bin
- LD_LIBRARY_PATH includes /usr/local/cuda-9.2/lib64, or, add /usr/local/cuda-9.2/lib64 to /etc/ld.so.conf and run ldconfig as root
To uninstall the CUDA Toolkit, run the uninstall script in /usr/local/cuda-9.2/bin
For Ubuntu 17.10 and Ubuntu 18.04 :
sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1710/x86_64/7fa2af80.pub
echo "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1710/x86_64 /" | sudo tee /etc/apt/sources.list.d/cuda.list
For Both Options:
sudo apt-get update
sudo apt-get -o Dpkg::Options::="--force-overwrite" install cuda-9-2 cuda-drivers
Step 7: Reboot the system to load the NVIDIA drivers.
Step 8: Go to terminal and type:
echo 'export PATH=/usr/local/cuda-9.2/bin${PATH:+:${PATH}}' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda-9.2/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}' >> ~/.bashrc
source ~/.bashrc
sudo ldconfig
nvidia-smi
Check driver version probably Driver Version: 396.26
(not likely) If you got nvidia-smi is not found then you have
unsupported linux kernel installed. Comment your linux kernel version
noted in step 5.
Step 10: Install Dependencies
pip3 install -U pip six numpy wheel mock
pip3 install -U keras_applications==1.0.5 --no-deps
pip3 install -U keras_preprocessing==1.0.3 --no-deps
Step 11: Configure Tensorflow from source:
Preparing shell for buildExit all running programms, shells, then go to run [Win+R] and enter follwings:
"C:\msys64\msys2_shell.cmd" -use-full-path
Add bazel to path
export PATH=/c/bazel:$PATH
Start the process of building TensorFlow by downloading latest tensorflow 1.12 .
cd /c/
mkdir tensorflow
cd tensorflow/
git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow/
git checkout r1.12
python configure.py
Give python path in
Please specify the location of python. [Default is C:\Users\ACER\AppData\Local\Programs\Python\Python36\python.exe]:
Hit enter for default path or use your own python path but install step 10 to it.Press enter two times
Do you wish to build TensorFlow with Apache Ignite support? [Y/n]: Y
Do you wish to build TensorFlow with XLA JIT support? [y/N]: N
Do you wish to build TensorFlow with ROCm support? [y/N]: N
Do you wish to build TensorFlow with CUDA support? [y/N]: Y
Please specify the CUDA SDK version you want to use. [Leave empty to default to CUDA 9.0]: 10.0
Please specify the location where CUDA 10.0 toolkit is installed. Refer to README.md for more details. [Default is C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.0]: {press enter}
Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7]: 7.3.1
Please specify the location where cuDNN 7 library is installed. Refer to README.md for more details. [Default is C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v10.0]: {press enter}
Now we need compute capability which we have noted at step 1 eg. 5.0
Please note that each additional compute capability significantly increases your build time and binary size. [Default is: 3.5, 7.0]: 5.0
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is /arch:AVX]: {press enter}
Would you like to override eigen strong inline for some C++ compilation to reduce the compilation time? [Y/n]: Y
Configuration finished
Step 12: Build Tensorflow using bazel
The next step in the process to install tensorflow GPU version will be to build tensorflow using bazel. This process takes a fairly long time.To build a pip package for TensorFlow you would typically invoke the following command:
bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
Note:-
add "--config=mkl" if you want Intel MKL support for newer intel cpu for faster training on cpu
add "--config=monolithic" if you want static monolithic build (try this if build failed)
add "--local_resources 2048,.5,1.0" if your PC has low ram causing Segmentation fault or other related errors
This process will take a lot of time. It may take 3- 4 hours or maybe even more.Also if you see no cpu usages or build failed then restart pc and try again once only.The bazel build command builds a script named build_pip_package. Running this script as follows will build a .whl file within the tensorflow_pkg directory:To build whl file issue following command:
bazel-bin/tensorflow/tools/pip_package/build_pip_package tensorflow_pkg
To install tensorflow with pip:
cd tensorflow_pkg
pip3 install tensorflow*.whl
or use pip instead of pip3Note : if you got error like unsupported platform then make sure you
are running correct pip command associated with the python you used
while configuring tensorflow build.You can check pip version and associated python by following command
pip -V
Step 13: Verify Tensorflow installation
Run in terminal
python
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
If the system outputs the following, then you are ready to begin writing TensorFlow programs:Success! You have now successfully installed tensorflow 1.12 on your machine. If you are on Ubuntu OS, you might want to check out our other post here,How to install Tensorflow GPU with CUDA 10.0 for python on Ubuntu.Cheers!!For prebuilt wheels go tothislink .