Install Conda TensorFlow-gpu and Keras on Ubuntu 18.04
Cuda toolkit 9.0, cuDNN 7.1, TensorFlow 1.8, Python 3.6, Miniconda3
I like to share my experience with installing a deep learning environment on a fresh Ubuntu 18.04 installation. The installation includes Nvidia software, TensorFlow that supports gpu, keras, numpy , etc. And jupyter notebook.
To run TensorFlow with GPU support, ensure your system has a NVIDIA® Supported GPU’s. If your graphics card is from NVIDIA and it is listed in http://developer.nvidia.com/cuda-gpus, your GPU is CUDA-capable. If your graphic card doesn't show, don't give up, google it, and you might get lackey like I was, that my old GeForce GT 755M, support Cuda although its not on the list.
Step 1. Install Miniconda
You can choose between installing Anaconda and Miniconda. Miniconda is the management system. When Installing Anaconda, you basically install Miniconda with 150 scientific python libraries. So Install Anaconda if:
- You have time and computer space.
Install Miniconda if:
- You want to save time and space.
- You will install all needed packages yourself.
Installation guide, here. Download Miniconda from here, then run:
bash Miniconda3-latest-Linux-x86_64.sh
Step 2. Install NVIDIA drivers
To install Nvidia drivers, you need Java. Install with:
sudo apt update
sudo apt install openjdk-8-jdk
Install gcc-4.8. This version is needed for cuda to compile tensorflow
sudo apt-get install gcc-4.8 g++-4.8
gcc --version
If you have already gcc version as default. Fix it with:
rm /usr/bin/gcc
rm /usr/bin/g++
ln -s /usr/bin/gcc-4.3 /usr/bin/gcc
ln -s /usr/bin/g++-4.3 /usr/bin/g++
For this part, I used the guide from here.
Install Drivers
Add graphics drivers to your source list :
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt update
sudo apt upgrade
Check what driver will be installed :
ubuntu-drivers devices
I got the following output:
== /sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0 ==
modalias : pci:v000010DEd00000FCDsv000017AAsd00003800bc03sc00i00
vendor : NVIDIA Corporation
model : GK107M [GeForce GT 755M]
driver : nvidia-driver-390 - third-party free
driver : nvidia-driver-396 - third-party free recommended
driver : nvidia-340 - third-party free
driver : xserver-xorg-video-nouveau - distro free builtin
Auto install latest driver (it will do everything blacklist drivers nouveau , create nvidia daemon , ect …) :
sudo ubuntu-drivers autoinstall
Then reboot your machine :
sudo reboot
If you boot without any kernel crash you’re ok but you can check the correct install of the driver :
lsmod | grep nvidia
or:
nvidia-smi
I got the following output:
nvidia-smi output when no GPU process is runing
Pay attention that in the above method, we install the latest Nvidia drivers. In the time of writing its 9.2. But we install Navidia toolkit 9.0 version. According to Nvidia documentation, that suppose to work.
Install Cuda
Install CUDA®, which is a parallel computing platform and programming model developed by NVIDIA. Cuda is needed needed to run TensorFlow with GPU support. Ubuntu 18.04 is not listed in the supported platforms, but it still works.
Install CUDA® Toolkit 9.0. For details, see NVIDIA’s documentation.
Download Cuda Toolkit 9.0 from here. Choose the following settings:
- Pay attention that cuda navigates by default to the last Cuda toolkit version, currently 9.2. Google 9.0 version and install it. TensorFlow expects 9.0 Cuda version
Run the following, and follow instructions from command line.
sudo sh cuda_9.0.176_384.81_linux.run --override --silent --toolkit
If everything is OK you should see a cuda installation folder in:
$ ls /usr/local/cuda-9.0/
bin include libnsight nvml samples tools
doc jre libnvvp nvvm share version.txt
extras lib64 nsightee_plugins pkgconfig src
Add Cuda to your PATH:
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}}
Install cuDNN
cuDNN is a GPU-accelerated library of primitives for deep neural networks. cuDNN provides highly tuned implementations for standard routines such as forward and backward convolution, pooling, normalization, and activation layers. cuDNN is part of the NVIDIA Deep Learning SDK. cuDNN 7.1 can be downloaded here. To download you need to sign in or log in to your Nvidia account. I downloaded the cuDNN v7.1.4 Library for Linux. to Istall run:
# Unpack the archive
tar -xzvf cudnn-9.0-linux-x64-v7.1.tgz # Move the unpacked contents to your CUDA directory
sudo cp cuda/include/cudnn.h /usr/local/cuda/include
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64# Give read access to all users
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
Check if you have correctly copied cudnn in /usr/local/cuda/lib64/.
$ ls /usr/local/cuda/lib64/
libaccinj64.so libcufftw.so.9.0 libnppc_static.a libnppig.so.9.0 libnpps_static.a
libaccinj64.so.9.0 libcufftw.so.9.0.176 libnppial.so libnppig.so.9.0.176 libnvblas.so
libaccinj64.so.9.0.176 libcufftw_static.a libnppial.so.9.0 libnppig_static.a libnvblas.so.9.0
libcublas_device.a libcuinj64.so libnppial.so.9.0.176 libnppim.so libnvblas.so.9.0.176
...
Now you must add some path to your /.bashrc :
gedit ~/.bashrc
Add those line at the end of your /.bashrc :
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64"
export CUDA_HOME=/usr/local/cuda
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}}
Now reload your terminal config :
source ~/.bashrc
sudo ldconfig
Check if the path are correctly installed :
$ echo $CUDA_HOME
/usr/local/cuda
Step 3. Install Tensorflow with Gpu support
Installation guide here: .
Create virtual environment, I names it tf36 for tensorflow and python 3.6
conda create --name tf36
source activate tf36
There are many discussion on the net if TensorFlow should br installed with pip or with conda. Even though TensorFlow documentation recommend pip installation, I decided to try installing with conda, since mixing conda and pip installations, might cause problems. Install TensorFlow:
conda install -c anaconda tensorflow-gpu
Check TensotFlow installation with:
python
>>> import tensorflow as tf
>>> tf.Session(config=tf.ConfigProto(log_device_placement=True))
output:
...
Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: GeForce GT 755M, pci bus id: 0000:01:00.0, compute capability: 3.0
/job:localhost/replica:0/task:0/device:GPU:1 -> device: 1, name: GeForce GT 755M, pci bus id: 0000:07:00.0, compute capability: 3.0
2018-07-18 18:49:50.512453: I tensorflow/core/common_runtime/direct_session.cc:284] Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: GeForce GT 755M, pci bus id: 0000:01:00.0, compute capability: 3.0
/job:localhost/replica:0/task:0/device:GPU:1 -> device: 1, name: GeForce GT 755M, pci bus id: 0000:07:00.0, compute capability: 3.0
If you get errors like: TensorFlow binary was not compiled to use: AVX AVX2
nothing to worry about. Read this StackOverFlow answer for detailes.
Step 4. Install Jupyter notebook and other packages
Install Jupyter notebook:
conda install -c conda-forge jupyter
conda install -c conda-forge keras
All is set. Just run:
$ jupyter notebook
Other needed libraries:
conda install -c conda-forge matplotlib
conda install -c conda-forge sklearn-contrib-lightning
Tools to manage your GPU
Verify cuda installation. Simple tests detailed here.
Get full info of devices:
cd /usr/local/cuda/samples/1_Utilities/deviceQuery
sudo make
./deviceQuery
Check if cards are recognized on the bus:
lspci | grep -i nvidia
Check drivers:
dmesg | grep NVRM
Trouble Shooting
Issues with gcc version are solve in the AskUbuntu answer.