Ehrweb.de

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

Prerequisites (Homebrew & compiler)

  1. Xcode Command Line Tools (for clang):
    xcode-select --install
  2. Use Homebrew for Apple Silicon (usually prefix /opt/homebrew).
  3. Recommended: use Homebrew Perl for a consistent arm64 setup:
    brew install perl
                brew install cpanminus
    Check:
    uname -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.

Note: This page is about Perl/Tk. A pure Tcl/Tk script run with wish is independent from the CPAN Tk module.