How to install Keras and TensorFlow from backend on Ubuntu

how to install keras on ubuntu

After finishing the Machine Learning course, I was looking where to continue. The development environments used in the Octave / Matlab prototyping course are not what people use, so you have to jump to something higher quality. Among the candidates that have been recommended to me the most is Keras, using backend TensorFlow. I'm not going to go into whether Keras is better than other tools or other frameworks or whether to choose TensorFlow or Theano. I'm just going to explain how it can be installed in Ubuntu.

First, I tried to install it from the documentation of the official pages, and it was impossible, I always had an error, an unresolved question. In the end I went to find specific tutorials on how to install keras in Ubuntu And yet I have spent two days spending a lot of time at night. In the end I have achieved it and I leave you how I have done it in case it can pave the way for you.

As we are going to follow the steps recommended by the websites that I leave you from sources at the end of the tutorial, we are going to install PIP that I did not have, to manage the packages. pip on linux it's just that, a package management system written in python.

sudo apt-get install python3-pip sudo apt install python-pip

Subscribe to our mailing list

Install virtualenv using pip3

With Virtualenv we can create virtual environments with Python. We could say that a virtual environment consists of encapsulating a project where we can work with different packages and in different versions.

Here the first problems have appeared when using sudo by the tutorial I was following (sudo pip3 install virtualenv) it returned the following error

Problems installing virtualenv

Some suggested clearing the http folder from cache but that has not worked. Another solution that I have not proposed is to use -H, that is, sudo -H pip3 install virtualenv. But the simplest solution that has worked in my case has been

pip3 install virtualenv

pip3 instead of pip means that we are going to use python 3

And we are going to install Virtualenvwrapper

Virtualenvwrapper us vitamin, automates many Virtualenv tasks and settings. It helps us to make everything easier. That is why we use it.

Following the steps proposed by various tutorials it seemed that everything was installed but when I ran the mkvirtualenva which is one of the steps below, it always told me that it did not recognize this instruction. In the end I was able to install it and make virtuanenvwrapper work like this.

pip install virtualenvwrapper

How to install virtualenvwrapper

Once we see Edit .bashrc with and we will put our source, that is, the address where we have the file

export WORKON_HOME = $ HOME / .virtualenvs export PROJECT_HOME = $ HOME / Devel source /home/nmorato/.local/bin/

These little things are the ones that I know that people starting out choke on because they don't know how to customize that line and find the path to their file. So there is a mini-explanation in 4 images

How to find and view the source or path of a file

  1. Open Nautilus, Ubuntu's file manager and click on other locations. It will show you your hard drive, choose the one you have Ubuntu installed on.
  2. Here we are at the root of our system. Click on the lupita that is above and the search engine will be displayed.
  3. Enter the name of the file, in this case and it will find you the ones in the whole system
  4. You get on top, click with the right button and give properties. There you will see its complete route. The one you have to take to modify the .bashrc


Well that's it. Once .bashrc is modified, execute that line in the console, in my case

source /home/nmorato/.local/bin/

run virtualenvwrapper on ubuntu

After an error in checking the tutorial

ERROR: virtualenvwrapper could not find virtualenv in your path

in this step I had to also install pip with

sudo apt install virtualenv


sudo apt install --reinstall virtualenv



We create keras environment in virtualenv and virtualenvwrapper

In my case I have called it keras_tf from TensorFlow which is the backend that we are going to use with Keras and I create the development environment.

mkvirtualenv keras_tf-p

It is very simple. With that it is already installed. From now on every time we want to enter we will enter

workon keras_tf

Install Tensor Flow

Very simple instruction. The truth is that here I have kept it simple. If you look at official documentation, there are many options.

pip install --upgrade tensorflow

To check that everything is going well we execute in console

 python >>> import tensorflow >>>
I have gotten an error associated with old CPUs that I will talk about at the end

Install keras

In order to install Keras, you must first install these python dependencies. It is also possible to take advantage of and install OpenCV now, but since I am not going to use it at the moment I have not wanted to complicate it further.

pip install numpy scipy pip install scikit-learn pip install pillow pip install h5py

And finally after all the above you can finally install Keras :)

pip install hard

We check the keras.json file from ~/.hard/hard.json you can click Search in nautilus, Ubuntu's file manager

The default values ​​have to be similar to this

{"floatx": "float32", "epsilon": 1e-07, "backend": "tensorflow", "image_data_format": "channels_last"}

Above all check which backend it is tensorflow and not theano and what image_data_format puts channel_last and not channels_first by theano

If you can't find keras.json

Most of the time the keras.json file and its subdirectories will not be created until you open a console and import the package directly.
So if this is your case and you cannot find it in your system, follow the following steps.
workon keras_tf python import keras quit ()

how to downgrade to tensrorflow, problem with avx instructions

Look again and magic !!! Now it appears.

If everything goes fine. You would have everything ready, you can start using Keras and enjoy Machine Learning, deep learning, artificial intelligence, ...

I have had an additional problem that will limit the use of TensorFlow. Look at the image and you will see that the last line is Illegal instruction ('core' generated) in English is the core dumped.

Problem with TensorFlow and AVX instructions. TensorFlow dumped

It appears that the precompiled binary versions of TensorFlow versions greater than 1.5 use AVX instructions that are not supported by older CPUs. After searching and searching, the only solution I found was on stackoverflow, where they said we had to stick with version 1.5

So I had to downgrade from TensorFlow to 1.5 If you have the same problem this is done with

pip install tensorflow == 1.5

So now what?

Well the first thing is to test Keras, how it works, if I dock it or not. If I am only going to do tests or if I am going to use it e truth in troubleshooting. The truth is that Keras is totally different from the use I made of Octave / Matlab in the Machine Learning course. With Keras, it appears that the algorithms do not even see them, you have them already implanted and you dedicate yourself to layering it. If I go ahead with it machine learning learning, and I need a more powerful tool maybe I will opt for cloud services where Keras is preconfigured like AWS, Azure, google cloud, etc.

But I leave this for later. I go step by step.


Leave a comment