How to Setup a Minecraft Server on Ubuntu: A Step-by-Step Guide
Tags: minecraft, ubuntu, games, hosting
Published: 2024-01-07 09:09:55

## Introduction

Have you ever wanted to setup your own Minecraft server? Perhaps a private server with your pals? Then you've come to the right place for a journey through a comprehensive guide! While the adventure may span a few paragraphs, rest assured that you'll emerge armed with the expertise to bring your exclusive Minecraft server to life by the end of this narrative!

## Prerequisites

Before we dive into the nitty-gritty of Minecraft magic, do note that this guide assumes knowledge of the following:
- Provisioning a VPS
- Familiarity with SSH
- Familiarity with linux command line

We will not be covering the above as they are not the focus of this guide - there are plenty of guides out there for them as well! All that said, if you need help with any of the above, you are more than welcome to [reach out](https://discord.gg/X8VSdZvBQY) for assistance.

## Server Setup

To kick off your Minecraft server journey, you will need to have a VPS (Virtual Private Server) provisioned from cloud providers such as [DigitalOcean](https://www.digitalocean.com/) or [OVH](https://www.ovhcloud.com/en-sg/). Since we're going with a basic Minecraft server setup, just 1GB of RAM will suffice. However, if you are a little more experienced and are ambitious, feel free to opt for a more robust machine for added flexibility and customization down the road.

Once you have your server provisioned, it's time to get your hands dirty with the actual setup! First, **SSH** into your server with the following command (replacing _172.1.1.1_ with your actual IP address):
```
ssh root@172.1.1.1
```
You will be prompted to enter the root password that would have been provided by your cloud provider. Upon logging in, run the following commands to update packages:
```
apt-get update && apt-get upgrade
```
Since Minecraft runs on Java, we will also need to install the relevant Java versions based on your choice of Minecraft versions. Below are the version compatibilities (last verified January 2024) along with the applicable Java installation commands:
**Minecraft Versions 1.8–1.12 (Java 8):**
```
sudo apt-get install openjdk-8-jdk
```
**Minecraft Versions 1.13–1.16 (Java 11):**
```
sudo apt-get install openjdk-11-jdk
```
**Minecraft Versions 1.17+ (Java 17):**
```
sudo apt-get install openjdk-17-jdk
```
Next, to ensure that we can keep our Minecraft server running 24/7 later on, we will be using **screen** which can be installed with the following command:
```
apt-get install screen
```
Finally, as Minecraft runs on the default port of 25565 (which is what we will be using), update the firewall to allow TCP connections on port 25565:
```
ufw allow 25565/tcp
```
And that sums up pretty much all the steps needed for our server setup!

## User Setup

With the server setup completed, we are not too far off from getting a simple Minecraft server up and running. But first, let us create a new minecraft user that we will be handling our server with! Run the following command to add the user:
```
adduser minecraft
```
We will also temporarily give the user superuser permissions for ease of setup:
```
usermod -aG sudo minecraft
```
For the remaining part of the guide, exit from your root **SSH** session and login as **minecraft** instead (again replacing _172.1.1.1_ with the IP address of your VPS):
```
ssh minecraft@172.1.1.1
```

## Download & Launch

Let us now create a **server** directory for this project and keep the relevant server files within it:
```
mkdir server
cd server
```
We will be downloading the Minecraft server files into this directory. To preface, there are numerous variations of server JARs that can be used (e.g. [Spigot](https://www.spigotmc.org/), [Paper](https://papermc.io/), [Purpur](https://purpurmc.org/) each touting their own benefits). For this tutorial, we will be working with [Paper](https://papermc.io/), a fork of [Spigot](https://www.spigotmc.org/) that is optimized for performance.

You may choose to download the files locally and then upload them to your server using another client like [WinSCP](https://winscp.net/eng/index.php) or [FileZilla](https://filezilla-project.org/), but the simplest way would be to use **wget** with the download link as below:
```
wget -O minecraft_server.jar https://api.papermc.io/v2/projects/paper/versions/1.20.4/builds/373/downloads/paper-1.20.4-373.jar
```
If you are using other variants, simply replace the download link in the command above. Once the download is complete, you will see a **minecraft_server.jar** file residing within the current directory. This is the executable that you will run to get your Minecraft server up and running! Give the following command a try:
```
java -Xmx512M -Xms512M -jar minecraft_server.jar nogui
```
The above command executes your **minecraft_server.jar** file with the first 2 flags specifying the amount of memory to run the server with (512mb in this case) while the nogui is included to save server resources since there is no need for any graphical interface. Feel free to adjust the memory but note that it is capped by the ram you provisioned your VPS with and that your VPS itself would need some memory for its system processes as well!

All that said, on the first try, you will get an error telling you to accept the **EULA**. You may do so by editing **eula.txt** (automatically generated when you attempt to run the server) and changing the last line from false to true:
```
vim eula.txt
```
You will also notice several new files, including **server.properties**. If you would like to run your server in **offline mode** (allow players with cracked launchers to join), you will need to edit the file to set **online-mode** to false.

Now, try running **minecraft_server.jar** again and your console output should quickly show that your server has been successfully started. Go ahead and fire up your Minecraft launcher on your PC and join your very own server with the IP address of your VPS!

![Demo Game](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/12mbs6e37nfy710ir33b.png)

Looks sweet isn't it? While we have successfully gotten a Minecraft server up and running, try exiting from your **SSH** session. Did your Minecraft server abruptly shutdown as well? Read on to find out what to do!

## Ensuring 24/7 Availability

As you may be able to tell from above, the continued running of your Minecraft server depended upon you maintaining an **SSH** connection to your VPS server - not the most practical approach if you want to have your server available all the time. This is why we have installed **screen** earlier to help us circumvent this problem! Start a new screen session with the following command:
```
screen
```
Hit enter and at first glance, everything would feel the same. However, you will notice that you are now attached to a screen session if you run the following command:
```
screen -ls
```
A screen session can essentially be thought of as its own window that continues to run in the background. Within your current screen session, run the exact same command again to start up your Minecraft server:
```
java -Xmx512M -Xms512M -jar minecraft_server.jar nogui
```
Exit the screen session (think of it as minimizing the window) by pressing **CTRL**, **A** and **D** on your keyboard together at the same time. Make sure all 3 keys are pressed together lest you make the nasty mistake of pressing only **CTRL** and **D** which would terminate the entire screen session instead.

Try exiting from your **SSH** session and connecting to your Minecraft server in the launcher again. You will notice that this time, your Minecraft server continues to run just fine!

To resume your screen session, you may **SSH** back into the server anytime and run the following command:
```
screen -r
```
On a side note, screen (or tmux if you prefer so) has a wide variety of uses beyond just the one shown in this guide. They may be used to run time consuming processes such as training machine learning models or even something as simple as to host telegram bots.

## Minecraft as a Service

A Minecraft server, especially those with significant modifications, tends to run a risk of performance issues overtime (e.g. from the use of poorly coded plugins that do not handle resources well). In rare situations, this can even result in a server crash!

With that in mind, we will now create a service for our Minecraft server so that even in the event of a major crash, the server is able to (or will at least try to) bring itself back up online.

First, create your own service file with the following command:
```
sudo vim /etc/systemd/system/mc-server@.service
```
Within it, copy and paste the following content:
```
[Unit]
Description=Minecraft Server: %i
After=network.target

[Service]
WorkingDirectory=/home/minecraft/%i

User=minecraft
Group=minecraft

Restart=always

ExecStart=/usr/bin/screen -DmSL mc-%i /usr/bin/java -Xms512M -Xmx512M -jar minecraft_server.jar nogui

ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "say SERVER SHUTTING DOWN IN 5 SECONDS. SAVING ALL MAPS…"\015'
ExecStop=/bin/sleep 5
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "save-all"\015'
ExecStop=/usr/bin/screen -p 0 -S mc-%i -X eval 'stuff "stop"\015'

[Install]
WantedBy=multi-user.target
```
In the above service file, we specify the **User** and **Group** as ourselves (**minecraft**) and also define the working directory that has the **minecraft_server.jar** we want to run. **ExecStart** and **ExecStop**, as you may be able to guess, specifies the commands we wish to execute when the service is started and when the service is stopped. In our case, we are particularly interested in **ExecStart** which creates a screen session and runs our **minecraft_server.jar** within it.

Now that you have a better understanding on the contents of the service file, try starting your service with the following command (**terminate your existing screen session** if you have not!):
```
sudo systemctl start mc-server@server.service
```
You will now notice again that there is an active screen session running if you run:
```
screen -ls
```
In addition, it's also a good idea to have the service start itself automatically if you ever do a full reboot of your VPS. This can be easily done with the following command:
```
sudo systemctl enable mc-server@server.service
```
All that done, your Minecraft server will now restart itself in the unfortunate event that it crashes/terminates, as well as when you do a full reboot of your VPS!

## Daily Server Restarts

As mentioned above, Minecraft servers may run into performance issues overtime. A common practice is thus to restart your server regularly - which doubles up as an opportunity for server owners to apply quick fixes/updates. We will now create a short script within the base directory of our setup (where **minecraft_server.jar** is). If you are not already there, go to the base of our setup with:
```
cd ~/server
```
Next, create a simple shellscript:
```
vim dailyrestart.sh
```
Within the shellscript, copy and paste the following:
```
#!/bin/sh
screen -S "mc-server" -X stuff 'say Daily server restart in 60 seconds\r' 
sleep 30
screen -S "mc-server" -X stuff 'say Daily server restart in 30 seconds\r'
sleep 20
screen -S "mc-server" -X stuff 'say Daily server restart in 10 seconds\r'
sleep 5
screen -S "mc-server" -X stuff 'say Server restarting in 5 seconds\r'
sleep 1
screen -S "mc-server" -X stuff 'say Server restarting in 4 seconds\r'
sleep 1
screen -S "mc-server" -X stuff 'say Server restarting in 3 seconds\r'
sleep 1
screen -S "mc-server" -X stuff 'say Server restarting in 2 seconds\r'
sleep 1
screen -S "mc-server" -X stuff 'say Server restarting in 1 second\r'
sleep 1
screen -S "mc-server" -X stuff 'restart\r'
```
The above script runs commands directly into the console of your Minecraft server, sending a 1 minute countdown reminder to whoever is in your server before actually restarting the server.

To have this script run automatically on a daily basis, we will use **crontab**:
```
crontab -e
```
At the top of your crontab file, add the following line and save:
```
0 1 * * * /bin/sh /home/minecraft/server/dailyrestart.sh
```
With the above example script, your server will now restart daily at 9am (GMT +8). Feel free to adjust the time according to your preference!

## Minecraft Server Domain Name (Optional)

With the majority of the setup done, the one last thing you need for your Minecraft server to look as professional as it is is a domain name for people to remember and go by. [Freenom](https://www.freenom.com/en/index.html?lang=en) provides free domain names so for those trying out but do not wish to spend the money to do so, you may wish to give it a try! Note that this section will only cover DNS configuration and will not go through how one can obtain a domain name but again, feel free to [reach out](https://discord.com/invite/X8VSdZvBQY).

Assuming you have obtained your own domain name, you will now want to configure your DNS. Within your DNS panel, you will want to create 2 new records - a **Service record** as well as an **A record**.

In your **Service record**, you will need to fill up the following fields (replace _play.mcdemo.tk_ with the name you want your players to join with):
```
Hostname: _minecraft._tcp.play.mcdemo.tk
Will direct to: join
TTL: 43200
Port: 25565
Priority: 0
Weight: 5
```
In your **A record**, you will need to fill up the following fields (again switch out the domain name and direct the IP address to that of your VPS):
```
Hostname: join.mcdemo.tk
Will direct to: 11.11.11.11
TTL: 3600
```
Save both records and wait a while for them to propagate, then try joining your server again with the newly configured domain name!

## Conclusion

With that, you now have a proper setup to run your very own Minecraft server! Take note that a lot of the details covered here are generic and there are many specific tweaks that you can explore on your own so feel free to tinker around with your own setup! If you need someone to bounce ideas with, I'm more than happy to do so [here](https://discord.com/invite/X8VSdZvBQY) or in the comments.

Otherwise, if you're interested in hearing about my Minecraft server journey, look out for my next article to accompany me on my adventure! Above all, remember to enjoy the experience!