How to Install Minecraft Server on Ubuntu 18.04
7 min read

How to Install Minecraft Server on Ubuntu 18.04

In this tutorial, you will install Minecraft on Ubuntu 18.04. Minecraft is one of the very popular games. It is a sandbox video game that allows its players to look for infinite worlds.
How to Install Minecraft Server on Ubuntu 18.04

Introduction

Before we begin talking about how to install Minecraft on Ubuntu 18.04, let’s briefly understand - What is Minecraft?

Minecraft is one of the very popular games. It is a sandbox video game that allows its players to look for infinite worlds. Moreover, enables us to build everything from simple houses to large skyscrapers.

In this tutorial, you will Install Minecraft Server on Ubuntu 18.04. We will also address some of the FAQs related to the Minecraft installation.

Prerequisites

  • A user with sudo privileges to be able to install the packages.

You will install the packages necessary to build the mcrcon tool, by:

sudo apt update
sudo apt install git build-essential

Step 1 - Installing the Java Runtime Environment

1) Minecraft needs Java 8 version or greater. The Minecraft Server does not need a graphical user interface. So, install the headless version of  JRE, as it is more suitable for server applications. Further, it has fewer dependencies as well as uses fewer system resources. To install the headless OpenJRE 8 package, run the following:

sudo apt install openjdk-8-jre-headless

2) Then, to verify the Java version installation, print:

java -version

You will see the following output:

Output

openjdk version "1.8.0_212"
OpenJDK Runtime Environment (build 1.8.0_212-8u212-b03-0ubuntu1.18.04.1-b03)
OpenJDK 64-Bit Server VM (build 25.212-b03, mixed mode)

Step 2 - Creating the Minecraft User

1) Minecraft should never be run as a root user due to security concerns. You will have to create a new system user and group in a home directory /opt/minecraft that will be used to run the Minecraft server.

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

Step 3 - Install Minecraft on Ubuntu

1) Firstly, switch to the minecraft user.

sudo su - minecraft

2) Create three new directories using the following command:

mkdir -p ~/{backups,tools,server}
  • Backups directory for storing your server backup.
  • Tools directory for storing the mcrcon client and the backup script.
  • Server directory for the actual Minecraft server and its data.

Downloading and Compiling

1) RCON protocol is used to connect to the Minecraft server and execute commands. You will need to download the source code from Github and build the mcrcon binary.

2) Navigate to the ~/tools directory and clone the Tiiffi/mcrcon repository from GitHub.

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

3) Switch to the repository directory once the cloning is complete.

cd ~/tools/mcrcon

4) Compile the mcrcon utility using the following command:

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

5) Test it once the compilation is done.

./mcrcon -h

You will get an output similar to below:

Output

Usage: mcrcon [OPTIONS]... [COMMANDS]...
Sends rcon commands to Minecraft server.

Option:
  -h		Print usage
  -H		Server address
  -P		Port (default is 25575)
  -p		Rcon password
  -t		Interactive terminal mode
  -s		Silent mode (do not print received packets)
  -c		Disable colors
  -r		Output raw packets (debugging and custom handling)
  -v		Output version information

Set Server address, port and password using following environment variables:
  MCRCON_HOST
  MCRCON_PORT
  MCRCON_PASS

Command-line options will override environment variables.
Rcon commands with arguments must be enclosed in quotes.

Example:
	mcrcon -H my.minecraft.server -p password "say Server is restarting!" save-all stop

mcrcon 0.6.1 (built: May 19 2019 23:39:16)
Report bugs to tiiffi_at_gmail_dot_com or https://github.com/Tiiffi/mcrcon/issues/

Step 4 - Downloading Minecraft Server

1) The Minecraft server mods like Craftbuk-kit or Spigot allows you to add features on the server. It further customizes as well as improves the server settings. You will now install the latest Mojang’s official vanilla Minecraft server.

2) Check the Minecraft download page before continuing with the next step.

3) After that, run the following wget command. It will download the Minecraft jar file in the ~/server directory:

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

Step 5 - Next Configuring the Minecraft Server

1) After the download is complete, you need to navigate to ~/server directory and start the Minecraft server:

cd ~/server
java -Xmx1024M -Xms512M -jar server.jar nogui

2) After you will start the server for the first time, it executes some operations and will create both server.properties and eula.txt files and then stop.

[23:41:44] [main/ERROR]: Failed to load properties from file: server.properties
[23:41:45] [main/WARN]: Failed to load eula.txt
[23:41:45] [main/INFO]: You need to agree to the EULA in order to run the server. Go to eula.txt for more info.

3) You will need to agree to the Minecraft EULA, to run the server. Then open the eula.txt file and change eula=false to eula=true:

nano ~/server/eula.txt
#By changing the setting below to TRUE you are indicating your agreement to our EULA (https://account.mojang.com/documents/minecraft_eula).
#Sun May 19 23:41:45 PDT 2019
eula=true

4) Then, close and save the file.

5) After that, you will need to edit the server.properties file. It will enable the rcon protocol and will set the rcon password. So, open the file using text-editor.

nano ~/server/server.properties

6) You will then locate the following lines to update their values as shown below:

rcon.port=25575
rcon.password=strong-password
enable-rcon=true
Note: Do change the strong-password to something more secure. If you do not want to connect to the Minecraft server from remote locations, make sure that the rcon port is blocked by your firewall.

You can even adjust the server’s default properties. Visit the official server.properties page for more details.

Step 6 - Creating Systemd Unit File

1) Now, to run the Minecraft as a service, you will create a new Systemd unit file.

2) Switch back to the sudo user by typing exit.

3) After that, open the text editor and, then create a file named minecraft.service in /etc/systemd/system/:

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

4) Now, paste the following configurations:

[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

5) You will need to modify both Xmx and Xms flags as per your server resources. Here, Xmx flag means the maximum memory allocation for a Java virtual machine (JVM). While, the Xms means the initial memory allocation pool. Also, make sure to use the correct rcon port and password.

6) Then, save and close the file. Reload the systemd manager configuration using the following command:

sudo systemctl daemon-reload

7) Now you will start the Minecraft server by running:

sudo systemctl start minecraft

8) The first time you start the service, it will generate several configuration files and directories as well, including the Minecraft world. You will check the service status by running the following command:

sudo systemctl status minecraft
Output

* minecraft.service - Minecraft Server
   Loaded: loaded (/etc/systemd/system/minecraft.service; disabled; vendor preset: enabled)
   Active: active (running) since Sun 2019-05-19 23:49:18 PDT; 9min ago
 Main PID: 11262 (java)
    Tasks: 19 (limit: 2319)
   CGroup: /system.slice/minecraft.service
           `-11262 /usr/bin/java -Xmx1024M -Xms512M -jar server.jar nogui

9) Finally, it will enable the Minecraft service to start automatically at boot time:

sudo systemctl enable minecraft

Step 7 - Adjusting the Firewall

1) If the server is protected by a firewall and you want to access the Minecraft server from outside of your local network. You will need to open the port 25565.

2) Also, to allow the traffic on default Minecraft port 25565, run the following command:

sudo ufw allow 25565/tcp

Step 8 - Configuring the Backups

1) Now, you will create a backup shell script and cronjob. It will automatically backup the Minecraft server.

Start by switching to user minecraft:

sudo su - minecraft

2) After that, open the text editor and create the following file:

nano /opt/minecraft/tools/backup.sh

3) After that, paste the below configurations:

#!/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

4) Next, you will need to save the file. Make the script executable by running the following chmod command:

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

5) Then create a cron job. It will run once in an entire day automatically at a fixed time.

Open the crontab file using the following command:

crontab -e

6) Now, run the backup script every day at around 21:00. You will paste the following line:

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

Step 9 - Accessing the Minecraft Console

1) To have access to Minecraft Console you will use the mcrcon utility. Further, you need to mention the host, rcon-port, rcon-password, and use -t switch. It will enable the mcrcon terminal mode, by running:

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

The Output will be:

Logged in. Type "Q" to quit!

2) Also, when accessing the Minecraft Console from a remote location, make sure the rcon port is not blocked. If you are a regular user, instead of connecting to Minecraft console, you should create a bash alias.

FAQs to Install Minecraft Server on Ubuntu 18.04

1) What if you are not able to join servers via IP addresses on consoles?

The console makers have certain restrictions on what players can connect to. These are rules that Minecraft needs to follow. As a result, connecting to the server IP addresses limits only to Windows 10 and mobile devices too.

2) Can you play multiplayer with other versions of the Minecraft?

The answer is No. The Minecraft - Education Edition is not compatible with other versions of Minecraft like Minecraft PE, Java, the Bedrock, and many more.

3) Can you code in Minecraft - Education Edition?

The answer is Yes. The Code Builder is now available in-game. You just need to press "c" on the keyboard for it to launch. Also, the Code Connection is a separate companion app. It is compatible with the older versions of Minecraft - Education Edition and the Bedrock edition as well.

Conclusion

We hope this detailed guide helped you understand how to install Minecraft on Ubuntu 18.04 server. To learn more about Minecraft installation, check out the official Minecraft installation document.

If you have any queries, please leave a comment below and we’ll be happy to respond to them for sure.