Thursday, 5 September 2013

Raspberry Pi - Setup Minecraft Server

This is my 'recipe' for setting up a minecraft server on a raspberry pi.  I used information I found in the following pages to setup my minecraft server, http://wiki.bukkit.org/Setting_up_a_serverhttp://picraftbukkit.webs.com/pi-minecraft-server-how-tohttp://www.raspberrypi.org/archives/4621.

My server is 'usually' up, so if you want to check it out before setting up your own, open up Minecraft and connect to martinohanlon.servebeer.com:25565.


Setting up the Pi
I amend the Pi config through raspi-config to give it the most amount of memory and overclock it.

sudo raspi-config

Options:
- Overclock, Ok, Medium, Ok
- Advanced Options, Memory Split, Change to 16, Ok

Choose Yes to reboot

Static IP address
This isn't essential but I find it a lot easier to manage the minecraft pi server if its got a static IP address; its easier to connect to as the IP address never changes and if you want to make it public it makes port forwarding simpler too.

See this post for details on how to give your Raspberry Pi a static IP address.

Install Java
Java isn't installed on the Pi, so this is the first step:

cd ~

wget --no-check-certificate http://www.java.net/download/jdk8/archive/b102/binaries/jdk-8-ea-b102-linux-arm-vfp-hflt-07_aug_2013.tar.gz

mkdir -p /opt

sudo tar zxvf jdk-8-ea-b102-linux-arm-vfp-hflt-07_aug_2013.tar.gz -C /opt

Check java is installed properly by running:

sudo /opt/jdk1.8.0/bin/java -version

If it hasn't returned an error, so far so good.

Install Minecraft Server
I installed the bukkit server variant, spigot, I found it to be the best for reliability and performance on the Pi.

mkdir minecraft_server

cd minecraft_server

wget http://ci.md-5.net/job/Spigot/lastStableBuild/artifact/Spigot-Server/target/spigot.jar

Run Minecraft Server
To run the minecraft server I create a bash script called start.sh:

nano start.sh

Cut and paste the following command into the start.sh file

/opt/jdk1.8.0/bin/java -Xms256M -Xmx496M -jar /home/pi/minecraft_server/spigot.jar nogui

Note - I never got the minecraft server working reliably on a 256 meg Raspberry Pi, you may have better results that me though, so if you have a 256 meg Pi use the following command instead of the one above.

/opt/jdk1.8.0/bin/java -Xms128M -Xmx256M -jar /home/pi/minecraft_server/spigot.jar nogui

Ctrl X to save

Make the script executable

chmod +x start.sh

Start-up the server

./start.sh

The first time the server runs it will install the server from the spigot.jar file and create the world.  This is going to take a little while the first time, but the next time it starts up, it will be much quicker.

Once it has finished and displayed the message Done, stop the server so it be can configured.

To stop the server type the command:

stop

Configuring the Server
As part of the install process a file called server.properties will be created in the ~/minecraft-server directory, this holds the key configuration for how the server runs.  See this page for full details on the server.properties configuration file.

The most important setting you need to change is the view-distance, when set to the default of 10, I found that the server was very unstable and prone to crashing.

nano server.properties

These are the common flags I change:

allow-flight=false - change to true if you want to allow users to fly

gamemode=0 - change to 1 if you want to have creative mode, rather than survival

max-players=20 - 20 maybe too much for the Pi to handle, I set a maximum of 5

spawn-monsters=true - set to false to turn off monsters

spawn-animals=true - set to false to turn off animals

view-distance=10 - this is the distance in chunks the player can see, I set this value to 4, to reduce load on the Pi

motd=A Minecraft Server - "message of the day", is displayed when people join the server

Ctrl X to save

In order to change the view-distance, you also need to modify the spigot.yml file:

nano spigot.yml

Scroll down till you find:

world-setting:
  default:
    view-distance: 10

Modify this value to your new view-distance.

Ctrl X to save

Installing plugins
I use 2 plugins on my server, NoSpawnChunks to improve performance and Raspberry Juice which allows you to run programs created using the Minecraft: Pi Edition api on your Minecraft server.

cd ~/minecraft_server/plugins

wget http://dev.bukkit.org/media/files/586/974/NoSpawnChunks.jar

wget http://dev.bukkit.org/media/files/675/691/raspberryjuice-1.2.jar

Start up the Server
Its time to start up the server and give yourself 'op', making yourself the operator, you leave your server open if there is no operator

