103

I am using Python 2.7, If i try to install Matplotlib I am getting this error if i use "pip install matplotlib"

 Exception:
  Traceback (most recent call last):
    File "/usr/local/lib/python2.7/dist-packages/pip/basecommand.py", line 232, in main
      status = self.run(options, args)
    File "/usr/local/lib/python2.7/dist-packages/pip/commands/install.py", line 339, in run
      requirement_set.prepare_files(finder)
    File "/usr/local/lib/python2.7/dist-packages/pip/req/req_set.py", line 355, in prepare_files
      do_download, session=self.session,
    File "/usr/local/lib/python2.7/dist-packages/pip/download.py", line 782, in unpack_url
      session,
    File "/usr/local/lib/python2.7/dist-packages/pip/download.py", line 667, in unpack_http_url
      from_path, content_type = _download_http_url(link, session, temp_dir)
    File "/usr/local/lib/python2.7/dist-packages/pip/download.py", line 843, in _download_http_url
      _download_url(resp, link, content_file)
    File "/usr/local/lib/python2.7/dist-packages/pip/download.py", line 615, in _download_url
      for chunk in progress_indicator(resp_read(4096), 4096):
    File "/usr/local/lib/python2.7/dist-packages/pip/utils/ui.py", line 46, in iter
      for x in it:
    File "/usr/local/lib/python2.7/dist-packages/pip/download.py", line 580, in resp_read
      decode_content=False):
    File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/response.py", line 256, in stream
      data = self.read(amt=amt, decode_content=decode_content)
    File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/response.py", line 186, in read
      data = self._fp.read(amt)
    File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/cachecontrol/filewrapper.py", line 54, in read
      self.__callback(self.__buf.getvalue())
    File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/cachecontrol/controller.py", line 205, in cache_response
      self.serializer.dumps(request, response, body=body),
    File "/usr/local/lib/python2.7/dist-packages/pip/_vendor/cachecontrol/serialize.py", line 81, in dumps
      ).encode("utf8"),
  MemoryError"

What might the problem be? I am using raspberry Pi 2 with a 16gb SD card. I still have 8gb data free but still getting this error. Kindly help

Ned Deily
  • 83,389
  • 16
  • 128
  • 151
nishanth anand
  • 1,031
  • 2
  • 8
  • 3

3 Answers3

324

This error is coming up because, it seems, pip's caching mechanism is trying to read the entire file into memory before caching it… which poses a problem in a limited-memory environment, as matplotlib is ~50mb.

A simpler solution, until pip is patched to use a constant-space caching algorithm, is to run pip with --no-cache-dir to avoid the cache:

$ pip --no-cache-dir install matplotlib
lashgar
  • 5,184
  • 3
  • 37
  • 45
David Wolever
  • 148,955
  • 89
  • 346
  • 502
37

It seems that you have insufficient RAM to build matplotlib from scratch. To overcome that, either turn on swap:

# create swap file of 512 MB
dd if=/dev/zero of=/swapfile bs=1024 count=524288
# modify permissions
chown root:root /swapfile
chmod 0600 /swapfile
# setup swap area
mkswap /swapfile
# turn swap on
swapon /swapfile

Or, if you have raspbian installed on your SD card, you can install matplotlib from the repository:

apt-get install python-matplotlib
miracula
  • 527
  • 7
  • 10
Andrey Sobolev
  • 12,353
  • 3
  • 48
  • 52
  • Hi,i tried swapping but still I am getting the same error. – nishanth anand Apr 06 '15 at 08:28
  • if i give "sudo apt-get install matplotlib" it says "E: Unable to locate package matplotlib" what should be done? I am new to linux so pls help – nishanth anand Apr 06 '15 at 08:29
  • I don't remember the exact name of the package (and have my RPi at home), but you can find it out by issuing `sudo apt-cache search matplotlib`. This should give you the exact name. – Andrey Sobolev Apr 07 '15 at 06:10
  • Have you tried searching apt-cache for matplotlib? It seems that the right name for the package is `python-matpolotlib`. – Andrey Sobolev Apr 14 '15 at 06:42
  • Hi, the swapfile method worked for me with a Pi 2, and a standard 8Gb SD. The SD size shouldn't make any difference to the procedure, since only a little is used for swap. Just a detail, I `sudo` ed all the swap creation and manipulation commands in Andrey's answer. I'm using a virtual environment as this guide: (http://www.pyimagesearch.com/2015/02/23/install-opencv-and-python-on-your-raspberry-pi-2-and-b/) which has proved really helpful. – Hugh Barnard May 22 '15 at 08:12
  • I too had a memoryerror when installing matplotlib, only to a beaglebone black instead of a Pi. The original instructions didn't help until I changed the block count from count=524288 to count=1524288. After that all went OK :) – Erik Knowles Jun 24 '15 at 20:03
7

--no-cache-dir didn't work for me. I just closed all apps and only then I was able to finish installation.

MrKsn
  • 1,185
  • 1
  • 16
  • 27