TensorFlow + Jupyter Notebook + Nvidia DIY Setup | by Choong Hong Cheng | Towards Data Science

Based on my first story, more detailed step by step setup using Xbuntu / Ubuntu 17.04. Note that we will be using a user that has sudo right instead of root directly.
Install Nvidia driver repository
sudo add-apt-repository ppa:graphics-drivers/ppasudo apt-get update  sudo apt-get upgrade  sudo apt-get install build-essential cmake g++ gfortran git pkg-config python-dev software-properties-common wget
Install CUDA 8.0 from Nvidia, get both base installer and patch 2. CUDA will also install nvidia driver accordingly specific to the CUDA version
sudo dpkg -i cuda-repo-ubuntu1604-8-0-local-*amd64.debsudo apt-get updatesudo apt-get install cuda
Setup the CUDA environment for local user
echo 'export PATH=/usr/local/cuda/bin:$PATH' >> ~/.bashrcecho 'export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH' >> ~/.bashrcsource ~/.bashrc
Test it using
nvcc -V
Finally install cuDNN, get it from Nvidia, this is for Tensorflow later. Get the right version for the current installed CUDA
cd ~/Downloads/tar xvf cudnn*.tgzcd cudasudo cp */*.h /usr/local/cuda/include/sudo cp */libcudnn* /usr/local/cuda/lib64/sudo chmod a+r /usr/local/cuda/lib64/libcudnn*
Grab the installer from Anaconda, using Python 3.6 and follow the installation instruction
cd ~/Downloads/bash Anaconda3-4.4.0-Linux-x86_64.sh
Normally I will install the Anaconda under /opt folder.
If is install under home folder, we need to locate the environment folder after activated the environment
source activate tf-gpuecho $PATH
This is important in order to setup Jupyter Notebook later.
Once we have Anaconda install, we going to create an environment for our Jupyter setup and install TensorFlow GPU
conda create --name tf-gpu python=3.6source activate tf-gpupip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.3.0-cp36-cp36m-linux_x86_64.whl
Once we install TensorFlow, we going install Jupyter, we going use conda to manage the packages for both Jupyter Notebook and shell runtime
conda install jupyter notebook numpy pandas matplotlib
Install any ddns client to able to update domain so we could connect back to our home server. We could use NoIP for this and it has linux client to update the domain.
Make sure we stop current nginx before using letsencrypt instead of buying a SSL certificate because I am on cost saving
sudo systemctl stop nginx
Once the NGINX is stop, we could run initial letsencrypt command that will spin off it’s own internal server for verification process
Clone letsencrypt using command, follow by setting up initial certificate
sudo git clone https://github.com/letsencrypt/letsencrypt /opt/letsencryptsudo -H /opt/letsencrypt/letsencrypt-auto certonly --email=me@chclab.net -d deeplearning.chclab.net -d jupyter-cpu.chclab.net -d jupyter-nvidia.chclab.net -d monitor.chclab.net
Setup the cron job to run the certificate renew process
0 0 * * *   /opt/letsencrypt/letsencrypt-auto renew --quiet
Create a shell script to start Jupyter Notebook at ~/start-tf-jupyter-gpu.sh This will use the conda environment called tf-gpu that we setup earlier.
#!/bin/bashexport PATH=/home/chc/.conda/envs/tf-gpu/bin:$PATH/home/chc/.conda/envs/tf-gpu/bin/jupyter notebook --no-browser --NotebookApp.allow_origin='*' --notebook-dir='/home/chc/project' --NotebookApp.port=9999
Secure the Jupyter Notebook by using the following command, more information about this from this link
jupyter notebook --generate-configjupyter notebook password
Create a Systemctl service at sudo vi /etc/systemd/system/jupyter-gpu.service
[Unit]Description=Service for jupyter cpu notebookAfter=local-fs.target network.target[Service]Type=simpleUser=chcGroup=chcExecStart=/home/chc/start-tf-jupyter-gpu.shRestart=alwaysSyslogIdentifier=jupyter cpu notebook[Install]WantedBy=multi-user.target
Enable the service and try to run it
sudo systemctl daemon-reloadsudo systemctl enable jupyter-gpu.servicesudo systemctl start jupyter-gpu.servicesudo systemctl status jupyter-gpu.service
Install NGINX
sudo apt-get install nginx
Setup the following NGINX, for me is sudo vi /etc/nginx/conf.d/jupyter-nvidia.chclab.net.conf
upstream notebook-tensorflow {        server localhost:9999;}server {        server_name jupyter-nvidia.chclab.net;        listen 443 ssl;        access_log off;ssl_certificate /etc/letsencrypt/live/deeplearning.chclab.net-0001/fullchain.pem;        ssl_certificate_key /etc/letsencrypt/live/deeplearning.chclab.net-0001/privkey.pem;ssl_protocols TLSv1 TLSv1.1 TLSv1.2;        ssl_prefer_server_ciphers on;        ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";        ssl_ecdh_curve secp384r1;        ssl_session_cache shared:SSL:10m;        ssl_session_tickets off;        ssl_stapling on;        ssl_stapling_verify on;        resolver 8.8.8.8 8.8.4.4 valid=300s;        resolver_timeout 5s;        # disable HSTS header for now        #add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";        add_header X-Frame-Options DENY;        add_header X-Content-Type-Options nosniff;ssl_dhparam /etc/ssl/certs/dhparam.pem;location / {            proxy_pass            http://notebook-tensorflow;            proxy_set_header      Host $host;        }location ~ /api/kernels/ {            proxy_pass            http://notebook-tensorflow;            proxy_set_header      Host $host;            # websocket support            proxy_http_version    1.1;            proxy_set_header      Upgrade "websocket";            proxy_set_header      Connection "Upgrade";            proxy_read_timeout    86400;        }        location ~ /terminals/ {            proxy_pass            http://notebook-tensorflow;            proxy_set_header      Host $host;            # websocket support            proxy_http_version    1.1;            proxy_set_header      Upgrade "websocket";            proxy_set_header      Connection "Upgrade";            proxy_read_timeout    86400;        }}
Generate a strong dhparam file
openssl dhparam -out /etc/ssl/certs/dhparam.pem 4096
Restart NGINX
sudo systemctl restart nginx
Finally there is how it look like using Jupyter Notebook