How to Install Ubuntu Server on a Raspberry Pi 4

Published

Table of Contents

Introduction

The Raspberry Pi 4 is powerful enough to run a web server to host multiple websites. At least the 8Gb version is. That’s the version I use for this very purpose. As a web designer and developer, I like to host my own websites on my own server.

This is a short guide on the steps you need to take to install Ubuntu Server, update the application packages and set a static IP address (optional). It forms part of a larger guide I’ve written on Running a Live Web Server on a Raspberry Pi from Home.

Install Ubuntu Server on Raspberry Pi 4

Ubuntu Server is probably the most user-friendly server setup we can use on the Pi and it’s free to use. It is not bundled with a graphical user interface, which means more hardware resources are available to your server environment. An important factor when using a low power device like a Raspberry Pi.

Install Ubuntu Server 20.04 LTS or 21.10 Image

The latest versions of Ubuntu are designed to work with the Raspberry Pi. This makes the setup much easier. You simply copy the image to SD card using an imaging application, such as Balena Etcher and you are good to go.

You can download the images from the Ubuntu website. Make sure to get the version made for computers with Arm processors.

Ubuntu Server Downloads

Then install Balena Etcher from here.

Balena Etcher Download

Installing the image is as simple as selecting your downloaded image, then selecting the destination drive, e.g. SD card, and flashing.

It is possible to copy your image to a USB SSD and boot from USB but you would need to install Raspberry Pi OS on an SD card first and follow the instruction on the Raspberry Pi website for setting up USB boot.

The configuration method has changed since the Pi 4 was first released, so it is safest to check the lastest method used on their website.

Raspberry Pi 4 USB Boot

If you choose to boot your Raspberry Pi 4 from USB, I would not recommend using Ubuntu Server 20.04. There are some changes you have to make to the configuration within Ubuntu to allow it to USB boot but these changes get overwritten during Ubuntu kernel updates, rendering your server unbootable. You can reconfigure the server each time an update occurs and get it working again, which is fine if you are using the server for local development only. However, having a server become unbootable is not ideal for a live production server. For this reason, I recommend Ubuntu Server 21.10 if you choose to install on to a USB drive. You can always upgrade to the next Long Term Support (LTS) server version when it becomes available, as this should also support USB boot, as version 21.10 does.

When you first boot your server, login with the default username ‘ubuntu’ and password, which is also ‘ubuntu’. You will then be asked to set a new password. Do so, login, and then move on to the next step.

Update Ubuntu Software Sources and Applications

You first want to upgrade your software sources list and then install the latest versions of all software currently installed. Typing ‘sudo’ before each command gives you superuser permissions and allows you to make changes to protected areas of the operating system. You will receive a password prompt for your admin password when elevating permissions in this way for some actions.

sudo apt update && sudo apt upgrade -y 

Usually, you do not have to restart after and update but if you do ever need to restart the server you can type:

sudo shutdown -r now 

If you want to shutdown your server when taking a break from this tutorial then type:

sudo shutdown now 

Set a Static IP Address

As mentioned in previous sections, it is necessary to use a static IP on your server. Let’s examine this in more detail now that we are setting up Ubuntu Server. Assigning a static IP address can be done in one of two ways.

The first method is to use DHCP on your home router. Using this method, you would assign a specific IP address to the MAC address of your Raspberry Pi network adapter, either the WiFi or wired connection and your router will reserve and automatically assign the chosen IP to your Pi on every boot. I prefer to use a wired connection as it is more stable and less can go wrong. Every router is different, so you should check out the instructions for your router, although it is fairly straightforward to setup.

To get your MAC address in Ubuntu, simply type:

ip addr show 

You can take the MAC address here and return to your router configuration to enter it if you want to assign your IP address using DHCP. Either selct the MAC address for eth0 or wlan0, depending on whther you decide to use a wired or wireless connection. The MAC address will be six pairs of hexadecimal values separated by colons. E.g. xx:xx:xx:xx:xx:xx.

You can also assign the IP address manually within Ubuntu Server, which is my preferred method. To do so, first identify the name of your network interface. Again this is likely to be ‘eth0’ or ‘wlan0’ but to be sure type:

ip link 

Now you need to edit the file that holds you network interface information. The name of this file can vary from system to system. You can list the contents of the directory where the file resides by typing:

sudo ls /etc/netplan 

On Ubuntu Server 20.04, the file name appears to be ‘50-cloud-init.yaml’. Let’s edit this file.

sudo nano /etc/netplan/50-cloud-init.yaml 

Then change the file contents to the same as below, entering the details of your home network that were displayed in the previous commands. The IP address will be the static address of your choosing. If you are still unsure what your network settings should be, you can either check your router settings via your router’s admin page. Or if you are on Windows you can type this command from the command line:

ipconfig /all 

Your edited file should look like this.

network:
    ethernets:
        eth0:
            dhcp4: no
            addresses:
            - 192.168.1.2/24
            gateway4: 192.168.1.1
            nameservers:
                addresses: [192.168.1.1]
            optional: true
    version: 2 

This is a YAML file which requires you to indent the lines according to YAML standards, so make sure you indent the lines properly under your adapter name. Also, you can have more than one IP address, which is why the addresses are indented underneath their name. However, you only need one IP address to get your server working.

Press CTRL+O to save and CTRL+X to exit the editor.

Apply the changes by typing:

sudo netplan apply 

If you have jumped ahead and are remotely connecting to your server at this point, you will lose your connection and will have to reconnect using the new IP address (if different). Then you can move on to the next step.

Verify the changes by typing:

ip addr show dev eth0 

Or if using the WiFi connection:

ip addr show dev wlan0 

The output should show you newly assigned static IP address.

Conclusion

That’s it! You now have Ubuntu Server running on a raspberry Pi 4. Whilst it’s slightly trickier than installing Ubuntu on a desktop PC because you have to prepare an SD card, it’s not too complex. If you want to expand your knowledge further then you can follow the link on setting up a web server that I’ve linked to in the introduction.