Setting Up a Minecraft Server on Google Compute Engine

This tutorial is not approved by or associated with Mojang or Minecraft.

To run a dedicated Minecraft server, you need to have a dedicated server machine, plenty of RAM, and plenty of bandwidth. Why not let Google handle these requirements for you? In this tutorial, you'll learn how to install, configure, and run a standard Java Minecraft server on Google Compute Engine. This server is compatible with standard Java-based desktop Minecraft clients.

Your Minecraft server software will run on a Compute Engine instance, which is a virtual machine that runs on Google's infrastructure. This tutorial uses the default machine type for Compute Engine instances, n1-standard-1. The n1-standard-1 machine type includes a 10 GB boot disk, 1 virtual CPU (vCPU), and 3.75 GB of RAM. This machine type runs Debian Linux by default.

To make sure there's plenty of room for your Minecraft server's world data, you'll also attach a high performance 50 GB persistent solid-state drive (SSD) to your instance. With the addition of this persistent SSD, your instance will satisfy the system requirements for a dedicated Minecraft server, supporting up to 50 players comfortably.

Objectives

  • Create a Google Compute Engine virtual machine instance
  • Install and configure the Minecraft server
  • Set up automatic backups of your Minecraft world data

Costs

This tutorial uses the following billable components of Google Cloud Platform:

  • Compute Engine virtual machines
  • Compute Engine persistent disks
  • Google Cloud Storage

You can use the pricing calculator to generate a cost estimate based on your projected usage. New Cloud Platform users might be eligible for a free trial.

Before you begin

  1. Sign in to your Google account.

    If you don't already have one, sign up for a new account.

  2. Select or create a Cloud Platform project.

    Go to the Manage resources page

  3. Enable billing for your project.

    Enable billing

Create and configure your Compute Engine instance

Start by creating and configuring a new Compute Engine instance:

  1. In the Cloud Platform Console, go to the VM instances page:

    VM instances page

    You'll arrive at a dialog prompting you to create a new Compute Engine instance.

  2. Click Create to get started.

  3. On the Create an instance page, configure your instance as follows:
    • Name your instance. This tutorial uses the instance name mc-server throughout.
    • Select the zone in which you want your instance to be hosted. This tutorial uses the zone us-central1-f throughout.
    • In the Boot disk section, click Change. The Boot disk dialog will pop up.
    • Change the disk type to SSD Persistent Disk.
  4. Click Select to commit the change and close the dialog.

That's it for basic configuration! That said, don't create your new instance just yet—to meet the requirements for a dedicated Minecraft server, you'll need to configure some advanced settings as well.

Enable Google Cloud Storage access for your instance

Later in this tutorial, you'll learn how to back up your world data to Google Cloud Storage, which requires your instance to have read and write access to Cloud Storage. To enable access:

  1. Under Identity and API access, click the Service account dropdown and select Compute Engine default service account.
  2. Set Access scopes to Set access for each API.
  3. In the Storage dropdown, select Read Write.

Tag your instance

Next, tag your instance. Later in the tutorial, you'll use this tag to create a firewall rule that will allow external Minecraft clients to access your server:

  1. Click Management, disks, networking, SSH keys to reveal a set of tabs for advanced settings.
  2. In the Networking tab, add the tag minecraft-server to the Network tags field.

Set up a static IP address for your instance

To be able to forward incoming requests to your instance dependably, your instance will need to have a static IP address. To add a static IP address to your instance:

  1. In the Networking tab, in the Network interfaces section, click Default. The Network interface configuration menu appears.
  2. Click the External IP dropdown and select Create IP address. A dialog pops up:

    Create a new static IP address

  3. Name your IP address mcs-ip.

  4. Click Reserve to create the address.
  5. Click Done to commit your changes and close the Network interface configuration menu.

Add a persistent disk to your instance

Next, you'll attach a persistent disk to your instance. Unlike boot disks, persistent disks are not tied to the life of your Compute Engine instance. For example, if your hosting needs change over time, you can move the disk to a more suitable machine type later.

