Saturday, November 19, 2016

Setting up Keras and Anaconda Python on Ubuntu 16.10

I’ve been using Anaconda Python for most of my Machine Learning experiments, mainly because of the flexibility it gives with the isolated Python environments. I recently did a post on how to install Keras on Anaconda on Windows.

I’m planning to switch to Linux for few of my experiments, so I decided to try out setting up Anaconda Python and Keras from scratch on Ubuntu. I’ll be using the latest Ubuntu 16.10 (Yakkety Yak) 64-Bit for this.

Note: The screenshots I captured are from a virtual machine with Lubuntu 16.10 (the LXDE flavor of Ubuntu). But the steps and commands are exactly the same for the standard Ubuntu desktop as well.

First and foremost, get and install the latest updates in Ubuntu, (Reboot the machine if necessary after updating.)
 sudo apt-get update  
 sudo apt-get upgrade  

Then, we’ll install the following necessary packages,
 sudo apt-get install build-essential cmake git unzip pkg-config  
 sudo apt-get install libopenblas-dev liblapack-dev  

Now, on to installing Anaconda. Head over to the Anaconda Python Downloads page, and get the Linux installer for Anaconda. We’ll be getting the Python 3.5 64-Bit package.
Go to the Anaconda Download page and download the Anaconda Python 3.5 64-Bit package for Linux
Download the Anaconda Python 3.5 64-Bit package for Linux

This will download a file named (the version numbers might be different based on the latest version available at the time of the download).

Open a terminal, and navigate to the location of the downloaded file, and run the following command to start the installation,

You need to include the 'bash' command even if you’re not using the bash shell.
Start the Anaconda Installation
Start the Anaconda Installation

Note: When installing Anaconda on the default location, you don’t need root access, so there’s no need to 'sudo'.

Accept the defaults…
Accept the defaults of the Installation
Accept the defaults of the Installation

… and let it install,
Anaconda installation ongoing
Installation ongoing

At the end of the installation, it will ask whether to add the Anaconda install location to your PATH. Say 'yes' to that,
Select the option to add Anaconda to the PATH
Select the option to add Anaconda to the PATH

After the installation completed, close the terminal, and open a new one.

Now, we are ready to create an Anaconda environment and install Keras in it.

Create a new environment (I’ll name it ‘keras-test’) with the following packages.
 conda create --name keras-test numpy scipy scikit-learn pillow h5py  

Having these commonly used packages installed will make your life easier later on.

Creating the Anaconda environment and installing the packages
Creating the Anaconda environment and installing the packages

Once the environment is created, and the package installations are finished, activate the environment,
 source activate keras-test  

I’ll be initially using Keras with the Theano backend, so I’ll install Theano first.
 pip install --upgrade --no-deps git+git://  

(I’ll be doing a separate guide for installing TensorFlow on Ubuntu)

Finally, install Keras from pip,
 pip install keras  

Installing Keras on Anaconda
Installing Keras on Anaconda

Keas will by default try to use the TensorFlow backend. So, when using Theano, remember to switch the backend in Keras Config. See my post Switching between TensorFlow and Theano on Keras on how to switch backends.

Finally, let’s test the Keras installation by invoking the Python interpreter in our Anaconda environment and running the following command,
  >>> import keras  
  Using Theano backend.  

Keras on Anaconda on Linux, ready for action
Keras on Anaconda on Linux, ready for action

If you don’t get any errors, then we are ready to start developing Machine Learning models on Keras on Linux.

Related posts:
Getting Keras working with Anaconda Python
Switching between TensorFlow and Theano on Keras

Build Deeper: Deep Learning Beginners' Guide is the ultimate guide for anyone taking their first step into Deep Learning.

Get your copy now!


  1. worked for the most part -- had this error "ImportError: No module named 'tensorflow'"


    (keras-test) ptahx@PtahXGamer:~$ python
    Python 3.5.2 |Continuum Analytics, Inc.| (default, Jul 2 2016, 17:53:06)
    [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import keras
    Using TensorFlow backend.
    Traceback (most recent call last):
    File "", line 1, in
    File "/home/ptahx/.conda/envs/keras-test/lib/python3.5/site-packages/keras/", line 2, in
    from . import backend
    File "/home/ptahx/.conda/envs/keras-test/lib/python3.5/site-packages/keras/backend/", line 67, in
    from .tensorflow_backend import *
    File "/home/ptahx/.conda/envs/keras-test/lib/python3.5/site-packages/keras/backend/", line 1, in
    import tensorflow as tf
    ImportError: No module named 'tensorflow'

    1. Hi Ptahx,

      Thanks for the follow up.
      When you install Keras, it's by default is set to use the TensorFlow backend, not Theano.
      Since we only installed Theano is this guide, Keras gives the above error when it can't find Tensorflow.

      We can tell Keras to use the Theano backend, and not to look for Tensorflow by editing the Keras config file. The config file is named 'keras.json' and is located in the '.keras' directory of your home directory ('~/.keras/keras.json' on linux, or '%USERPROFILE%\.keras\keras.json' on windows).
      In that config file, change the "backend" parameter to "theano" (it would be "tensorflow" by default) and the "image_dim_ordering" parameter to "th" (would be "tf" by default). After changing these, Keras will start to use Theano instead of Tensorflow.

      You can find more detailed instructions of changing the config in this post:
      also, details explanation about the "image_dim_ordering" parameter:

  2. Hi, Thimira.
    Thanks very much for this guide. I have successfully created a new environment following your instructions. Activated the environment, installed theano and keras, and both can be successfully imported via python interpreter in that environment. However, when I try to execute code in a Jupyter notebook, it cannot find theano; result is shown below. Can you help? Thanks very much.

    import utils; reload(utils)

    from utils import plots

    ImportError Traceback (most recent call last)
    in ()
    ----> 1 import utils; reload(utils)
    2 from utils import plots

    /home/pdoerschuk/Workspace/FastAINotebooks/Notebooks/courses/deeplearning1/nbs/ in ()
    27 from IPython.lib.display import FileLink
    ---> 29 import theano
    30 from theano import shared, tensor as T
    31 from theano.tensor.nnet import conv2d, nnet

    ImportError: No module named theano

    1. Hi Peggy,

      I'm not 100% sure (I haven't used Jupyter much), but the issue appears to be due to a path issue. Since Keras and Theano are working with the Python interpreter, then it could be just Jupyter doesn't have them on its search path.

      Do you have the PYTHONPATH environmental variable set? if so, try adding the path to the '\Lib\site-packages' directory to it. You can find the path of your Anaconda environment by running 'conda info --envs'.

      Also, few more things to look at:
      Did you install Jupyter inside the Anaconda environment?
      If not, then Jupyter might be using the system installed Python and it's packages, not the ones you installed with Anaconda. You can try uninstalling the system installed Jupyter, and installing it in the Anaconda environment you created by 'conda install jupyter'.

      You can also try explicitly adding the site-packaged directory via code by adding,
      import sys

      Also, try the Jupyter Notebook Extensions from Anaconda, which give access to conda environments and packages to Pupyter,

      Let me know if this helps.