108

I am trying to install matplotlib in a new virtualenv.

When I do:

pip install matplotlib

or

pip install http://sourceforge.net/projects/matplotlib/files/matplotlib/matplotlib-1.1.0/matplotlib-1.1.0.tar.gz

I get this error:

building 'matplotlib._png' extension

gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -fPIC -  DPY_ARRAY_UNIQUE_SYMBOL=MPL_ARRAY_API -DPYCXX_ISO_CPP_LIB=1 -I/usr/local/include -I/usr/include -I. -I/home/sam/django-projects/datazone/local/lib/python2.7/site-packages/numpy/core/include -I. -I/usr/include/python2.7 -c src/_png.cpp -o build/temp.linux-x86_64-2.7/src/_png.o

src/_png.cpp:10:20: fatal error: png.h: No such file or directory

compilation terminated.

error: command 'gcc' failed with exit status 1

Anyone have an idea what is going on?

Any help much appreciated.

Joe Kington
  • 275,208
  • 71
  • 604
  • 463
Darwin Tech
  • 18,449
  • 38
  • 112
  • 187
  • 2
    You may also see `The following required packages can not be built: freetype, png` which is the same error in (I think) a newer version of `pip`. – LondonRob Jul 27 '15 at 17:06

12 Answers12

178

Building Matplotlib requires libpng (and freetype, as well) which isn't a python library, so pip doesn't handle installing it (or freetype).

You'll need to install something along the lines of libpng-devel and freetype-devel (or whatever the equivalent is for your OS).

See the building requirements/instructions for matplotlib.

Joe Kington
  • 275,208
  • 71
  • 604
  • 463
  • 44
    Thanks for the info! I finally got it working by installing the following dependencies in ubuntu: `libpng-dev`, `libjpeg8-dev`, `libfreetype6-dev`. – Darwin Tech Mar 26 '12 at 15:26
  • 37
    `sudo apt-get build-dep python-matplotlib` also does the trick! – william_grisaitis Feb 04 '13 at 00:59
  • 2
    It may work, but note that it wants to install quite a few packages (more than can fit in this comment box), but about 0.6 GB worth of packages. – Achal Dave Feb 15 '14 at 23:32
  • 18
    On my debian (wheezy/unstable) system it was failing even with `libfreetype6-dev` installed. It turned out that I also needed `pkg-config` installed. – Jon Mar 05 '14 at 20:25
  • I actually have libfreetype6-dev intalled on ubuntu 14.04. Matplotlib seems not to be picking it. – tutuca Aug 06 '14 at 04:19
  • Also check the versions you have installed are the ones stated in the requirements. I already had png (libpng) and freetype installed and "yum" said that they were already at the latest version. However, the version was still lower than the requirements so I had to go to the package's website and install by hand. That fixed it for me. – mirandes Sep 24 '14 at 11:58
52

To generate graph in png format you need to Install following dependent packages

sudo apt-get install libpng-dev
sudo apt-get install libfreetype6-dev

Ubuntu https://apps.ubuntu.com/cat/applications/libpng12-0/ or using following command

sudo apt-get install libpng12-0
Lava Sangeetham
  • 2,943
  • 4
  • 38
  • 54
31

As I have struggled with this issue twice (even after fresh kubuntu 15.04 install) and installing freetype did not solve anything, I investigated further.

The solution:
From github issue:

This bug only occurs if pkg-config is not installed;
a simple
sudo apt-get install pkg-config
will shore up the include paths for now.

After this installation proceeds smoothly.

redacted
  • 3,789
  • 6
  • 25
  • 38
  • Same here. pip install matplotlib gave me error: `IMPORTANT WARNING: pkg-config is not installed. matplotlib may not be able to find some of its dependencies.` Freetype shows not installed. I just did `brew install pkg-config` and was able to get by. – Lily Jan 18 '16 at 01:45
  • This is also the problem on Bash on Ubuntu on Windows Insider Preview – Jens de Bruijn Jun 11 '16 at 13:27