cd ~/minecraft_server

./start.sh

Once the server has started up and reported "Done", you can give yourself 'op' by typing the following command:

op <yourusername>

e.g. op martinohanlon (but don't give me op - I can't be trusted!)

Your done, your server is running, you can login from minecraft using the address <ipofyourpi>:25565

Using screen to Run the Server
One of the problems with this setup so far, is if you started up the server directly on the Pi, you now cant do anything else with it, or if you have started it up over SSH you can't disconnect otherwise the server will stop.  I use a utility called 'screen' to open multiple terminal sessions, which you can 'detached' from and they keep on running even when you disconnect.

Install screen

sudo apt-get install screen

To use screen, just type:

screen

This opens a new terminal window in a terminal window, cool eh!  Anything you run in this screen is separate from you main terminal, you can exit screen using the exit command, or you can detached the screen using Ctrl A, then D, which takes you back to your original terminal window but leaves the screen running.

See this page for a Tutorial on how to use screen.

You can then view all the detached screens by using the command:

screen -ls

Which will show you information like this:

There are screens on:
        3158.pts-0.minepi       (03/09/13 22:08:32)     (Detached)
        3064.pts-0.minepi       (03/09/13 22:04:05)     (Attached)
2 Sockets in /var/run/screen/S-pi.

Showing there are 2 screens running at the moment, to reconnect to a screen type, screen -r <name of screen>:

screen -r 3064.pts-0.minepi

Open a new screen,  run ~/minecraft-server/start.sh and use Ctrl A, then D to detach and your minecraft server will be running in the background

screen
~/minecraft-server/start.sh
Ctrl A, D

Running Minecraft:Pi Edition programs
I setup my minecraft server originally to show people what you could do with the Minecraft: Pi edition's API, , even though the raspberry juice plugin doesn't support all of the API functions, you can run most of the same programs on your server as you would on the minecraft Pi edition.  If you want to try it out, the minecraft clock I created works really well.  Startup the server, download the code and run it.

sudo apt-get install git-core
cd ~
git clone https://github.com/martinohanlon/minecraft-clock.git
cd minecraft-clock
python minecraft-clock.py



