Configuring And Running A Minecraft Java Edition Server On Your VPS Running Ubuntu 16.04
Unless you’ve been living under a block since 2010, you’ve probably heard about the runaway success story of the computer game Minecraft. Having said that, I suspect if you are reading this article you already have a fair idea what Minecraft is. One of the features of the PC version of Minecraft, these days called Java Edition, is the ability to run your own game server on your own computer or server, allowing you and your friends to play together in privacy.
The first thing you need to do is spec up your VPS. We’d recommend a minimum of 1GB RAM to enable your VPS to run the Minecraft server for a handful of players, and 1.5GB RAM should see you capable of running a server for up to 20 players. Regarding the operating system, we’ll be looking at using Ubuntu 16.04 to ease the setup.
Installing Java
Minecraft requires Java to run, and for stability, it’s best to use the official Oracle Java distribution. To install this on Ubuntu we can use the Web Upd8 team’s PPA:
sudo apt-add-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer
This makes simple work of getting Java installed. After this, we need to prepare for running Minecraft. However, first we’ll make a user to run the server:
sudo adduser minecraft
Next, we need to create somewhere to store the game files. By convention on Linux this would be in /usr/local/games, and then we need to set ownership for our Minecraft user:
sudo mkdir /usr/local/games/minecraft
sudo chown minecraft:minecraft /usr/local/games/minecraft
cd /usr/local/games/minecraft
Downloading Minecraft
Now we need to change user to our Minecraft user and download the latest Minecraft server which can be found at http://minecraft.net/en/download/server, which at the time of writing is 1.12.2. Right-click the download link on that page and copy the link to use in the command below:
su minecraft
wget http://s3.amazonaws.com/Minecraft.Download/versions/1.12.2/minecraft_server.1.12.2.jar -O minecraft_server.jar
To run the server use the following command, and note that this command is based on you having a VPS with 1GB RAM:
java -Xms512M -Xmx512M -jar minecraft_server.jar nogui
The command will fail, but by doing so will create a file called eula.txt, which is a licensing agreement you must agree to before you can use the server. To agree, open the file for editing and change the value of false to true:
nano eula.txt
With the file edited and saved, you can repeat the previous command to launch the server. If it launches, you’ll be at the Minecraft server’s command prompt, and to stop the server you can use the following command:
/stop
When launching Minecraft, the -Xms and -Xmx values refer to the amount of memory that Java should use when running Minecraft. We’d recommend making it 512MB smaller than the amount of memory assigned to the VPS, though you may get away with only being 256MB smaller; this remaining memory is used by the Linux operating system on your VPS. So if you have 2GB RAM in your VPS, you could use the following command:
java -Xms1536M -XmX1536M -jar minecraft_server.jar nogui
Once you have worked out the command you need to use to run Minecraft we’ll want to make a short bash script to launch it for us:
nano minecraft.sh
Then paste in the following:
#!/bin/bash
/usr/bin/java -Xms512M -Xmx512M -jar /usr/local/games/minecraft/minecraft_server.jar nogui
Note that this time we’ve used the full paths to the files to be sure it’ll run fine. Tweak the Xms and Xmx values to match your system, then save the file. Now set the file to be executable:
chmod +x minecraft.sh
Service File for Starting and Stopping
Finally, we need to start and stop the server easily, and to do this we’ll create a service file for systemd. To do this, we’ll exit from the Minecraft user’s session, then create the file we’ll use:
exit
sudo nano /etc/systemd/system/minecraft.service
Into that file, paste the following configuration:
[Unit]
Description=Minecraft server
Wants=network.target
After=network.target
[Service]
User=minecraft
Group=minecraft
Nice=5
WorkingDirectory=/usr/local/games/minecraft
ExecStart=/usr/bin/screen -dmS minecraft /usr/local/games/minecraft/minecraft.sh
ExecStop=/usr/bin/screen -p 0 -S minecraft -X eval ‘stuff \”stop\”\015’
ExecStop=/bin/bash -c “while ps -p $MAINPID > /dev/null; do /bin/sleep 1; done”
[Install]
WantedBy=multi-user.target
Save and exit this file.
You can now use systemctl to manage your Minecraft server.
sudo systemctl start minecraft.service
Note that you won’t get any output from this script, whether it works or not. So you can check with:
sudo systemctl status minecraft.service
Stopping Minecraft can be done with:
sudo systemctl stop minecraft.service
Finally, you can enable Minecraft to ensure it restarts automatically if the VPS reboots with the following command:
sudo systemctl enable minecraft.service
Something to note is that as we’ve launched Minecraft within screen, if you ever need to interact with the server at its command line you can do so by connecting to its screen session.