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):
  • Miniconda:

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.)

Then you can activate the virtual environment with this command:

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

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

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:

Then validate your TF installation with:

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

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:

Then run the kernel “self-install” script:

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

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!

--

--

Computer Vision Applied Scientist @ Amazon. https://coldmanck.github.io

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store