Work on Jupyter Notebook with Keras & Tensorflow with Conda Virtualenv on Ubuntu

This is a step-by-step tutorial recording how to set Keras with Tensorflow with Conda Virtual Environment, and (bonus) work on Jupyter notebook. Here’s are some advantages using conda virtual environment instead of installing directly on your system:

  • Keep your system tidy: Multiple packages, different versions of python and Deep Neural Network (DNN) framework makes your system cluttered. Virtual Environment serves as an independent environment, the programs inside won’t affect your system.
  • Be able to work with different versions of software: Given a python 2.7 program while the version of python in your system is 3.6? Install python 2.7 with virtual environment!
  • Easy to manage: With Anaconda, you can easily upgrade, downgrade, mass-install packages you need for programs.

So let’s see how to do it.

  1. Install Anaconda: firstly you need to install Anaconda or Miniconda.
  • Anaconda (Recommended, as the full version):
bash Anaconda-latest-Linux-x86_64.sh
  • Miniconda:
bash Miniconda3-latest-Linux-x86_64.sh

2. Create a virtual environment with conda: (For python 3.6, change to other number if you want; myenv is the name of your virtualenv.)

conda create -n myenv python=3.6

Then you can activate the virtual environment with this command:

source activate myenv

When you have done your work on the virtual environment, do the following to deactivate it:

source deactivate myenv

3. Install Tensorflow (CPU/GPU version) on the virtual environment: firstly, ensure you have activated your virtual environment, then install Tensorflow with:

pip install --ignore-installed --upgrade tfBinaryURL

tfBinaryURL should be any one of the URLs listed here. For example, the following command installs the CPU-only version of TensorFlow for Python 3.6:

pip install --ignore-installed --upgrade \ https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.6.0-cp36-cp36m-linux_x86_64.whl

Then validate your TF installation with:

# Python
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
>>> Hello, Tensorflow!

4. Install Keras (with Tensorflow as backend): Also ensure you have activated your virtual environment. It’s very easy:

pip install keras

5. (Bonus) Set Jupyter notebook’s kernel as this virtual environment: Still, ensure you are working on your virtual environment, let’s say you want to do experiments using Jupyter notebook, you have to firstly install the ipython kernel module into your virtualenv:

pip install ipykernel

Then run the kernel “self-install” script:

python -m ipykernel install --user --name=myenv

Similarly, change --name=myenv to the name of your virtual environment. Finally, enter the directory you want to work on, then type in

jupyter notebook

The notebook will show up through your default browser. Then, change to the kernel (the environment, which is myenv) by clicking the “kernel” tab.

6. Congrats!

Enjoy your Experiments on Jupyter notebook with Keras & Tensorflow with Conda virtual environment on Ubuntu!