How to solve the error with DISPLAY env when installing gitk on Ubuntu 20.04

Problem statement:

When installing gitk on Ubuntu 20.04 by:
sudo apt install gitk
there is an error with launcing the program.

Error message: Invalid MIT-MAGIC-COOKIE-1 keyError: cannot open display ':0'

Solution:

To solve this issue, you need to change your default DISPLAY environment variable value from:

DISPLAY=desktop:0

to the following:

DISPLAY=:1.0

Don't use export command, as it will take effect only for the current bash session and it will be terminated after you'll exit the shell.

To persist the change to all bash sessions and the current user add the following entry to your /etc/environment file: DISPLAY=:1.0

Explanation:

Gitk is a graphical repository browser. It was the first of its kind. It can be thought of as a GUI wrapper for git log. It is useful for exploring and visualizing the history of a repository. It’s written in tcl/tk which makes it portable across operating systems. gitk is maintained by Paul Mackerras as an independent project, separate from Git core. Stable versions are distributed as part of the Git suite for the convenience of end users. Gitk can be a helpful learning aid for newcomers to Git. [Source]

[Source]: The magic word in the X window system is DISPLAY. A display consists (simplified) of:

  • a keyboard,
  • a mouse
  • and a screen.

A display is managed by a server program, known as an X server. The server serves displaying capabilities to other programs that connect to it.

The remote server knows where it has to redirect the X network traffic via the definition of the DISPLAY environment variable which generally points to an X Display server located on your local computer.

The value of the display environment variable is:
hostname:D.S

where:

hostname is the name of the computer where the X server runs. An omitted hostname means the localhost.

D is a sequence number (usually 0). It can be varied if there are multiple displays connected to one computer.

S is the screen number. A display can actually have multiple screens. Usually, there's only one screen though where 0 is the default.

Example of values: localhost:4 google.com:0 :0.0

hostname:D.S means screen S on display D of host hostname; the X server for this display is listening at TCP port 6000+D.

:0.0 means that we are talking about the first screen attached to your first display in your local host

Resources

  1. Gitk explained
  2. Use gitk to understand git