The specific type of persistent disk you will use in this tutorial is a persistent SSD. This type of persistent disk supports very fast I/O operations, which can help reduce server lag.

To add a persistent disk to your instance:

  1. In the Disks tab, in the Additional disks section, click Add item. This creates a row with three dropdown list boxes.
  2. Click the list box under Name and click Create disk. The Create a disk page appears:

    Create a new disk

  3. In the dialog, fill out the form as follows:

    • Name: minecraft-disk
    • Disk type: SSD Persistent Disk
    • Source type: None (blank disk)
    • Size (GB): 50
  4. Click Create to create the new disk. When you create the instance, the disk will be attached automatically.

Create your instance

That's it for instance configuration! Click the Create button at the bottom of the page to create your new instance. This action will also take you back to the VM instances page.

Format and mount your persistent disk

At this point, your disk is attached to your instance, but it hasn't yet been mounted to the instance. That's okay—if you mounted the disk now, you wouldn't be able to do much with it. That's because, as with any disk, your persistent disk first needs to be formatted with a filesystem that your operating system—in this case, Debian Linux—can understand.

Begin by establishing an SSH connection with your instance. In the row for mc-server on your VM instances page, click SSH to open a browser-based SSH terminal:

SSH terminal

After your SSH terminal opens, create a new directory named minecraft in your instance's home directory:

user@mc-server: sudo mkdir -p /home/minecraft

You'll use this directory as a mount point for your persistent disk.

Next, format your disk:

user@mc-server: sudo mkfs.ext4 -F -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/disk/by-id/google-minecraft-disk

Finally, mount your disk:

user@mc-server: sudo mount -o discard,defaults /dev/disk/by-id/google-minecraft-disk /home/minecraft

Install and run the Minecraft server on your instance

Your persistent disk is officially mounted. Now it's time to do what you came here to do in the first place: install and run your Minecraft server.

Set up the Java Runtime Environment (JRE)

The Minecraft server runs on top of the Java Virtual Machine (JVM), so it requires the Java Runtime Environment (JRE) to run. Because the server doesn't need a graphical user interface, this tutorial uses the headless version of the JRE. This approach reduces the JRE's resource usage on your machine, helping ensure that the Minecraft server has plenty of room to expand its own resource usage if needed.

You'll need to update the Debian repositories on your Debian installation before you can download and install the headless version of the JRE. To do so, run the following command in your SSH terminal:

user@mc-server: sudo apt-get update

After your repositories are updated, you can install the headless JRE:

user@mc-server: sudo apt-get install -y default-jre-headless

Install the Minecraft server

Now that you've set up the JRE, it's time to download and install the Minecraft server. Begin by navigating to your minecraft directory:

user@mc-server: cd /home/minecraft

Because the minecraft directory contains your mounted persistent disk, you're going to need a special level of access known as root user access to run commands on it. Run the following to become the root user:

user@mc-server: sudo su

Next, download the current Minecraft server's Java archive file (JAR) to your instance:

root@mc-server: wget https://s3.amazonaws.com/Minecraft.Download/versions/1.12.2/minecraft_server.1.12.2.jar

Start the server for the first time:

root@mc-server: java -Xms1G -Xmx7G -d64 -jar minecraft_server.1.12.2.jar nogui

The first run is a little anticlimactic: the server simply starts, executes some quick operations, and stops. If you run the following command, however, you'll notice that some new files have been created in the minecraft directory:

root@mc-server: ls -l

Among these new files, you'll find a file called eula.txt. Open this file for editing:

root@mc-server: nano eula.txt

This file contains a single boolean variable, eula. To use the Minecraft server, you must accept the terms of the Minecraft End User License Agreement (EULA). If you accept the terms of the EULA, set the value of eula from false to true, then save and exit.

Configure the Minecraft server

Before you run your Minecraft server, you might want to edit the server's default properties. To do so, you can edit the file server.properties:

