How to Install a Minecraft Server on Ubuntu 22.04 VPS
This guide will walk you through the process of setting up a Minecraft server on an Ubuntu 22.04 VPS. It includes every command you'll need, with detailed instructions for each step.
Prerequisites
- A VPS running Ubuntu 22.04
- A user with sudo privileges
- Basic knowledge of terminal commands
Step 1: Update and Upgrade Your System
First, ensure your system is up-to-date:
sudo apt update && sudo apt upgrade -y
Step 2: Install Java
Minecraft requires Java to run. Install OpenJDK 17, which is the recommended version for Minecraft servers:
sudo apt install openjdk-17-jre-headless -y
Verify the installation:
java -version
You should see output similar to `openjdk version "17.0.x"`.
Step 3: Create a Directory for Minecraft Server
Create a directory where your Minecraft server files will be stored:
mkdir ~/minecraft
cd ~/minecraft
Step 4: Download Minecraft Server
Download the latest version of the Minecraft server from the official website:
wget https://launcher.mojang.com/v1/objects/e3b2d9e4d27b20a83e1ab2cc36a521b9940c0ad8/server.jar -O server.jar
Step 5: Run the Minecraft Server
Start the Minecraft server to generate the necessary configuration files:
java -Xmx1024M -Xms1024M -jar server.jar nogui
The server will start and then stop, prompting you to accept the EULA.
Step 6: Accept the EULA
To run the Minecraft server, you need to accept the End User License Agreement (EULA). Follow these steps to accept the EULA:
- Open the EULA file using a text editor such as nano:
- In the nano editor, you will see the following line:
- Change this line to:
- To save and close the file in nano:
- Press CTRL + X to begin the process of exiting nano.
- Nano will ask if you want to save the modified buffer. Press Y to confirm that you want to save the changes.
- Nano will then ask for the file name to write. Press Enter to confirm the current file name.
nano eula.txt
eula=false
eula=true
After performing these steps, the changes will be saved, and you will have accepted the EULA. Now, you can proceed to start the Minecraft server.
Step 7: Configure the Minecraft Server
Edit the server.properties
file to customize your Minecraft server settings:
nano server.properties
Adjust the settings according to your preferences. Common settings to change include:
-
motd
: The message of the day displayed in the server list. -
max-players
: Maximum number of players that can join. -
spawn-protection
: Radius of spawn protection. -
online-mode
: Set totrue
to enable player authentication through Mojang's servers.
After making your changes, save and close the file.
Step 8: Start the Minecraft Server
Now that you have accepted the EULA, you can start your Minecraft server. Additionally, you can adjust the amount of RAM allocated to the server based on your VPS's available resources. Follow these steps:
Setting the Amount of RAM
The -Xmx
and -Xms
flags are used to specify the maximum and initial heap size for the Java Virtual Machine (JVM). Here's what they mean:
-
-Xmx
: Sets the maximum heap size. This is the maximum amount of RAM that the server will use. -
-Xms
: Sets the initial heap size. This is the amount of RAM that the server will use on startup.
For example, to allocate 2GB of RAM to the server, you would use:
java -Xmx2048M -Xms2048M -jar server.jar nogui
To allocate 4GB of RAM, you would use:
java -Xmx4096M -Xms4096M -jar server.jar nogui
Adjust the values based on your VPS's available RAM and the needs of your server.
Running the Server
Run the server with the desired amount of RAM:
java -Xmx1024M -Xms1024M -jar server.jar nogui
Using screen
To keep the server running even after you log out, you can use screen
or tmux
. Here, we'll use screen
:
Install screen
if it’s not already installed:
sudo apt install screen -y
Start a new screen session and run the server:
screen -S minecraft
java -Xmx1024M -Xms1024M -jar server.jar nogui
To detach from the screen session, press Ctrl + A, then D.
To reattach to the screen session, use:
screen -r minecraft
Step 9: Allow Minecraft Port in Firewall
Allow the default Minecraft port (25565) through the firewall:
sudo ufw allow 25565
Ensure the firewall is enabled:
sudo ufw enable
Step 10: Managing Your Minecraft Server
To stop the server, enter the server console and type:
stop
To automate server startup, you can create a systemd service.
Step 11: Create a Systemd Service
Create a new service file for the Minecraft server:
sudo nano /etc/systemd/system/minecraft.service
Add the following content to the file:
[Unit]
Description=Minecraft Server
After=network.target
[Service]
User=your-username
WorkingDirectory=/home/your-username/minecraft
ExecStart=/usr/bin/java -Xmx1024M -Xms1024M -jar server.jar nogui
Restart=on-failure
[Install]
WantedBy=multi-user.target
Replace your-username
with your actual username.
Save and close the file, then reload the systemd daemon:
sudo systemctl daemon-reload
Start the Minecraft server with systemd:
sudo systemctl start minecraft
Enable the Minecraft server to start on boot:
sudo systemctl enable minecraft
Step 12: Monitor the Server
Check the status of your Minecraft server:
sudo systemctl status minecraft
Step 13: Backing Up Your Server
Regular backups are essential. You can create a simple backup script:
Create a backup directory:
mkdir ~/minecraft/backups
Create a backup script:
nano ~/minecraft/backup.sh
Add the following content:
#!/bin/bash
tar -cvpzf ~/minecraft/backups/minecraft_backup_$(date +%Y%m%d_%H%M%S).tar.gz ~/minecraft
Make the script executable:
chmod +x ~/minecraft/backup.sh
You can automate the backup process using cron:
crontab -e
Add the following line to schedule a daily backup at 2 AM:
0 2 * * * /home/your-username/minecraft/backup.sh
Replace your-username
with your actual username.
Conclusion
By following these steps, you will have a fully functional Minecraft server running on your Ubuntu 22.04 VPS. This setup ensures a stable, secure, and high-performance environment for you and your friends to enjoy Minecraft.