How to run X server using xserver-xorg-video-dummy driver on Ubuntu

When you need to have a dummy display on a headless server (i.e. without any physical monitor attached), first install the dummy driver package:

sudo apt install xserver-xorg-video-dummy

After that, save this config file as dummy-1920x1080.conf

Section "Monitor"
  Identifier "Monitor0"
  HorizSync 28.0-80.0
  VertRefresh 48.0-75.0
  # 1920x1080 @ 60.00 Hz (GTF) hsync: 67.08 kHz; pclk: 172.80 MHz
  Modeline "1920x1080_60.00" 172.80 1920 2040 2248 2576 1080 1081 1084 1118 -HSync +Vsync

Section "Device"
  Identifier "Card0"
  Driver "dummy"
  VideoRam 256000

Section "Screen"
  DefaultDepth 24
  Identifier "Screen0"
  Device "Card0"
  Monitor "Monitor0"
  SubSection "Display"
    Depth 24
    Modes "1920x1080_60.00"

Now you can start

sudo X -config dummy-1920x1080.conf

The output will look like this:

X.Org X Server 1.19.6
Release Date: 2017-12-20
X Protocol Version 11, Revision 0
Build Operating System: Linux 4.4.0-138-generic x86_64 Ubuntu
Current Operating System: Linux ubuntu-s-1vcpu-2gb-fra1-01 4.15.0-45-generic #48-Ubuntu SMP Tue Jan 29 16:28:13 UTC 2019 x86_64
Kernel command line: BOOT_IMAGE=/boot/vmlinuz-4.15.0-45-generic root=LABEL=cloudimg-rootfs ro console=tty1 console=ttyS0
Build Date: 25 October 2018  04:11:27PM
xorg-server 2:1.19.6-1ubuntu4.2 (For technical support please see 
Current version of pixman: 0.34.0
        Before reporting problems, check
        to make sure that you have the latest version.
Markers: (--) probed, (**) from config file, (==) default setting,
        (++) from command line, (!!) notice, (II) informational,
        (WW) warning, (EE) error, (NI) not implemented, (??) unknown.
(==) Log file: "/var/log/Xorg.0.log", Time: Sat Feb 23 17:48:07 2019
(++) Using config file: "dummy-1920x1080.conf"
(==) Using system config directory "/usr/share/X11/xorg.conf.d"

You need to keep this process running as long as you need the display.

Identifying the display number

If no other X server is running, this will, by default, use display number 0. Look for this line to identify the display in use:

(==) Log file: "/var/log/Xorg.0.log", Time: Sat Feb 23 17:48:07 2019

In said line, Xorg.0.log tells you that display 0 is in use, whereas Xorg.1.log tells you that display 1 is in use.

Starting your software

Now you can start your software that needs a graphical interface as follows (we use firefox, and display number 0, as an example):

DISPLAY=:0 firefox

Don’t forget the colon in DISPLAY=:0!

Starting on a specific display number

If you want to start the X server on a specific display number, e.g. 7, because some other dummy server is running concurrently, use this command to start the X server:

sudo X :7 -config dummy-1920x1080.conf

Of course you’ll need to use DISPLAY=:7 firefox in order to start firefox using this config!

In case there is already an X server running on that display, you’ll see an error message like this:

Fatal server error:
(EE) Server is already active for display 7
        If this server is no longer running, remove /tmp/.X7-lock
        and start again.
Please consult the The X.Org Foundation support 
 for help. 

Look for Server is already active for display 7 to be sure you’re seeing the same error message!