XQuartz on macOS

Motivation

Running X11 on macOS is possible via XQuartz. Examples of applications that use the X protocol include Xfig for drawing vector graphics and Xfce for running a Desktop User Interface. This post covers installing XQuartz on macOS 14 (Sonoma) and testing it by running Xfig.

Install

First, install XQuartz on macOS using Homebrew:

brew install xquartz

Next, run XQuartz (if it works, it will hang; if it fails, the command will exit immediately):

xquartz
echo $?

The expected output of the above command echo $? is to print 1, which indicates a non-zero status code (error). To fix the error, read the system logs to find out what went wrong.

Reading XQuartz Error Logs

Open the Console app in macOS i.e. open Console. Then navigate to the Log Reports section and select the most recent entry org.xquartz.log. Error message looks similar to:

X11.app: DISPLAY does not look like a launchd set variable, unsetting.
X11.app: main(): argc=2
	argv[0] = /Applications/Utilities/XQuartz.app/Contents/MacOS/X11.bin
	argv[1] = --listenonly
Waiting for startup parameters via Mach IPC.
X11.app: No launchd socket handed off, unsetting DISPLAY
X11.app: do_start_x11_server(): argc=1
	argv[0] = xquartz
[ 25024.976] Xquartz starting:
[ 25024.976] X.Org X Server 21.1.6
[ 25024.976] _XSERVTransmkdir: ERROR: euid != 0,directory /tmp/.X11-unix will not be created.
[ 25024.976] _XSERVTransSocketUNIXCreateListener: mkdir(/tmp/.X11-unix) failed, errno = 2
[ 25024.976] _XSERVTransMakeAllCOTSServerListeners: failed to create listener for local
[ 25024.976] (EE) 
Fatal server error:
[ 25024.976] (EE) Cannot establish any listening sockets - Make sure an X server isn't already running(EE) 
[ 25024.976] (EE) Server terminated with error (1). Closing log file.

The key part is that the directory /tmp/.X11-unix does not exist and failed to be created.

Solution

To fix the error, create the directory, give ownership to root, and set the DISPLAY env variable as done below:

mkdir -p /tmp/.X11-unix
sudo chown root /tmp/.X11-unix/
launchctl setenv DISPLAY /tmp/.X11-unix/X0:0

Although not required, the following command may also be useful:

defaults write org.macosforge.xquartz.X11 dpi -int 110

Run XQuartz

Finally, verify XQuarts is working by running it:

xquartz

Note the command should not return (unless you send CTRL-C or CTRL-\ to end it). Otherwise, go back to the error logs step and keep iterating to resolve the error messages.

To demonstrate that XQuartz is working, in another terminal run:

brew install xfig
xfig

Like xquartz, the xfig command should not return if it runs successfully, and it will open up a GUI using the X11 protocol (via XQuartz).

Note: The /tmp/.X11-unix directory will not be persisted across reboots and, therefore, needs to be re-created as described above after each boot.

References