1

I have already installed numpy successfully and I'm now trying to install matplotlib. I am following the steps found on the following website:

http://matplotlib.org/faq/installing_faq.html#source-install-from-git

The error occurs after the following command:

python setup.py install

Here is the error:

Checking .pth file support in /usr/local/lib/python2.7/dist-packages/
error: can't create or remove files in install directory                                     '

The following error occurred while trying to add or remove files in the installation directory:

    [Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/test-easy-install-24752.pth'

The installation directory you specified (via --install-dir, --prefix, or the distutils default setting) was:

    /usr/local/lib/python2.7/dist-packages/

I am working as the administrator on my machine and (I think) have all the rights to read/write files:

administrator@ubuntu:/usr/local/lib/python2.7/dist-packages$ ll -a
total 8
drwxrwsr-x 2 root staff 4096 Aug 20  2013 ./
drwxrwsr-x 4 root staff 4096 Mar  9 11:04 ../

What is the problem and how do I fix it?

solalito
  • 1,189
  • 4
  • 19
  • 34
  • 1
    You can try to work in a virtualenv (http://virtualenv.readthedocs.org/en/latest/) – daouzli May 13 '14 at 11:29
  • 2
    @daouzli Installing `matplotlib` in a `virtualenv` is rather tricky because of the backend dependencies, which usually need to be installed system-wide. Since the OP is using Ubuntu, probably the easiest way to install `matplotlib` is simply `$ sudo apt-get install python-matplotlib`. If it's absolutely necessary for the OP to build it from source in a `virtualenv`, then [this guide](http://www.stevenmaude.co.uk/2013/09/installing-matplotlib-in-virtualenv.html) may be helpful. – ali_m May 13 '14 at 11:34
  • @ali_m That `sudo` command actually did the trick thanks. But any idea why it could not create files even though I had all the sufficient permissions? – solalito May 13 '14 at 11:39
  • 1
    Check the permissions on `/usr/local/lib/python2.7/dist-packages/` - it's almost certainly only writeable by `root`. Being logged in as an 'administrator' usually means that you are on the `sudoers` list but not that your normal login shell can write to system files (and that's a very good thing!). In general I would strongly recommend against `sudo` installing Python packages, either using `pip`, `easy_install` or `setup.py`. You can get into a real mess because other package managers such as `apt-get` have no awareness of any changes made in this way. – ali_m May 13 '14 at 11:52
  • @ali_m Please post that as the answer. I tend to use a hybrid system to install mpl, the gui-toolkit stuff in installed system-wide (as are BLAS an company) and I install mpl, np, scipy, ipython,... in the venv (using system packages). – tacaswell May 13 '14 at 12:19

1 Answers1

3

I am working as the administrator on my machine and (I think) have all the rights to read/write files

This is really a discussion more suited to SuperUser, but the problem is that being an 'administrator' doesn't really mean what you think it does. In this line:

drwxrwsr-x 2 root staff 4096 Aug 20  2013 ./

root is the owner of the directory, and the final r-x means that other users are only allowed to read and execute, not write to this directory (see here for more details).

What can you do?

  1. Run the install command as root, e.g. $ sudo python setup.py install. This is NOT RECOMMENDED! The problem with installing system-wide Python modules in this way is that other package managers (e.g. apt-get) are totally oblivious to any changes made in this way, which tends to lead to a mess of broken dependencies for other system packages.

  2. Install the package from the Ubuntu repositories, i.e. $ sudo apt-get install python-matplotlib. This is probably the easiest way to install matplotlib, and is very unlikely to break any dependencies. However, the Ubuntu repositories tend to contain somewhat older versions of most Python packages.

  3. Install it into a virtualenv. This is safe, in that it doesn't affect anything in your global site-packages, and it allows you to install the very latest bleeding-edge versions. However, installing matplotlib into a virtualenv can be tricky, since matplotlib has lots of backend dependencies that generally need to be installed system-wide. To work around this you could either:

    a) Create your virtualenv with the --system-site-packages flag. With this option, if Python tries to import a module that isn't installed locally in your virutalenv's site-packages directory, it will then look in the system-wide site-packages. This means that you can install matplotlib locally in your virtualenv and it will find all of its backend dependencies in the system-wide site-packages. The downside is that if you have the same module installed both locally and system-wide, you have to be a bit more careful about version is actually being imported.

    b) Create your virtualenv with the --no-site-packages flag, then make symbolic links to the required modules in your system-wide site-packages directory. This blog article gives a good set of instructions for installing matplotlib inside a virtualenv using symlinks. This method is trickier, but the advantage is that your virtualenv is (almost!) completely self-contained, and there can be no doubt about where your module imports are coming from.

Community
  • 1
  • 1
ali_m
  • 71,714
  • 23
  • 223
  • 298
  • great answer. I will probably use that symlink trick sometime in the near future. I wonder how hard it would be to hack venv to give it a list of packages to use.... – tacaswell May 13 '14 at 14:08
  • you mean to automatically generate symlinks to specific directories in system site-packages? with virtualenvwrapper you could probably shove something in a [`post_mkvirtualenv`](http://virtualenvwrapper.readthedocs.org/en/latest/plugins.html#plugins-post-mkvirtualenv) script provided you knew exactly what needed to be linked. – ali_m May 13 '14 at 14:19
  • yes, I think exactly that. The main use I would want is to able to use the system installs of pyside/pyqt/pygtk for putting together the scientific python stack in a virtual enviroment. – tacaswell May 13 '14 at 14:25
  • I also often need to do this sort of thing, but so far I haven't bothered putting a script together. [this](http://stackoverflow.com/questions/1961997/is-it-possible-to-add-pyqt4-pyside-packages-on-a-virtualenv-sandbox) might be useful for the pyqt side of things. – ali_m May 13 '14 at 14:30