Perl and Tk on Mac (Apple Silicon)
This guide describes common issues when installing the CPAN module Tk (Perl/Tk, not plain Tcl/Tk) on macOS with Apple Silicon (arm64), and how to solve them using Homebrew and a clean toolchain.
What can go wrong
-
"C compiler cannot create executables" during
cpanm Tk/cpan Tk, often inJPEG/jpeg(bundled libjpeg). -
After that:
Error opening Makefilein the JPEG part — a follow-up error becauseconfiguredid not generate a Makefile. -
Paths: Perl/Tk first probes system
libjpeg using hardcoded paths under
/usr/local/include. On Apple Silicon, Homebrew libraries usually live under/opt/homebrew. That check fails, so Tk falls back to building the old bundled libjpeg, which often fails with strict Perl/MakeMaker compiler flags. - Architecture mismatch: perl running as x86_64 (Rosetta) while libraries are arm64 (or vice versa) causes link/runtime failures.
Prerequisites (Homebrew & compiler)
-
Xcode Command Line Tools (for
clang):xcode-select --install -
Use Homebrew for Apple Silicon (usually prefix
/opt/homebrew). -
Recommended: use Homebrew Perl for a consistent
arm64 setup:
Check:brew install perl brew install cpanminusuname -m # arm64 file "$(which perl)" # arm64
Fix: make system libjpeg visible (Homebrew)
To avoid building bundled libjpeg, make sure Homebrew
libjpeg is visible where Tk expects it (/usr/local/include
and /usr/local/lib), or adjust environment so the
built-in test can resolve -ljpeg.
Option A (often successful): install
jpeg and create symlinks in
/usr/local (one-time, requires admin rights):
brew install jpeg
sudo mkdir -p /usr/local/include /usr/local/lib
JPEG_PREFIX="$(brew --prefix jpeg)"
sudo ln -sf "$JPEG_PREFIX/include/jpeglib.h" /usr/local/include/jpeglib.h
sudo ln -sf "$JPEG_PREFIX/include/jmorecfg.h" /usr/local/include/jmorecfg.h
sudo ln -sf "$JPEG_PREFIX/include/jerror.h" /usr/local/include/jerror.h
[ -f "$JPEG_PREFIX/include/jconfig.h" ] && sudo ln -sf "$JPEG_PREFIX/include/jconfig.h" /usr/local/include/jconfig.h
sudo ln -sf "$JPEG_PREFIX/lib/libjpeg.dylib" /usr/local/lib/libjpeg.dylib
sudo ln -sf "$JPEG_PREFIX/lib/libjpeg.a" /usr/local/lib/libjpeg.a
If you see output similar to "Using system's -ljpeg", the problematic JPEG sub-build is being skipped.
Option B: clear aggressive environment flags before
building (old configure scripts are fragile):
unset CPPFLAGS LDFLAGS CFLAGS CXXFLAGS
export SDKROOT="$(xcrun --sdk macosx --show-sdk-path)"
cpanm Tk
Tcl/Tk libraries (optional)
Perl/Tk uses its own sources for core GUI parts, but Homebrew tcl-tk can still help when headers/libs are searched:
brew install tcl-tk
If needed before CPAN build (adjust paths via
brew --prefix tcl-tk):
TCL_PREFIX="$(brew --prefix tcl-tk)"
export PATH="$TCL_PREFIX/bin:$PATH"
export PKG_CONFIG_PATH="$TCL_PREFIX/lib/pkgconfig${PKG_CONFIG_PATH:+:$PKG_CONFIG_PATH}"
Install module and test
cpanm Tk
perl -MTk -e 'print "Tk ok
"'
A minimal sample (window with text and Exit button) is available in
this project as hello_world_tk.pl.
wish is
independent from the CPAN Tk module.