I am working in a opencv project on the raspberry pi, and im using a virtual environment, but i used pip to install numpy, scipy and other libraries, but when trying to install matplotlib on pip by:

pip install matplotlib

it throws an error:

Exception: Traceback (most recent call last): File "/home/pi/.virtualenvs/cv/local/lib/python2.7/site-packages/pip/basecommand.py", line 223, in main status = self.run(options, args) File "/home/pi/.virtualenvs/cv/local/lib/python2.7/site-packages/pip/commands/install.py", line 282, in run requirement_set.prepare_files(finder) File "/home/pi/.virtualenvs/cv/local/lib/python2.7/site-packages/pip/req/req_set.py", line 334, in prepare_files functools.partial(self._prepare_file, finder)) File "/home/pi/.virtualenvs/cv/local/lib/python2.7/site-packages/pip/req/req_set.py", line 321, in _walk_req_to_install more_reqs = handler(req_to_install) File "/home/pi/.virtualenvs/cv/local/lib/python2.7/site-packages/pip/req/req_set.py", line 491, in _prepare_file session=self.session) File "/home/pi/.virtualenvs/cv/local/lib/python2.7/site-packages/pip/download.py", line 825, in unpack_url session, File "/home/pi/.virtualenvs/cv/local/lib/python2.7/site-packages/pip/download.py", line 673, in unpack_http_url from_path, content_type = _download_http_url(link, session, temp_dir) File "/home/pi/.virtualenvs/cv/local/lib/python2.7/site-packages/pip/download.py", line 886, in _download_http_url _download_url(resp, link, content_file) File "/home/pi/.virtualenvs/cv/local/lib/python2.7/site-packages/pip/download.py", line 621, in _download_url for chunk in progress_indicator(resp_read(4096), 4096): File "/home/pi/.virtualenvs/cv/local/lib/python2.7/site-packages/pip/utils/ui.py", line 133, in iter for x in it: File "/home/pi/.virtualenvs/cv/local/lib/python2.7/site-packages/pip/download.py", line 586, in resp_read decode_content=False): File "/home/pi/.virtualenvs/cv/local/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/response.py", line 307, in stream data = self.read(amt=amt, decode_content=decode_content) File "/home/pi/.virtualenvs/cv/local/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/response.py", line 243, in read data = self._fp.read(amt) File "/home/pi/.virtualenvs/cv/local/lib/python2.7/site-packages/pip/_vendor/cachecontrol/filewrapper.py", line 54, in read self.__callback(self.__buf.getvalue()) File "/home/pi/.virtualenvs/cv/local/lib/python2.7/site-packages/pip/_vendor/cachecontrol/controller.py", line 224, in cache_response self.serializer.dumps(request, response, body=body), File "/home/pi/.virtualenvs/cv/local/lib/python2.7/site-packages/pip/_vendor/cachecontrol/serialize.py", line 81, in dumps ).encode("utf8"), MemoryError

So for now i cant install matplotlib. How can i avoid the error?

2 Answers 2


I researched a little more an I solved it by using

pip install --no-cache-dir matplotlib


Like @jacob suggested pip install --no-cache-dir matplotlib helped to avoid the out of memory error.

However, matplotlib did not build because 'libpng' is not available on the raspberry and cannot be fetched by pip. In this case sudo apt-get install python-matplotlib worked just fine.

  • I would also like to add that using apt-get worked better for me. I installed matplotlib using pip sudo pip install matplotlib and I was getting some Gdk errors so I unistalled with pip sudo pip uninstall matplotlib, then reinstalled with apt-get sudo apt-get install matplotlib and it worked just fine.
    – Seven
    Nov 30, 2016 at 2:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.