$ nano server.properties

For a complete description of each property type and its possible values, see the server.properties page on the Minecraft Wiki.

Run the Minecraft server

If you start the Minecraft server again at this point, it will be tied to the life of your SSH session—that is, if you close your SSH terminal, the server will be terminated as well. To get around this issue, you can use screen, an application that allows you to create a virtual terminal that can be "detached," becoming a background process, or "reattached," becoming a foreground process. When a virtual terminal is detached to the background, it will run whether you are logged in or not.

In your SSH terminal, run the following to install screen:

root@mc-server: apt-get install -y screen

Next, start your Minecraft server in a screen virtual terminal. Use the -S flag to name your terminal mcs:

root@mc-server: screen -S mcs java -Xms1G -Xmx7G -d64 -jar minecraft_server.1.12.2.jar nogui

Detach the screen terminal by pressing Ctrl + a, then typing d. The terminal will continue to run in the background. To reattach the terminal, run screen -r <terminal_name> as follows:

root@mc-server: screen -r mcs

Finally, detach your screen terminal again if needed. Type exit once to leave root user mode, then type exit again to close your SSH connection.

Congratulations! You now have a running Minecraft server. However, it's not quite ready to be shared just yet. Before you can share your server, you need to set up a firewall rule that will allow people to access it.

Allow clients to access the Minecraft server

To be able to forward incoming requests to your instance dependably, you'll need to create a firewall rule. To do so:

  1. In the Cloud Platform Console, go to the VPC networks page.

    Go to the VPC networks page

  2. In the table of available networks, in the Name column, click the default network.

  3. On the VPC network details page, in the Firewall rules tab, click Add firewall rule.

On the Create a firewall rule page, fill out the form as follows:

  • Name: minecraft-rule
  • Target tags: minecraft-server
  • Source filter: IP ranges
  • Source IP ranges: 0.0.0.0/0
  • Protocols or ports: tcp:25565

Click Create to create your new firewall rule. Users can now access your server from their local Minecraft clients.

Schedule regular backups

Whether you're running a local Minecraft client or running a Minecraft server, it's a good idea to back up your Minecraft world data on a regular basis. This section demonstrates how to set up regular backups of your world data using Google Cloud Storage.

Create a backup script

Begin by establishing an SSH connection with your instance from the VM instances page in your Cloud Platform Console. After the terminal opens, become the root user:

user@mc-server: sudo su

Create a new Cloud Storage bucket. You'll use this bucket to store your backups:

root@mc-server: gsutil mb gs://<project_id>-minecraft-backup

Next, create a new shell script file, backup.sh, in your minecraft folder, and open it for editing:

root@mc-server: nano /home/minecraft/backup.sh

Paste the following script into the file. Replace <bucket> with your Google Cloud Storage bucket:

