"Could not open display `(null)'" error. How can I fix this?

Solution 1:

Errors like this mean that you are running a program that needs a graphical display and it can't find one. GUI programs connect to the display defined by the $DISPLAY environmental variable. The general format of the error is

Could not open display $DISPLAY

Since, in your case, the error says (null), this means that $DISPLAY is not set. You therefore need to:

  1. If you are logging in to a remote machine using something like ssh, you will need to export the $DISPLAY of your local machine and tell the remote computer to display GUI programs there. This can be done with the -X or -Y options of ssh:

    ssh -Y [email protected]
    

    As explained in man ssh:

     -Y  Enables trusted X11 forwarding.  Trusted X11 forwardings are not
         subjected to the X11 SECURITY extension controls.
     -X  Enables X11 forwarding.  This can also be specified on a per-host
         basis in a configuration file.
    
  2. If this is your local machine, you need to install a graphical environment. If one is already installed, assuming a default Ubuntu setup, you can start it with this command:

    sudo service lightdm start
    
  3. If you have a graphical environment running but for whatever reason, $DISPLAY is set to null, you can redefine it. The details will depend on your actual situation but in most cases, what you will need (assuming, again, you are on your local machine) is

    export DISPLAY=:0.0
    

    You can then run your GUI program normally.

  4. If you do have an X server running but have switched to a tty (for example by pressing Ctrl+Alt+F1), you might simply need to return to your graphical environment. This depends on which virtual console your GUI is running but in most cases on Ubuntu that will be 7, so you can get back to it using Alt+F7.

    If that does not bring you back to your desktop, just cycle through all ttys Alt+Left Arrow or Alt+Right Arrow until you find the right one.

  5. Another common problem is that you have started an X session as your normal user and are now trying to connect to it as root or another user. To enable this, you need to specify that this user has the right to access your graphical desktop. For example:

    xhost +si:localuser:terdon
    

    That will allow the local user terdon to connect to the running X server. The command needs to be run by the owner of the X session. Alternatively, to allow anyone to connect:

    xhost +
    

    And to revoke permissions:

    xhost -