72

I succeed in installing graphviz and cgraph with

$ sudo pip install graphviz
....
Successfully installed graphviz-0.5.1

$ sudo pip install cgraph
...
Successfully installed cgraph-0.1

I encounter the issue No package 'libcgraph' found while running sudo pip install pygraphviz. Below is the full stacktrace.

$ sudo pip install pygraphviz
The directory '/Users/sparkandshine/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/sparkandshine/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting pygraphviz
  Downloading pygraphviz-1.3.1.zip (123kB)
    100% |████████████████████████████████| 133kB 1.5MB/s 
Installing collected packages: pygraphviz
  Running setup.py install for pygraphviz ... error
    Complete output from command /usr/bin/python -u -c "import setuptools, tokenize;__file__='/private/tmp/pip-build-qfFpFG/pygraphviz/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-JmwjA6-record/install-record.txt --single-version-externally-managed --compile:
    running install
    Trying pkg-config
    Package libcgraph was not found in the pkg-config search path.
    Perhaps you should add the directory containing `libcgraph.pc'
    to the PKG_CONFIG_PATH environment variable
    No package 'libcgraph' found
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/tmp/pip-build-qfFpFG/pygraphviz/setup.py", line 87, in <module>
        tests_require=['nose>=0.10.1', 'doctest-ignore-unicode>=0.1.0',],
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/core.py", line 151, in setup
        dist.run_commands()
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 953, in run_commands
        self.run_command(cmd)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py", line 972, in run_command
        cmd_obj.run()
      File "setup_commands.py", line 44, in modified_run
        self.include_path, self.library_path = get_graphviz_dirs()
      File "setup_extra.py", line 121, in get_graphviz_dirs
        include_dirs, library_dirs = _pkg_config()
      File "setup_extra.py", line 44, in _pkg_config
        output = S.check_output(['pkg-config', '--libs-only-L', 'libcgraph'])
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 573, in check_output
        raise CalledProcessError(retcode, cmd, output=output)
    subprocess.CalledProcessError: Command '['pkg-config', '--libs-only-L', 'libcgraph']' returned non-zero exit status 1

    ----------------------------------------
Command "/usr/bin/python -u -c "import setuptools, tokenize;__file__='/private/tmp/pip-build-qfFpFG/pygraphviz/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-JmwjA6-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/tmp/pip-build-qfFpFG/pygraphviz/

I tried the solutions provided by Python does not see pygraphviz, but it doesn't work.

Community
  • 1
  • 1
SparkAndShine
  • 14,337
  • 17
  • 76
  • 119

6 Answers6

118
sudo apt-get install python-dev graphviz libgraphviz-dev pkg-config

or the following might be needed if you're using Python 3:

sudo apt-get install python3-dev graphviz libgraphviz-dev pkg-config

then

pip install pygraphviz
Dhiraj Dhule
  • 1,303
  • 1
  • 8
  • 6
  • 1
    I found I needed to first do `sudo apt-get install python-dev`, otherwise I'd get a compile error about a missing `Python.h` when doing the `pip install pygraphviz`. – Brian Stormont Jun 07 '17 at 18:19
  • 1
    I have updated the python-dev requirement in case somebody has similar problem. Thanks. – Dhiraj Dhule Jun 09 '17 at 10:00
  • In the first step, you can specify the python version you want: e.g. `sudo apt-get install python3.6-dev`. The default is 2.7. Also, for the second step using `sudo` gave me a pip import error, but dropping it fixed the issue and pygraphviz installed. – r3robertson Feb 18 '19 at 17:43
  • Just a quick note. It may not be a good idea to run "sudo pip ...". It's best to keep pip as part of a virtual environment but if not, then just run pip as your regular user. See https://stackoverflow.com/questions/21055859/what-are-the-risks-of-running-sudo-pip – Harlin Feb 21 '20 at 01:27
  • `graphviz` itself doesn’t seem to be necessary. – Skippy le Grand Gourou Jan 20 '21 at 14:44
  • @myself Well, [it actually is](https://stackoverflow.com/a/13987392/812102), despite the absence of explicit dependency. – Skippy le Grand Gourou Jan 20 '21 at 15:04
46

For MacOS, I needed to do:

$ brew install graphviz
$ sudo pip install pygraphviz 
David Dehghan
  • 14,410
  • 2
  • 84
  • 85
Rob
  • 751
  • 6
  • 8
  • 4
    I was using an virtualenv and Python 3 and I was able to do `pip install graphviz` without any additional options and it worked fine (after `brew install graphviz`). – Trenton Jun 20 '17 at 16:04
  • Oh maann.. Thankx a lot.. This works perfectly for me. You saved my day and my ass being kicked off. :) – Jyotirmay Jul 06 '17 at 11:45
  • As per @Trenton's note--I found all requirements satisfied on `pip install graphviz` after I had run `brew install graphviz` note the brew install pauses a long time on font stuff. – Rob Feb 24 '18 at 23:30
13

For fedora users:

  1. yum list available graphviz*
  2. select the appropriate package for your Fedora distribution
  3. sudo yum install 'graphviz-devel.x86_64'
  4. pip install pygraphviz
Davide Aversa
  • 4,368
  • 6
  • 25
  • 36
Edouard Amosse
  • 143
  • 1
  • 8
6

On macOS, I made it with,

$ brew install graphviz
$ sudo pip install pygraphviz
SparkAndShine
  • 14,337
  • 17
  • 76
  • 119
  • Didn't work for me. After installing graphviz, I had to download the git project of pygraphviz and use `sudo python3 setup.py install --include-path=/usr/local/Cellar/graphviz/2.40.1/include/graphviz --library-path=/usr/local/Cellar/graphviz/2.40.1/lib` – Itay Jul 04 '18 at 12:48
2

For CentOS. As graphviz-devel is required to fix this issue, you will need to install it with yum. To do this you are required to add repo's where package is available. In my case Atomic repos worked fine.

wget -q -O - http://www.atomicorp.com/installers/atomic | sh

This will add atomic repos, then

sudo yum install graphviz-devel

And you should be able to perform

pip install pygraphviz
wolendranh
  • 3,731
  • 1
  • 25
  • 37
0

I ran into this problem when creating a Dockerfile for Django with a python-alpine image.

I was able to solve it thanks to this post adding the package "graphviz-dev" along with the rest of my app's dependencies.

Example:

#Install dependencies
RUN apk add --update --no-cache --virtual .build-deps \
    build-base \
    alpine-sdk \
    postgresql-dev \
    libffi-dev \
    python3-dev \
    libffi-dev \
    jpeg-dev \
    zlib-dev \
    musl-dev \
    libpq \
    graphviz-dev \  
    && pip install --no-cache-dir -r /code/requirements_dev.txt \
    && find /usr/local \
        \( -type d -a -name test -o -name tests \) \
        -o \( -type f -a -name '*.pyc' -o -name '*.pyo' \) \
        -exec rm -rf '{}' +

Regards