#!/bin/bash
screen -r mcs -X stuff '/save-all\n/save-off\n'
/usr/bin/gsutil cp -R ${BASH_SOURCE%/*}/world gs://<bucket>/$(date "+%Y%m%d-%H%M%S")-world
screen -r mcs -X stuff '/save-on\n'

This script begins by saving the current state of your world data and pausing your server's auto-save functionality. Next, the script backs up your server's world data directory (world), placing its contents in a timestamped directory (<timestamp>-world) in your Google Cloud Storage bucket. After the script finishes backing up the data, it resumes auto-saving on the Minecraft server.

Save and exit, then run the following to make your script executable:

root@mc-server: chmod 755 /home/minecraft/backup.sh

Test the script:

root@mc-server: /home/minecraft/backup.sh

After the script finishes, visit the Storage browser in the Cloud Platform Console and click on your bucket. You should see a timestamped backup of your world directory.

Schedule a cron job

Unless you prefer to manually initialize each backup, you'll probably want to have your script run automatically at predictable intervals. To accomplish this task, you need to schedule a new cron job.

To schedule a cron job, begin by opening the cron table for editing:

root@mc-server: crontab -e

Scroll to the bottom of the file and paste the following line, which specifies that backup.sh will run every 4 hours:

0 */4 * * * /home/minecraft/backup.sh

Save and exit.

That's it! Your Compute Engine instance will now automatically back up your world data to a Google Cloud Storage bucket every 4 hours.

Automatically remove old backups

If you back up your world data every 4 hours, that means you're backing it up 6 times a day, 72 times a week, and roughly 300 times a month. You can remove old backups automatically by using a feature of Cloud Storage called Object Lifecycle Management. This feature allows you to configure your Cloud Storage bucket to automatically archive or delete old backups after a certain time or if there are newer backups available.

To set up your Cloud Storage bucket to remove backups automatically:

  1. Open the Cloud Storage browser in the Cloud Platform Console:

    Go to the Cloud Storage browser

  2. In the bucket list, find your Minecraft backups bucket.

  3. Click None in the bucket's Lifecycle column. The View object lifecycle rules page appears.
  4. Click Add rule.
  5. In the Select object conditions section, select Age. Set the age to 7 days and click Continue.
  6. In the Select action section, select Delete and click Continue.
  7. Click Save to save your settings. You're taken back to the View object lifecycle rules page.

Each backup will now be deleted one week after your backup script sends it to Cloud Storage.

Shut down your Minecraft server

If you don't need to run your Minecraft server, you should shut it down to avoid incurring unnecessary expenses.

Begin by establishing an SSH connection with your instance from the VM instances page in your Cloud Platform Console. After the terminal opens, stop the Minecraft server by passing a \stop command to the screen terminal in which it is running:

user@mc-server: sudo screen -r -X stuff '/stop\n'

Now that you've stopped your Minecraft server, you can safely shut down your instance. On the VM instances page, click on your instance's name and then click the Stop button at the top of the page. You will be logged out of your SSH session.

To start up your instance again, visit your instance page and then click the Start button at the top of the page. To start the Minecraft server again, you can establish an SSH connection with the instance, remount your persistent disk, and start your Minecraft server in a new screen terminal, as described in the Run the Minecraft server section.

Automate start up and shutdown procedures

If you plan to shut down your server regularly, consider adding startup and shutdown scripts to your instance to automate common startup and shutdown procedures. You can automate the startup procedure as follows:

  1. Go to the VM instances page in the Cloud Platform Console:

    VM instances page

  2. Click your instance name.

  3. Click Edit.
  4. In the Custom metadata section, add a new key called startup-script and copy the following script into its Value field:

    #!/bin/bash
    mount /dev/disk/by-id/google-minecraft-disk /home/minecraft
    (crontab -l ; echo "0 */4 * * * /home/minecraft/backup.sh")| crontab -
    cd /home/minecraft
    screen -d -m -S mcs java -Xms1G -Xmx7G -d64 -jar minecraft_server.1.12.2.jar nogui
    

When you restart your instance, this script will automatically mount your Minecraft disk to the appropriate directory, reinstall your cron job, start your Minecraft server in a screen session, and detach the session.

To automate your shutdown procedure, add another key called shutdown-script and copy the following into its Value field:

#!/bin/bash
sudo screen -r -X stuff '/stop\n'

At the bottom of the page, click Save to commit your changes. When you stop your instance, this script will shut down your Minecraft server before the instance shuts down.

What's next

Visit the Minecraft Wiki

The Minecraft Wiki provides tons of useful resources for Minecraft players and Minecraft server adminstrators alike.

Try an alternative Minecraft server

The vanilla Minecraft server you installed in this tutorial is just one of the many Minecraft servers available. Visit Custom servers in the Minecraft Wiki to see a list of alternative servers that have been optimized for game types, machine requirements, easy modifications, and more.

Try other tutorials

Try out other Google Cloud Platform features for yourself. Have a look at our tutorials.

Monitor your resources on the go

Get the Google Cloud Console app to help you manage your projects.

Send feedback about...