Setup Theano and Keras with CUDA support on Ubuntu 16.04

Make sure you have installed Nvidia drivers and cuda toolkit on your system. Also follow caffe setup for preliminary setup of libraries.

Step 1: Install Dependencies

sudo apt-get install python-numpy python-scipy python-dev python-pip python-nose g++ libopenblas-dev liblapack-dev gfortran git

Step 2: Now Install Theano

sudo pip install Theano

Step 3: Work around for a glibc bug

echo -e "\n[nvcc]\nflags=-D_FORCE_INLINES\n" >> ~/.theanorc

Step 4: ERROR (theano.sandbox.cuda): nvcc compiler not found on $PATH. Check your nvcc installation and try again.

Open file “gedit ~/.theanorc” add edit the path to CUDA root:


root = /usr/local/cuda-8.0

Step 5: Install Libgpuarry

Libgpuarry: Required for GPU/CPU code generation on CUDA and OpenCL devices

For the stable version of Theano you need a specific version of libgpuarray, that has been tagged v0.6.2. Download it with:

git clone
cd libgpuarray
git checkout tags/v0.6.2 -b v0.6.2

For libgpuarray:

mkdir Build
cd Build
# you can pass -DCMAKE_INSTALL_PREFIX=/path/to/somewhere to install to an alternate location
cmake .. -DCMAKE_BUILD_TYPE=Release # or Debug if you are investigating a crash
sudo make install
cd ..

For pygpu:

# This must be done after libgpuarray is installed as per instructions above.
python build
sudo python install

If installed globally under Linux (in /usr/local), you might have to run:

sudo ldconfig

Step 6: Testing Theano with GPU

Download this python script Theano Testing with GPU save it in a file called

Test Theano in GPU mode

THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python

Optional if you want to compare GPU performanace against a regular CPU, you just need to adjust one parameter to measure the time this script takes when run on a CPU.

Test Theano in CPU mode

THEANO_FLAGS=mode=FAST_RUN,device=cpu,floatX=float32 python


ERROR (theano.sandbox.gpuarray): pygpu was configured but could not be imported

Install libgpuarray and pygpu, as per this instructions provided above for libgpuarray.

Step 7: Install Keras

sudo pip install keras  #Could it get any easier!! Thanks to developers of Keras!



  1. For learning how to use theano with GPU read this post
  2. Learn more about keras







Join the Discussion

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at

Up ↑

%d bloggers like this: