How to Run a Minecraft Server on Ubuntu 20.04
You want to play Minecraft in multiplayer. You’ve decided that you want your own world with your own rules. Then let’s set up your own online server.
You will learn how host a Spigot Minecraft server on Ubuntu Server 20.04. You will also learn how to keep access to its console via Tmux.
Prerequisites
Before you can set up your own server, you do the following:
- Set up a machine with Ubuntu Server 20.04
- Access the machine’s command line interface
- Allow traffic through port 25565
From my experience of running a server for 2-4 players, a machine with 2GiB of memory and 8GiB of storage size is enough. Minecraft server uses port 25565, therefore you must open it or the server will be inaccessible to players.
Environment Preparation
Before you start, update the software on your server.
sudo apt update && sudo apt upgrade
When that is done, go ahead and create a user for the Minecraft server. Using a dedicated account for the server limits its access to other files on the system. This can help if the server gets compromised.
sudo useradd minecraft -r -m -d /opt/minecraft
The useradd
command creates a new user named minecraft
. The -r
argument creates a system account, which indicates that this user is used by a service or daemon. Argument -m
creates a home directory and -d
specifies the path where this directory will be created.
Installation
Now that the environment is set up, you will install Spigot, which is a server software for Minecraft that is optimized for performance. Spigot must be compiled by another program called BuildTools, so you will have to install that first.
Setting up BuildTools
To run BuildTools you need to have Git and Java installed on your machine.
sudo apt-get install git openjdk-8-jre-headless
When that is done, switch to the minecraft
user you created while preparing the environment.
sudo su --login minecraft
Running the su
command with --login
argument should also switch your current directory to /opt/minecraft
. You can verify that by running pwd
, which prints the name of working directory.
pwd
/opt/minecraft
Now create two folders, one for BuildTools and one for Spigot server under /opt/minecraft
.
mkdir tools server
Finally, download the BuildTools
wget -O tools/BuildTools.jar https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar
Compiling The Server
There is a chance that BuildTools program fails to compile the server due to Git configuration. The documentation recommends disabling autocrlf
in Git preemptively.
git config --global --unset core.autocrlf
Run BuildTools and consider taking a short break, because it can take awhile. You deserve it.
cd tools
java -jar BuildTools.jar
Running The Server
After successfully compiling the server with BuildsTools, you should see a spigot-1.16.5.jar
file in your current directory /opt/minecraft/tools
. At the moment of writing the latest version is 1.16.5
which can differ in your case. Adjust accordingly.
Move spigot-1.16.5.jar
file to the server directory.
cp spigot-1.16.5.jar /opt/minecraft/server/spigot.jar
To start the server, run the following java
command from opt/minecraft/server
directory.
cd /opt/minecraft/server
java -Xmx1024M -Xms1024M -jar spigot.jar nogui
The 1024M
in -Xmx
and -Xms
arguments represents how much memory you want to allocate to the server in megabytes. You can adjust the number as necessary considering your hardware limitations.
Running the server for the first time produces an error. You can’t run a Minecraft server without agreeing to EULA. Therefore open eula.txt
and do just that by setting eula=true
.
nano eula.txt
Instead of remembering this command that starts the server, create a script that does it for you. Start by making a bash script.
nano start.sh
Now add the start command to the script.
#!/bin/bash
java -Xmx1024M -Xms1024M -jar spigot.jar nogui
And make the script executable.
chmod +x start.sh
Run the script to finally start your own Minecraft server.
./start.sh
Give it a go by connecting to the server now when it is done generating the world.
You can stop the server by entering stop
in its console. Enter exit
to stop using the shell as minecraft
user.
There’s one more thing left to do if you wish to always have access to the server’s console.
Accessing Server Console
When you start the server from command-line you have access to its console. If you disconnect from the remote machine, the next time you log back in, you won’t see the console anymore. It will have become a background process.
To fix this, you can run the server using Tmux. It is a program that lets you see the console after reconnecting to the remote machine by saving it in a session.
Run Minecraft server in a Tmux session from the /opt/minecraft/server
directory.
Install Tmux if it’s not already installed on your system.
sudo apt install tmux
Start the Minecraft server in a session as the minecraft
user.
sudo su --login minecraft
cd server
tmux new -s minecraft-session ./start.sh
The -s
argument specifies the name of the session, which you can choose as you please.
To leave the session press Ctrl+b
followed by d
.
You can see a list of available sessions if you forget the name of one.
tmux ls
To reattach to the session use this command.
tmux attach -t minecraft-session
The -t
argument specifies the name of the session.
When you reconnect to the remote machine, you can quickly gain access to the session without switching to minecraft
user.
sudo -H -u minecraft bash -c "tmux attach -t minecraft-session"
Summary
In this post you learned how to install BuildTools and Spigot to run your own Minecraft server. You also learned how to regain access to the server console using Tmux.