Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using matplotlib on headless Ubuntu 14.04 Server

I have a headless Ubuntu 14.04 Server that I connect to remotely using SSH. I want to use matplotlib and have plots appear at the ssh client. For example, I would connect using:

ssh -X [email protected]

And then from a Python console, I want this to produce a plot in a window:

import matplotlib.pyplot as plt
plt.plot(range(10))
plt.show()

I have installed matplotlib in my virtualenv, and I ran sudo apt-get install python-gtk2, but the plot still doesn't appear. I assume I'm missing lots of packages. What is a fairly minimal set of X-related packages I could install to make this work? I do NOT want to install ubuntu-desktop.

like image 850
Scott Avatar asked Nov 07 '14 21:11

Scott


1 Answers

I got it working on Ubuntu 14.04.1 Server, but it was painful! The tricky part is definitely virtualenv. I finally had luck using the Qt4 backend, which I was only able to install via the Ubuntu package and then had to symlink it into my virtualenv. So here's the step-by-step process...

First install the pre-reqs and hack PyQt4 into your virtualenv:

$ sudo apt-get install xauth x11-apps python-qt4 
$ ln -s /usr/lib/python2.7/dist-packages/PyQt4 /path/to/myvenv/lib/python2.7/PyQt4

Now manually download and install SIP (http://www.riverbankcomputing.com/software/sip/intro) with your venv activated, as follows:

$ tar xzf sip-4.16.4.tar.gz
$ cd sip-4.16.4
$ python configure.py
$ make
$ sudo make install

Next, download matplotlib source tarball and modify the setup configuration to force it to install Qt4 backend:

$ tar xzf matplotlib-1.4.2.tar.gz
$ cp matplotlib-1.4.2/setup.cfg.template matplotlib-1.4.2/setup.cfg

Now edit setup.cfg near line 68 to read:

qt4agg = True 

Matplotlib will now install cleanly in your venv:

$ pip install -e matplotlib-1.4.2/

Now you can SSH using the -X flag and plots will load remotely!

like image 166
Scott Avatar answered Oct 08 '22 10:10

Scott