27

As a supplementary, on Amazon EC2, what I need to do is:

sudo yum install freetype-devel
sudo yum install libpng-devel
sudo pip install matplotlib
ciphor
  • 8,018
  • 11
  • 53
  • 70
  • 6
    It is recommended to install a package to a virtual environment, which makes `pip` so nice. Hence `sudo pip install` negates most of the usefulness that pip brings to the table. – Roman May 14 '15 at 06:48
8

On OSX I was able to get matplotlib to install via:

pip install matplotlib==1.4.0

only after I ran:

brew install freetype
gh4x
  • 818
  • 1
  • 10
  • 16
6

Under Windows this worked for me:

python -m pip install -U pip setuptools
python -m pip install matplotlib

(from https://matplotlib.org/users/installing.html)

ralfiii
  • 584
  • 4
  • 13
2
sudo apt-get install libpng-dev libjpeg8-dev libfreetype6-dev

worked for me on Ubuntu 14.04

user2436428
  • 1,653
  • 3
  • 17
  • 23
  • I needed `sudo apt-get install pkg-config` first though. – jpcgt May 10 '16 at 18:52
  • @RahulChauhan: Please post your [alternative solution](https://stackoverflow.com/review/suggested-edits/17336490) as a separate answer instead of trying to edit it into someone else's answer. – Ilmari Karonen Sep 14 '17 at 20:56
0

None of the above answers worked for me in Mint, so I did:

sudo apt-get install build-essential g++
Shapi
  • 5,493
  • 4
  • 28
  • 39
0

If on MacOSx try

xcode-select --install

This complies subprocess 32, the reason for the failure.

Coderaemon
  • 3,619
  • 6
  • 27
  • 50
0

To reduce the required packages to install you just need

apt-get install -y \
    libfreetype6-dev \
    libxft-dev && \
    pip install matplotlib

and you will get the following packages locally installed

Collecting matplotlib
  Downloading matplotlib-2.2.0-cp35-cp35m-manylinux1_x86_64.whl (12.5MB)
Collecting pytz (from matplotlib)
  Downloading pytz-2018.3-py2.py3-none-any.whl (509kB)
Collecting python-dateutil>=2.1 (from matplotlib)
  Downloading python_dateutil-2.6.1-py2.py3-none-any.whl (194kB)
Collecting pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 (from matplotlib)
  Downloading pyparsing-2.2.0-py2.py3-none-any.whl (56kB)
Requirement already satisfied: six>=1.10 in /opt/conda/envs/pytorch-py35/lib/python3.5/site-packages (from matplotlib)
Collecting cycler>=0.10 (from matplotlib)
  Downloading cycler-0.10.0-py2.py3-none-any.whl
Collecting kiwisolver>=1.0.1 (from matplotlib)
  Downloading kiwisolver-1.0.1-cp35-cp35m-manylinux1_x86_64.whl (949kB)
Requirement already satisfied: numpy>=1.7.1 in /opt/conda/envs/pytorch-py35/lib/python3.5/site-packages (from matplotlib)
Requirement already satisfied: setuptools in /opt/conda/envs/pytorch-py35/lib/python3.5/site-packages/setuptools-27.2.0-py3.5.egg (from kiwisolver>=1.0.1->matplotlib)
Installing collected packages: pytz, python-dateutil, pyparsing, cycler, kiwisolver, matplotlib
Successfully installed cycler-0.10.0 kiwisolver-1.0.1 matplotlib-2.2.0 pyparsing-2.2.0 python-dateutil-2.6.1 pytz-2018.3
loretoparisi
  • 15,724
  • 11
  • 102
  • 146
0

Another option is to install anaconda, which comes with packages such as: Matplotlib, numpy and pandas.

https://anaconda.org

0

Installing matplotlib==3.3.3 with Python 3.9 resolved the issue for me. Reference