By Heidy RamirezJune 4th 2021

Written by: Suparna Ganguly

Minecraft is quite popular among video game lovers.

And if you want an amazing gaming experience on Linux Ubuntu you must have a dedicated server for dedicated servers like Minecraft. It has an advanced performance potential that makes Minecraft suitable for gaming workloads.

In this tutorial, I’ll tell you why and how to install a Minecraft server on Linux Ubuntu.

So, let’s start.

Why You Should Install Minecraft Server on Linux Ubuntu

A dedicated Minecraft server on Ubuntu allows enjoying the server resources all by yourself. So, you’re in control of storage, memory, and processing. Some of the main benefits of installing Minecraft server on your Linux are:

  • Control over plugins and mods
  • Full use of bandwidth and resources
  • Better privacy
  • Improved performance

Minecraft Server Prerequisites

To have Minecraft on your Linux PC, you need:

  • Minimum 5GB RAM
  • Dedicated game server
  • Intel core-based CPUs
  • Access to the Command Line

Install the packages to create the mcrcon tool:

$ sudo apt update

$ sudo apt install git build-essential

Steps of Installing Minecraft Server on Linux Ubuntu

Follow the steps given below to have a smooth Minecraft install on your Linux PC.

Install JRE

You need Java 8 or above for Minecraft.

For Minecraft, one needs to use headless JRE, because Minecraft isn’t compatible with a graphical user interface.

So, OpenJRE 8 package will be installed by:

$  sudo apt install openjdk-8-jre-headless

And verify the installation through:

$ java -version

Create Minecraft User for Your Security

Don’t run Minecraft under the root user for security purposes. Create a separate system user and group it with the home directory /opt/minecraft in the following manner:

$ sudo useradd -r -m -U -d /opt/minecraft -s /bin/bash minecraft

We’ll not set a password for the user because a password will resist the user from login via SSH.

To alter to a Minecraft user login as a user with sudo privileges or root.

Install Minecraft on Linux Ubuntu

Now, switch to Minecraft user by:

$ sudo su - minecraft

Create three new directories by writing the code below:

$ mkdir -p ~/{backups,tools,server}

So what’re these directories for?

  1. The backups will take a server backup.
  2. The tools will keep the backup script and the mcrcon client.
  3. The server directory stores the Minecraft server and the related details.

Download and Compile mcrcon

RCON protocol lets you execute commands when you’re connected to your Minecraft server on Linux Ubuntu. 

And mcrcon is a C-based RCON client.

From GitHub, download the source code and create the mcrcon binary.

To do this, navigate to the ~/tools and clone Tiiffi/mcrcon using this command:

$ cd ~/tools && git clone https://github.com/Tiiffi/mcrcon.git

After that, switch back to your repository directory:

$ cd ~/tools/mcrcon

Compile the mcrcon utility via:

$ gcc -std=gnu11 -pedantic -Wall -Wextra -O2 -s -o mcrcon mcrcon.c

And then test it by:

$ ./mcrcon -h

Download the Minecraft Server on Linux Ubuntu

Minecraft server mods like Spigot or Craftbukkit let you add plugins on your Minecraft server. You can also customize the server settings.

Here we’re installing Mojang’s vanilla Minecraft server.

You can download the Java archive file (JAR) of the Minecraft server from this page. And to do that run:

$ wget https://launcher.mojang.com/v1/objects/ed76d597a44c5266be2a7fcd77a8270f1f0bc118/server.jar -P ~/server 

Configure Minecraft Server

Next, navigate to the ~/server and begin with Minecraft server.

$ cd ~/server

$ java -Xmx1024M -Xms512M -jar server.jar nogui

Minecraft server will perform some operations and will create server.properties and eula.txt.

Agree to the EULA to run the server. For this, open the eula.txt and change eula=false to eula=true:

$ nano ~/server/eula.txt

Then close that file. Don’t forget to save!

Now, edit the server.properties to enable rcon and set a suitable rcon password.

Open your text editor by typing:

$ nano ~/server/server.properties

Update the values in the following manner:

rcon.port=25575

rcon.password=strong-password

enable-rcon=true

Build Systemd Unit File

Minecraft can be run as a service only after creating a Systemd unit file.

Type exit to switch back to sudo.

Then create a file called minecraft.service in /etc/systemd/system/ :

$ sudo nano /etc/systemd/system/minecraft.service

Paste the configuration as follows:

[Unit]

Description=Minecraft Server

After=network.target

[Service]

User=minecraft

Nice=1

KillMode=none

SuccessExitStatus=0 1

ProtectHome=true

ProtectSystem=full

PrivateDevices=true

NoNewPrivileges=true

WorkingDirectory=/opt/minecraft/server

ExecStart=/usr/bin/java -Xmx1024M -Xms512M -jar server.jar nogui

ExecStop=/opt/minecraft/tools/mcrcon/mcrcon -H 127.0.0.1 -P 25575 -p strong-password stop

[Install]

WantedBy=multi-user.target

Modify Xms and Xmx flags as per your server resources.

The Xms flag defines the initial memory allocation, while the Xmx defines the maximum memory allocation for a JVM (Java Virtual Machine)

You need to use the correct rcon password and port. Always keep this in mind.

Execute the command below to begin with the Minecraft server.

$ sudo systemctl start minecraft

Check the Minecraft service status with:

$ sudo systemctl status minecraft

Finally, the command below will enable the Minecraft service at boot time:

$ sudo systemctl enable minecraft

Adjust the Firewall

Open port 25565 to access the Minecraft server from other than your local network while the server has a firewall. 

Allow traffic on the port 25565 by:

$ sudo ufw allow 25565/tcp

Configure Backups

We’ll create an automatic backup using shell script and cronjob. At first, switch to user Minecraft:

$ sudo su - minecraft

Create the following file in your text editor:

$ nano /opt/minecraft/tools/backup.sh

Then paste the configuration given below:

$ #!/bin/bash

function rcon {

  /opt/minecraft/tools/mcrcon/mcrcon -H 127.0.0.1 -P 25575 -p strong-password "$1"

}

rcon "save-off"

rcon "save-all"

tar -cvpzf /opt/minecraft/backups/server-$(date +%F-%H-%M).tar.gz /opt/minecraft/server

rcon "save-on"

## Delete older backups

find /opt/minecraft/backups/ -type f -mtime +7 -name '*.gz' -delete

Run the following command to save the file and make your script executable.

$ chmod +x /opt/minecraft/tools/backup.sh

Then, create a cron job by typing:

$ crontab -e

0 23 * * * /opt/minecraft/tools/backup.sh

The second line will make the backup script run every day at 23:00.

Access Minecraft Console

Use the mcrcon utility to get hold of the console of Minecraft.

The syntax is given below:

$ /opt/minecraft/tools/mcrcon/mcrcon -H 127.0.0.1 -P 25575 -p strong-password -t

The rcon port mustn’t be blocked while accessing the Console.

The Conclusion

And that’s the end of this tutorial. You’ve installed the Minecraft server on Linux Ubuntu successfully. Set up a backup on a regular basis.

)