How to disable GUI in Ubuntu

Say if I wanted to replicate Ubuntu Server and have no GUI running what would I do to make this happen? Upon running htop/top from tty session I see both gnome and xorg are still running and sucking up quite a substantial amount of memory (I'm using VirtualBox and have 1024MB allocated to Ubuntu). I would preferably want the ability to disable/stop gnome and Xorg whenever I wish in order to free resources. I don't see any point in switching to tty from Xorg and keeping the processes running. I would however like to have the ability to re-enable/start the necessary files upon leaving tty. I do not want things to be permanently disabled although having this option would be useful/

Any help? Thanks


Solution 1:

You can use systemctl to "isolate" targets, which is to some extend similar to switching runlevels. The targets of interest here are

  • graphical.target

  • multi-user.target

Confusingly, graphical.target is the default target in both Ubuntu desktop and server, but since there is no display manager installed in server it's essentially the same as multi-user.target.

Switch while Ubuntu is running

Switch to "text mode" (you can simply run this in e.g. gnome-terminal):

sudo systemctl isolate multi-user.target

Switch to "graphical mode":

sudo systemctl isolate graphical.target

Set boot target

You can set the default target that is reached after boot (persists across reboot), e.g.:

sudo systemctl set-default multi-user.target

You can also set a target with the kernel parameter systemd.unit, e.g.

systemd.unit=multi-user.target

The kernel parameter has precedence over the default target.

This can be used to boot to a specific target once by editing the grub boot entry before boot. Or you could add multiple boot entries for different targets.

Solution 2:

You can use systemctl(the systemd system and service manager) to control your display manager. In the case of Ubuntu, this is GDM - Gnome Display Manager, SDDM and LightDM are other common display managers.

To check the status from the command line:

sudo systemctl status gdm

To stop:

sudo systemctl stop gdm

To start:

sudo systemctl start gdm

To disable (prevent loading at system startup):

sudo systemctl disable gdm

To enable (loading at system startup):

sudo systemctl enable gdm

Solution 3:

Another way of achieving this is to edit the line beginning with the linux command on your grub entry and add the number 3 at the end to boot in runlevel 3 which won't start x-server at all by default.

It should look something like this :

 linux  /boot/vmlinuz-5.3.0-46-generic root=UUID=SOME_UUID ro quiet splash 3

This has the same result as others have pointed out , but you can change it even before the system boots up.

In the grub menu press the e button on the ubuntu menu entry and after putting 3 at the end of the line , just press Ctrl+x to boot. Note that this won't save this config for you. If you want to save it , you must edit the file /boot/grub/grub.cfg. (And this is the scenario if you cannot get the grub menu at boot screen , because for example grub's timeout was set to zero. )

And another option for you is to install Ubuntu server which doesn't have GUI at all. unless you have a reason to stick with the desktop version. (e.g desktop version has more tools installed by default , like g++ ).