1. 引言
Graphviz 是一个开源绘图工具,很多软件都基于graphviz,例如:torchviz中的动态计算图绘制就依赖它。缺少graphviz时执行make_dot函数会报如下错误:
graphviz.backend.ExecutableNotFound: failed to execute ['dot', '-Tpdf', '-O', 'Digraph.gv'], make sure the Graphviz executables are on your systems' PATH
2. 通过homebrew安装
关于mac下如何安装graphviz,网上很多教程都是用homebrew包管理器来安装:
brew install graphviz
但实际执行时,却会遇到关于SSL、mac版本的各种环境问题,尝试多次均无法成功。
Running `brew update --auto-update`...
Error: Failed to download https://formulae.brew.sh/api/formula.jws.json!
==> Downloading https://formulae.brew.sh/api/formula.jws.json
### 1.8%
curl: (28) Operation too slow. Less than 100 bytes/sec transferred the last 5 seconds
Warning: formula.jws.json: update failed, falling back to cached version.
==> Downloading https://formulae.brew.sh/api/formula.jws.json
curl: (35) LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to formulae.brew.sh:443
Error: Failure while executing; `/usr/bin/env /usr/local/Homebrew/Library/Homebrew/shims/shared/curl --disable --cookie /dev/null --globoff --user-agent Homebrew/4.1.19\ \(Macintosh\;\ Intel\ Mac\ OS\ X\ 11.6.7\)\ curl/7.64.1 --header Accept-Language:\ en --fail --progress-bar --remote-time --output /Users/a200007/Library/Caches/Homebrew/api/formula.jws.json --location --disable --cookie /dev/null --globoff --show-error --user-agent Homebrew/4.1.19\ \(Macintosh\;\ Intel\ Mac\ OS\ X\ 11.6.7\)\ curl/7.64.1 --header Accept-Language:\ en --fail --progress-bar --compressed --speed-limit 100 --speed-time 5 https://formulae.brew.sh/api/formula.jws.json` exited with 35.
curl: (35) LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to formulae.brew.sh:443
==> Downloading https://formulae.brew.sh/api/formula.jws.json
###### 3.0%
curl: (28) Operation too slow. Less than 100 bytes/sec transferred the last 5 seconds
Warning: formula.jws.json: update failed, falling back to cached version.
==> Downloading https://formulae.brew.sh/api/formula.jws.json
################################################################################################################################################################################################################### 100.0%
Warning: You are using macOS 11.
We (and Apple) do not provide support for this old version.
It is expected behaviour that some formulae will fail to build in this old version.
It is expected behaviour that Homebrew will be buggy and slow.
Do not create any issues about this on Homebrew's GitHub repositories.
Do not create any issues even if you think this message is unrelated.
Any opened issues will be immediately closed without response.
Do not ask for help from Homebrew or its maintainers on social media.
You may ask for help in Homebrew's discussions but are unlikely to receive a response.
Try to figure out the problem yourself and submit a fix as a pull request.
We will review it but may or may not accept it.
==> Fetching libpng
==> Downloading https://raw.githubusercontent.com/Homebrew/homebrew-core/138f7b1d12de97bd1ace6c345d7676306aed1ee1/Formula/lib/libpng.rb
curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused
Error: graphviz: Failed to download resource "libpng.rb"
Download failed: https://raw.githubusercontent.com/Homebrew/homebrew-core/138f7b1d12de97bd1ace6c345d7676306aed1ee1/Formula/lib/libpng.rb
3. 源代码安装
鉴于通过brew始终无法安装成功,于是就寻找其它安装方式,访问官网发现还有源码安装方式。
先下载最新版本源码包:
下载完成后解压并进入源码目录:
依次执行三个命令:
./configure
checking build system type... x86_64-apple-darwin20.6.0
checking host system type... x86_64-apple-darwin20.6.0
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... config/install-sh -c -d
……
----------------------------------------------------------------
graphviz-12.0.0 will be compiled with the following:
options:
cgraph: Yes (always enabled)
digcola: Yes
expat: Yes
freetype: Yes
……
make
printf '#pragma once\n' > ./graphviz_version.h
/usr/bin/grep -E 'PACKAGE|VERSION|GVPLUGIN' config.h >> ./graphviz_version.h
/Applications/Xcode.app/Contents/Developer/usr/bin/make all-recursive
Making all in libltdl
……
Making all in vuln
Making all in input
make[5]: Nothing to be done for `all'.
Making all in reference
make[5]: Nothing to be done for `all'.
make[5]: Nothing to be done for `all-am'.
make[4]: Nothing to be done for `all-am'.
make[3]: Nothing to be done for `all-am'.
make[2]: Nothing to be done for `all-am'.
make install
/Applications/Xcode.app/Contents/Developer/usr/bin/make install-recursive
Making install in libltdl
/Applications/Xcode.app/Contents/Developer/usr/bin/make install-am
Making install in lib
Making install in cdt
……
config/install-sh -c -d '/usr/local/share/doc/graphviz'
/usr/bin/install -c -m 644 AUTHORS COPYING CHANGELOG.md NEWS cpl1.0.txt '/usr/local/share/doc/graphviz'
config/install-sh -c -d '/usr/local/share/man/man7'
/usr/bin/install -c -m 644 graphviz.7 '/usr/local/share/man/man7'
config/install-sh -c -d '/usr/local/include/graphviz'
/usr/bin/install -c -m 644 ./graphviz_version.h '/usr/local/include/graphviz'
测试安装:dot -version
dot - graphviz version 12.0.0 (20240704.0754)
libdir = "/usr/local/lib/graphviz"
Activated plugin library: libgvplugin_dot_layout.6.dylib
Using layout: dot:dot_layout
Activated plugin library: libgvplugin_core.6.dylib
Using render: dot:core
Using device: dot:dot:core
The plugin configuration file:
/usr/local/lib/graphviz/config6
was successfully loaded.
render : dot dot_json fig json json0 map pic pov ps svg svg_inline tk xdot xdot_json
layout : circo dot fdp neato nop nop1 nop2 osage patchwork sfdp twopi
textlayout :
device : canon cmap cmapx cmapx_np dot dot_json eps fig gv imap imap_np ismap json json0 pic plain plain-ext pov ps ps2 svg svg_inline svgz tk xdot xdot1.2 xdot1.4 xdot_json
loadimage : (lib) eps gif jpe jpeg jpg png ps svg
输出dot的版本信息表示安装成功。
此时再执行make_dot函数,就能输出计算图:
参考资料:
- 二进制安装:https://graphviz.org/download/
- 源码安装:https://graphviz.org/download/source/