83 comments:

  1. I've gone on that server quite a lot... No one is ever on :(

    ReplyDelete
  2. O dear, pass on the address if ypu want that way people will be.

    If you want me to run one of your programs on it just let me know.

    ReplyDelete
    Replies
    1. My programs?? That would be cool... I don't know, but can you program plugins? Or is possible to make it so when you go onto a certain spot, it would start my arena game... I might re write it for that! Thanks :) I'll see what I can do.

      Delete
    2. Mind if you used my world? Cause the world your server has is kinda rough. This one is in the plains, and I made a "machine" that starts the program. You'll have to run the script first though.
      World: http://1371.pancakeapps.com/world.zip
      Script: http://1371.pancakeapps.com/server.py
      Thanks! When you press the button, it will randomly choose between the Arena game, and Dodgegame.

      Delete
    3. I see you generated a giant Raspberry Pi.

      Delete
    4. I did, I wrote a program which converts 3d models (obj files) into minecraft

      Delete
    5. I guess you made it so it runs that program in a loop.

      Delete
    6. Just had a look through your program Nicholas. Ill update the server at some point this week to run your program.

      However, one piece of advice, comments in code are a great thing. They are really useful in describing why your code is doing what its doing, not only for other people, but for yourself looking back at why you did something. I really struggled to work out what your program was doing.

      Delete
    7. Okay, I've updated the file with high scores, and comments all over. :) You can use the same download link.

      Delete
    8. well check you out.. That is much better, thank you

      Delete
  3. i just tryed to join but it wouldn't let me. any help

    ReplyDelete
    Replies
    1. Sorry dude, pi was momentarily off due to knackered power supply, back online now.

      Delete
  4. Thanks you for the tutorial. For some reason the minecraft pi application on the desktop can't find the pi minecraft server. Any suggestions?

    ReplyDelete
  5. Hi,
    I would like to know if with this procedure I can connect the minecraft pi edition to the the spigot.jar server .
    Thx!

    ReplyDelete
    Replies
    1. No worry, spigot is a server for the full version of minecraft not the pi edition.

      Delete
  6. Martin, I'm getting an error when I try to check to see that the Java server is running properly using the following command line you included in your instructions:

    sudo /opt/jdk1.8.0/bin/java -version

    ReplyDelete
    Replies
    1. ...btw, the error message is as follows

      sudo: /opt/jdk1.8.0/bin/java: command not found

      Delete
    2. I suspect java hasnt been installed properly / at all, if your using a newer version of raspbian java may already be installed type "java -version" to see. Its an older version 1.7 but it should be ok.

      Delete
    3. This Happened to me, make sure you did -C when you ran tar otherwise it will be extracted to who knows where not /opt.

      Delete
  7. JAVA is included in the new 2013-09-25-wheezy-raspbian.zip which gives me some headegg

    ReplyDelete
    Replies
    1. The solution was to type: sudo java -Xms256M -Xmx496M -jar /home/pi/spigot.jar nogui

      Delete
  8. If anyone know how to make the Pi launch the ./Start.sh – when the Pi is restarting or booting – I would like your help :o)

    ReplyDelete
    Replies
    1. I set up a server for my son. After 2 days of screwing with this I have a scheme that works. I am sure there is a better method, but this worked for me ....
      Simple short answer: use -d -m options for screen.
      Longer explanation: I put a script in /etc/init.d called mcstart.sh
      It had the following code:
      #!/bin/bash
      sudo echo hello3 > /home/pi/debug.txt
      screen -d -m /home/pi/mc.sh
      The second line was debugging so I could look at the file 'debug.txt and see that my script had indeed run.
      While in the /etc/init.d directory I did the following command, because my script was never being run.
      sudo update-rc.d mcstart.sh defaults

      Now in the /home/pi directory the file mc.sh had:
      #!/bin/sh
      cd /
      sudo /opt/jdk1.8.0/bin/java -Xms256M -Xmx496M -jar /home/pi/spigot.jar nogui

      For some reason if I was not in the root (aka / ) directory, the process would hang when dealing with the plugins. Why no clue. Is this the best, no, but it works and I can go onto something else, and my son can have fun. ... Oh wait now I have to configure the external access...

      Delete
    2. There is a tutorial for running a program at startup using init.d here http://www.stuffaboutcode.com/2012/06/raspberry-pi-run-program-at-start-up.html

      Delete
  9. gzip: stdin: unexpected end of file
    tar: Unexpected EOF in archive
    tar: Unexpected EOF in archive
    tar: Error is not recoverable: exiting now


    This is what i get when i try to install java. Some please help me!

    ReplyDelete
    Replies
    1. I suspect the java install file hasnt been downloaded properly / at all. Is you Pi connected to the internet? Try deleting the file with rm ~/zxvf jdk-8-ea-b102-linux-arm-vfp-hflt-07_aug_2013.tar.gz and starting "Install Java" again.

      Alternatively if your using a recent version of raspbian you may already java installed, type "java -version", if it comes back with a version number, it is. Its an older version 1.7, rather than 1.8 but I doubt it will matter.

      Delete
    2. Thank you so much Martin!!!

      Delete
    3. Martin i got another error when trying to type ./start.sh it says this: Error: Unable to access jarfile /home/pi/spigot.jar

      Delete
    4. Please help me some one :(

      Delete
    5. Nevermind, i got my server up and running :)

      Delete
    6. I also get this error how do you resolve?

      Delete
    7. Hi Olly, Which error are you getting?

      Delete
    8. I am getting the error "Unable to access jarfile /home/pi/spigot.jar"
      Thanks for prompt reply

      Delete
    9. Would you look at that! There was an error in my post.. It should have read:

      /opt/jdk1.8.0/bin/java -Xms256M -Xmx496M -jar /home/pi/minecraft_server/spigot.jar nogui

      Edit your start.sh file with nano to include the correct path of /home/pi/minecraft_server/spigot.jar rather than /home/pi/spigot.jar

      Delete
  10. wow i thought i was the only fool playing with this lol...
    i've got a better startup script if you all would like it lol

    ReplyDelete
    Replies
    1. https://www.mediafire.com/?hfva5w5lewhtav6 Place this in your /etc/init.d/ folder and make sure to sudo nano /etc/init.d/minecraft and change the MCPATH= (to your spigot.jar dir) also you can set your maxheap and minheap and lots more.... then you can use, sudo service minecraft start/stop/backup/update......read the log script for all it does...but note you will need to update the info for UPDATE and BACKUP if you want to use them..enjoy guys. also my server is kicker22004.no-ip.biz:25567 if it kicks you relog in...poor pi working hard lol

      Delete
  11. Now that java (1.7) is included in raspbian, any thoughts on which version is better for minecraft?

    ReplyDelete
    Replies
    1. No thoughts on which would be better, sorry... Presumably there are some improvements in 1.8 over 1.7, BUT, I wouldn't worry about it.

      Delete
    2. Tried 1.8 (build 128) and that actually seems a little better

      Delete
  12. First, thank you for this. It really helped me get Minecraft working with our Pi. What we want to do is run a multiplayer server with Raspberry Juice so friends can come together to run scripts they have created in Python. I can't figure out how to give anyone else access to running scripts other than me SSH'd into our own pi. Is there a way? Please help - we are very confused! Thank you!

    ReplyDelete
    Replies
    1. Multiple people can connect to the minecraft server and the raspberry juice plugin. Its pretty simple, when you create the connection to minecraft in your python program with mc = minecraft.Minecraft.create() just put the IP address of your server in i.e. mc = minecraft.Minecraft.create("192.168.1.99") where "192.168.1.99" is the address of your pi.

      Delete
    2. Thank you for this... I am further ahead, but still not quite there. I didn't realize you had to enter the server info in the code, so that is a huge help. I apologize but I am trying to figure this out to help a group of children who want to use this to learn to code Python.... Our setup is that we have a Pi, and Spigot with Raspberry Juice working and the server is running. What we hope to be able to do is connect with regular Windows PCs and run scripts on the Spigot server on the Pi. I do have Python 2 and have the tnt snake script ready to try with our IP/port, and the port is set to 4711 and is open.

      Where I am stalled is if I am on a Windows computer with my saved .py script how do I run it to execute on the server? When I ran it here I got the following error:

      import mcpi.minecraft as minecraft
      ImportError: No module named mcpi.minecraft

      This happens when it tries to import the minecraft.py module from the minecraft directory. Is there any way to accomplish this with the setup we have? It seems that we are missing a pretty big step somewhere... ?? Thank you again so much!

      Delete
    3. Hi,

      You dont have to execute your script on the server, you can execute it from the PC, but you will need the mcpi folder which contains the minecraft.py file.

      So on your PC, create your myprogram.py program in a folder which also has the mcpi folder in it.

      When you call mc = minecraft.Minecraft.create()

      use mc.minecraft.Minecraft.create("192.168.1.99")

      passing the IP address of the Pi rather than "192.168.1.99". It might also be worth looking at RaspberryJuice1.3 which has an upgrade to allow multiplayer support so you can do

      mc = minecraft.Minecraft.create("ipaddress", 4711, playername)

      that way you can specify which player you want to use.

      Delete
  13. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  14. Hi, this is a great tutorial and I did pretty much everything step by step and my server works great for me. The problem is that whenever one of my friends try to find the server, they cant log on. Any ideas? Thanks

    ReplyDelete
    Replies
    1. Are you friends accessing from outside your network, i.e. Over the internet?

      Have you forwarded port 25565 on your router to point to your Pi?

      Delete
  15. Yes, they are trying to access outside of my network. I'm not sure what you mean by forwarding the port. Im pretty basic with my knowledge of programming and routers and that stuff. Thanks for replying so quickly!

    ReplyDelete
    Replies
    1. In order for people to connect to your server over the internet you need to tell your router where to send the traffic, this is port forwarding, check put portforward.com they have guides for most routers.

      You will also need to know your external ip address, this is the address you have on the internet, your router will also be able to tell you this, but if you google 'external ip address' it will come up.

      When you have done this, you friends should be able to connect to externalip:25565.

      Delete
    2. Thanks so much, I will try it out and let you know

      Delete
    3. When I googled my external IP address, it came up like this:
      2602:304:b3f3:6c49:85b4:6fd3:35b4:a14d

      I also attempted to make the Pi's IP address static with the link you included earlier and now my computer wont connect through Putty

      Delete
  16. I have it setup successfully on my local network, but I can't get it where other people from other networks can join. I have a Belkin N150 router and a static IP on the Pi. I've gone into the router settings and enabled 25565 on both TCP UDP and added my Pi's static IP. I've tested the port and for TCP it says "Your port is open on another device." I've given some friends the IP (example: 192.168.2.8:25565) and they are unable to join. I'm baffled. I've also tried setting the server IP in the server.properties file on the Pi. I don't know what else to do. Please help.

    Thanks

    ReplyDelete
    Replies
    1. You need to give your friends your internet ip address, not the local ip address of your pi. When your friends connect they need the ip address that your router has on the internet, your router will then forward this traffic to the internal ip address of your pi. Hope that makes sense.

      You might also want to sign up for a noip account or similar as your internet ip address changes from time to time see this link for info http://www.stuffaboutcode.com/2012/06/raspberry-pi-access-from-internet-using.html

      Delete
  17. whenever I try to get on to minecraft it just says'* failed to add service - already in use?' can you help?

    ReplyDelete
    Replies
    1. Do you mean when you try and connect to the server from Minecraft you get the error? Its not one I've seen before. If you can offer any more information that might help.

      Delete
    2. I might be able to help... Where are you getting this? When you try to join the RPi server?

      Delete
    3. Don't worry Iv'e sorted it out, the memory split was to low but how do you actually get onto the server where do you type ':25565' do you type it into the terminal? please reply.

      Delete
    4. Load up Minecraft on your PC, go Multiplayer, pick Direct Connect and enter the IP address of your Pi + ":25565" e.g. 192.168.1.10:25565. You can find out the IP address of your Pi, by entering ifconfig in the terminal.

      Delete
  18. how do you get on multiplayer? it just says start game or join game on my start screen do i click join game?

    ReplyDelete
    Replies
    1. This sets up a minecraft server for the pc version of minecraft not the pi version.

      Delete
    2. oh so i cant connect to other people with pi's is this only for if you want to connect your pi to a pc because I dont have Pc minecraft

      Delete
  19. i keep getting a screen saying permission denied help please

    ReplyDelete
    Replies
    1. You should probably use 'sudo' before the command to run the server.

      Delete
    2. Some more information would be useful. What are you doing when you get 'permission denied'?

      Nicholas is right tho, you could just be missing a sudo at the start of the command.

      Delete
  20. This comment has been removed by the author.

    ReplyDelete
  21. We have been running the Minecraft server (Spigot) on our pi for a while now. We would like to start programming with the API. Do we have to install Minecraft PI edition? Should I get a second memory card or just install along with the spigot server?

    Thank you for all of hte information, videos etc. My 9 year old is really excited to start programming!

    ReplyDelete
    Replies
    1. You should be able to install it on the same sd card, but you will have to the Memory Split on the Pi back to the default, as the Minecraft:Pi Edition will need the video memory to run.

      Delete
  22. Thank you. I managed to get it going. Now if only I could run it via SSH from my notebook. Haha.

    ReplyDelete
  23. This is an error I get when I start it :Error: Unable to access jarfile /home/pi/minecraft_server/spigot.jar

    ReplyDelete
    Replies
    1. Did the spigot.jar file download properly? Have you looked in the /home/pi/minecraft_server directory? Is it in there?

      Delete
    2. ok it took me awhile to do it and i finally did it so im just trying to come up w an ip

      Delete
  24. This comment has been removed by the author.

    ReplyDelete
  25. nevermind of the ip its just that when i login to the server it gives me another error "Connection Lost Java read timed out" and when i check my rpi it says a bunch errors and ends the server. Help if you can.

    ReplyDelete
    Replies
    1. What are the errors? Did you get the viewdistance in server.properties and spigot.yml? I found the server crashed if the view distance was too high.

      Delete
  26. I don't really know cause there are like 20 of em. I will check the veiw distance to make sure

    ReplyDelete
  27. Also when I try to restart instead it just stops the server.

    ReplyDelete
  28. please help am getting this error when trying to run the start file
    ./start.sh: 1: ./start.sh: /opt/jdk1.8.0/bin/java: not found
    ./start.sh: 2: ./start.sh: /home/pi/minecraft_server/spigot.jar: Permission denied

    ReplyDelete
    Replies
    1. The error suggests java didnt download or extract properly. Re-run the Install Java commands and see if you get an errors.

      Delete
  29. I'm about to get a pi of my own, and I was wondering what the limitations are here. Do you think I could, say, create my own mobs and code them in, as well as my own items and their effects on the world? This would stimulate a loott of ideas haha.

    ReplyDelete
  30. Spigot seems to be unavailable now

    http://www.spigotmc.org/threads/our-dmca-response.28772/

    ReplyDelete
    Replies
    1. I know, it has put something of a spanner in the works of Raspberry Pi hosted minecraft servers. If you look around though Im sure you will find a copy of spigot from other sources.

      Delete