How To Install A Desktop And VNC On Ubuntu 16.04

Sometimes you might need an instance running a desktop environment to run GUI applications or to just use it with a desktop as a remote computer. Luckily for you, installing a desktop and connecting to it using VNC is really simple. VNC (Virtual network computing) is a graphical desktop sharing system which will allow you to connect to a computer, view it’s graphical interface and control it remotely using a VNC client.

Before You Begin

Update your system using the following command.

sudo apt-get update && sudo apt-get upgrade -y

Install Ubuntu Desktop on Your Instance

There are multiple desktop environments which you can install on your instance to use it with a graphical interface. We’re going to install the Unity desktop, which is the default desktop environment of Ubuntu. To install it along with a few other packages which are required for the graphical interface to function properly, run the following command.

sudo apt-get install ubuntu-desktop gnome-panel gnome-settings-daemon metacity nautilus gnome-terminal

During the installation, you might be asked whether you want to change a system file to a new version. Type y and then press enter to use the updated version.

Install and Configure a VNC Server

There are several VNC servers to chose from. We’re going to install vnc4server.

sudo apt-get install vnc4server

Now test the VNC server by starting a session.

vncserver :1

You will be prompted for a password. Set any password you like. You should now be able to connect using a VNC client. But you’ll see a gray screen instead of the desktop. We need to configure VNC to launch the full Unity desktop when it starts. To do it, first kill the VNC session.

vncserver -kill :1

Now edit the file ~/.vnc/xstartup to match the configuration given below. Start by opening the file on an editor.

Note: This file will only be created when you start a VNC session for the first time.

nano ~/.vnc/xstartup

Now edit the end of the file to match it with the below configuration.

#!/bin/sh

# Uncomment the following two lines for normal desktop:
# unset SESSION_MANAGER
# exec /etc/X11/xinit/xinitrc

[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &
x-terminal-emulator -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
x-window-manager &

gnome-panel &
gnome-settings-daemon &
metacity &
nautilus &

Save the file by pressing ctrl + o and exit by pressing ctrl + x and then pressing enter.

Now launch a VNC session again.

vncserver :1

You can now use a VNC client/viewer to connect to your computer remotely and use it. The following is your address which you need to enter when connecting.

server_ip_address:1

And the password will be what you set earlier when first launching the VNC session.