Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save husjon/c5225997eb9798d38db9f2fca98891ef to your computer and use it in GitHub Desktop.
Save husjon/c5225997eb9798d38db9f2fca98891ef to your computer and use it in GitHub Desktop.
Script and Instructions to setup a Valheim Dedicated Server using Oracle Cloud Infrastructure

Table of Content

⚠️ Disclaimer ⚠️

This install script has now been updated with a configuration for box64 which has been tested to work for a few weeks and by different people using this guide.
If you have made any changes to the ~/.box64rc configuration file for the [valheim_server.x86_64] section, please remove it and run the setup script.
If you should experience any issues, please leave a comment.

This script / procedure for setting up a Valheim server on ARM is currently broken as of 7th of November 2023. (https://www.valheimgame.com/news/patch-0-217-28/)

This unfortunately also includes the rollback procedure. Check comments for any updates regarding this issue.

Credit

The original guide is from Reddit user That_Conversation_91 on r/Valheim.
Original post can be found here

Instructions

Note: For this guide a free account is required on the Oracle Cloud Infrastructure allowing us to spin up a server which is decently specced.

In this guide, we'll:

  • Set up a Virtual Machine using the Oracle Cloud Infrastructure, including firewall rules
  • Prepare and use SSH to connect to the Virtual Machine
  • Update the Operating System and install the Valheim Dedicated Server software
  • Finally we'll use Systemd to allow the server to run automatically.

If you have the knowledge of setting up a server on an alternative cloud provider or your own hardware you may skip ahead to Installing the Valheim Dedicated Server

Pre-requisite

Windows

To connect to the server in the section Connecting to the VM Instance we need to do some preparation.

  1. First of all we need an SSH client, namely Putty
  2. Head on over to https://www.putty.org/ and click on the Download PuTTY link
  3. Scroll down to Alternative binary files
    1. click on putty.exe (64-bit x86)
    2. next scroll down and you'll find puttygen click on puttygen.exe (64-bit x86)
  4. Open up puttygen
    1. Press Generate
    2. Copy the whole SSH key starting at ssh-rsa, we'll need this in the next section when Creating the VM Instance
    3. Press Save public key and save it to f.ex your Desktop
    4. Press Save private key and save it to f.ex your Desktop
      It will ask about password protecting the key, this isn't necessary for this setup.

Mac / Linux

Verify that we have a set of SSH key pairs

  1. Open a terminal
  2. Run the command ls -l ~/.ssh/.
    If you see the files id_rsa and id_rsa.pub, you can continue on with OCI (Oracle Cloud Infrastructure)
  3. If you did not see these files, you can run the command ssh-keygen -N '' -f ~/.ssh/id_rsa Now you can re-run the command from step 2 and you should see both files.

OCI (Oracle Cloud Infrastructure)

  1. Head on over to https://cloud.oracle.com/ to sign up for a free account.
    After logging in you will be shown a Get Started page.
    All subsequent section starts from Getting Started.

Creating the VM instance

  1. From the Getting Started dashboard, scroll down a bit and click the Create a VM instance
  2. On the right hand side of Image and shape click Edit
    1. Click Change image and choose a Canonical Ubuntu Minimal aarch64 and confirm with the Select image button.
      Note: make sure you select aarch64 which is aimed at ARM server
    2. Click Change shape and set the following:
      • Instance type: Virtual machine
      • Shape series: Ampere
      • Shape: VM.Standard.A1.Flex
      • OCPUs: 4
      • Memory: 24GB
  3. On the right hand side of Networking click Edit
    1. Select Create new virtual cloud network and leave the values as is.
  4. On the right hand side of Add SSH keys click Edit
    • If you're f.ex on Linux or Mac you can find your SSH keys under ~/.ssh/id_rsa.pub In this case you can select Upload public key files (.pub) then navigate to ~/.ssh/id_rsa.pub
    • For Windows, the SSH public key we copied in Pre-requisite for Windows can be pasted in by under Paste public keys
  5. Click Create.
    Note: If you get a warning about Out of Capacity, scroll up to the Placement section and try another Domain (AD 1, AD 2 or AD 3), and try again.
    This will take a couple of minutes while the instance is being provisioned / set up.
    While we wait for it we'll go back to the dashboard and set up the networking.
    Click on the ORACLE Cloud header or click here to go back to the Getting started page.

Configuring the Network and firewall rules

  1. At the top of the Getting started page, click on Dashboard
  2. Under Resource explorer, click Virtual Cloud Networks, then click the network (f.ex vcn-20221120-1500)
  3. On the left hand side, click on Security Lists, then the Default Security List for NETWORKNAME
  4. We will be creating a rule so that we can connect to the server from Valheim.
    1. Under Ingress Rules, click Add Ingress Rules:
      • Source Type: CIDR
      • Source CIDR: 0.0.0.0/0
      • IP Protocol: UDP
      • Source Port Range: All
      • Destination Port Range 2456-2459
  5. Click on the ORACLE Cloud header or click here to go back to the Getting started page.
  6. Navigate back to the Dashboard, under the Resource explorer click on Instances then the instance (f.ex instance-20221120-1503)
  7. On the right hand side you'll see Instance access, click Copy to the right of Public IP address, we need this in the next step.

Connecting to the VM Instance

The IP Address we copied in the previous step will be referenced here as IP_ADDRESS

Windows

  1. Start putty which we downloaded in Pre-requisite for Windows
  2. We'll configure the following parameters:
    • Host Name (or IP address): IP_ADDRESS
    • Port: 22
    • Saved Sessions: Valheim Server
    • Close window on exit: Never
    • Click Save
  3. Next in the navigation tree to the left go to Connection > SSH > Auth > Credentials
    1. Under Private key file for authentication click Browse... and navigate to the Private key we saved using puttygen
  4. Go back up in the navigation tree to Session and click Save
  5. Then click Open
  6. You should within a couple of seconds see a prompt along the lines of ubuntu@instance-20221120-1503:~$
  7. You're now good to go to Installing the Valheim Dedicated Server

Mac / Linux

  1. On Mac and Linux we already have an SSH client installed.
  2. Open up a terminal then execute ssh ubuntu@IP_ADDRESS
  3. You should within a couple of seconds see a prompt along the lines of ubuntu@instance-20221120-1503:~$
  4. You're now good to go to Installing the Valheim Dedicated Server

Installing the Valheim Dedicated Server

  1. Run the following command:

    wget https://gist.githubusercontent.com/husjon/c5225997eb9798d38db9f2fca98891ef/raw/setup_valheim_server.sh
    

    This will download the installation script onto your server allowing it to set up everything which is needed.

  2. Then run the following command:

    bash ./setup_valheim_server.sh

    The first time this is run, the setup script will update the operating system and then reboot. If you get notification about kernel upgrades, or restarting services you may just press Enter, allowing the default values be. After it's done you will be disconnected, wait about 15-20 seconds then reconnect to the server.

  3. Run the same command again

    bash ./setup_valheim_server.sh

    Note: If you're playing on Console or Xbox Game Pass / Microsoft Game Pass, please enable Crossplay.
    Also, read the Crossplay section.

    The installation will take a couple of minutes to complete as the script installs all the necessary packages and set up the server with initial values.
    Once it finishes it let you know that we need to make a small edit to one file then start the server.

Configuring the Valheim Server

  1. Open up the server_credentials file with nano ~/server_credentials (or text editor of choice)
  2. Adjust the SERVER_NAME, WORLD_NAME PASSWORD, PUBLIC as you see fit.
    Note: The setup script populated the password field automatically with a random decently strong password.
  3. When done, Press Ctrl+X, then y and finally Enter.
    Note: Mac users might need to use the Cmd button instead of Ctrl

Starting the Valheim Server

To start the server, run the command valheim_server start
This will take a couple of minutes as the world is being generated.

From within the game, it might not show in the Select Server list, instead click the Add server button and type in the address IP_ADDRESS:2456 (Using the IP address fromConfiguring the Network and firewall rules)

More information can be found in the attached Readme.md file and can be viewed with cat ~/Readme.md

Updating the Valheim Server

Whenever the Valheim client updates, the server also needs to be updated.
To do this, log onto the VM then run the command valheim_server update
This will stop the running server and update the server files.
Once done, you must start the server using valheim_server update

Crossplay (Console / Game Pass)

Note: Crossplay on ARM architecture is currently experimental (thanks to @bitdo1).

During setup you will be asked if crossplay should be enabled or disabled.

If you'd like to enable / disable this after the first setup, you can change it by rerunning the setup script. You will need to restart the server for this to take effect using valheim_server restart

After crossplay has been enabled, the join procedure is the same as normal using IP:port, however you can now also join by using a 6 digit code which can be found in the logs after the server has started (using the valheim_server logs-live command).
Example log message:
Session "My Valheim server" with join code 295265 and IP 12.34.56.78:2456 is active with 0 player(s)

Note: Do keep in mind that the join code will change every time the server is restarted!

Modding

BepInEx currently do not support ARM, hence modding currently seem to not be possible.
If this changes in the future, this section will be updated to reflect that.
An issue has been raised with BepInEx and can be found here BepInEx/BepInEx#336

PS: If you're willing to try to install mods on your ARM instance and are able to so successfully, please do let me know.

As for a guide to install mods, here is one.
https://www.youtube.com/watch?v=h2t9cSFidt0

Installer Self-update

The setup_valheim_server.sh now has a self-update feature which allow it to update itself and apply any bugfixes that should be necessary whenever the script is run.

After updating, it will show what have changed, update itself, then ask the user to restart the setup script.
It is not retroactively applied, hence the script will need to be downloaded again f.ex with:

wget https://gist.github.com/husjon/c5225997eb9798d38db9f2fca98891ef/raw/setup_valheim_server.sh -O ~/setup_valheim_server.sh

This will overwrite the existing script.

This feature was added Thu, 15 Dec 2022 19:56:51 +0100.

Adding Pre-existing worlds

If you already have a world you've played on (f.ex hosted on your own computer) and you'd like to continue using it with this server,
the following steps can be used.

  1. Locate your save folder, navigate to this folder:
    The files we are interested in are the .db and .fwl files.
    • Windows: %userprofile%/AppData/LocalLow/IronGate/Valheim/Worlds
    • Linux: $HOME/.config/unity3d/IronGate/Valheim/worlds
  2. Stop the Valheim Server with valheim_server stop
  3. With an SFTP client (f.ex FileZilla), upload the .db and .fwl file to the folder: /home/${USER}/valheim_data
  4. Edit the ~/server_credentials and update the WORLD_NAME parameter to the name of your World files.
    F.ex if you world file was My_Valheim_World.db and My_Valheim_World.fwl, set it to WORLD_NAME="My_Valheim_World"
  5. Start the Valheim Server with valheim_server start
  6. Within a few moments the server should be back up and running with the world you uploaded.

Troubleshooting

In case you should experience any issues and would need some assistance, the install logs and server logs are helpful to troubleshoot the issue.

To help with this the following steps should be followed:

  1. Create a log output of the running server using the following command:
    journalctl --no-pager --since=-1d --user -u valheim_server > ~/valheim_server.systemd.log
    This will take a snapshot of the logs from the Valheim Server from the last 24 hours.
    In case the Valheim server was never started, this can be omitted.
  2. Download the install_valheim_server.log and valheim_server.systemd.log file located under /home/ubuntu using f.ex FileZilla.
    Note: Use port 22 for SFTP/SSH.
  3. Go to https://gist.github.com/, click Add File for each file, then Create Public / Secret Gist
    This creates a gist (similar to this guide) which we can go through to troubleshoot.
  4. Copy the URL to the gist and create a comment down below describing the issue and adding the link to the gist.

I might be delayed due to work / timezones etc, but hoping to get you going as quickly as possible.

Discord

I've decided to open up my discord server so if you need help or would like to suggest improvements or similar, please tag along.
https://discord.gg/ExnzM4E7pE

Changing versions

This could be useful in case the public version breaks something. Make sure you create a backup of the server before switching versions.

Switcing to the Previous Stable Version

  1. Run SteamCMD to change to the default_old branch
cd ~/steamcmd

./steamcmd.sh \
    +@sSteamCmdForcePlatformType linux \
    +force_install_dir "/home/$USER/valheim_server" \
    +login anonymous \
    +app_update 896660 -beta default_old validate \
    +quit
  1. In Steam, right-click the game, open Properties, go to Betas and select default_old from the dropdown and wait for the game to update (this might take a few minutes).
  2. Start the game and connect to your server as normal.

Switching to the Public Beta Branch

  1. Run SteamCMD to change to the public-test branch
cd ~/steamcmd

./steamcmd.sh \
    +@sSteamCmdForcePlatformType linux \
    +force_install_dir "/home/$USER/valheim_server" \
    +login anonymous \
    +app_update 896660 -beta public-test -betapassword yesimadebackups validate \
    +quit
  1. In Steam, right-click the game, open Properties, go to Betas and select public-test from the dropdown and wait for the game to update (this might take a few minutes).
    If public-test is not in the list, type in yesimadebackups in the Input field below and press Check Code.
  2. Start the game and connect to your server as normal.

Reverting back to the public version

  1. Run SteamCMD to change to the public branch
cd ~/steamcmd

./steamcmd.sh \
    +@sSteamCmdForcePlatformType linux \
    +force_install_dir "/home/$USER/valheim_server" \
    +login anonymous \
    +app_update 896660 -beta public validate \
    +quit
  1. In Steam, right-click the game, open Properties, go to Betas and select None from the dropdown and wait for the game to update (this might take a few minutes).
  2. Start the game and connect to your server as normal.

Oracle and Reclamation of Idle Compute Instances

Oracle have a policy on their Always Free instances whichs allows them to reclaim instances that are idle or using less than a certain percentile (See: Always_Free_Resources).

For the most part using the server should not trigger this.

If the server should be flagged for reclaimation, you'll receive an email saying that it has been flagged for being idle for the last 7 days.
Next it will say that if it continues for another 7 days, the server will be stopped.
This gives us ample time to either back up the data or continue playing.

The only thing needed to do is to log onto your Oracle Cloid Infrastructure, go to your Instances and click the Restart button, this will pause the reclaimation.

TODOs

  • Add ability to update the Valheim server prior to starting the server.
    Tue, 24 Jan 2023 23:01:18 +0100
    Now part of the valheim_server helper command.

  • Add information about adding pre-existing worlds
    Tue, 24 Jan 2023 22:47:19 +0100

  • Add support for users other than ubuntu
    Tue, 24 Jan 2023 22:33:56 +0100
    Made it so that the install script no longer is tied to the ubuntu user.

#!/bin/bash
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
ORANGE=$(tput setaf 3)
BOLD=$(tput bold)
CLEAR=$(tput sgr0)
function warn { echo -en "\n\n${BOLD}${ORANGE}[-] $* ${CLEAR}\n"; }
function success { echo -en "${BOLD}${GREEN}[+] $* ${CLEAR}\n"; }
function info { echo -en "\n\n${BOLD}[ ] $* ${CLEAR}\n"; }
function error { echo -en "${BOLD}${RED}[!] $* ${CLEAR}\n"; }
function notify { echo -en "\n\n${BOLD}${ORANGE}[!] $* ${CLEAR}\n"; }
function perform_self_update {
SETUP_SCRIPT_URL="https://gist.github.com/husjon/c5225997eb9798d38db9f2fca98891ef/raw/setup_valheim_server.sh"
ETAG_CACHE="${HOME}/.cache/setup_valheim_server.etag"
SETUP_SCRIPT_PATH="$(realpath "$0")"
TEMP_SCRIPT_PATH="$(mktemp)"
info "Checking for setup script updates"
curl --silent --etag-save "${ETAG_CACHE}" --etag-compare "${ETAG_CACHE}" -L "${SETUP_SCRIPT_URL}" -o "${TEMP_SCRIPT_PATH}"
if [[ -s "${TEMP_SCRIPT_PATH}" ]]; then
if ! cmp --silent $SETUP_SCRIPT_PATH $TEMP_SCRIPT_PATH; then
echo "Setup script available, updating..."
notify "Changes (< = removed | > = added):"
diff --color --minimal "${SETUP_SCRIPT_PATH}" "${TEMP_SCRIPT_PATH}"
echo
sleep 1
mv "${TEMP_SCRIPT_PATH}" "${SETUP_SCRIPT_PATH}"
success "Updated setup script."
notify "Please re-run the setup script..."
echo
exit 0
fi
fi
success "No update available"
echo
rm -f "${TEMP_SCRIPT_PATH}"
}
function main {
# Stop on error
set -e
cd
while :; do
echo "This script will install the Valheim Dedicated server "
echo -n "Are you sure? [yes/no] "
read -r answer
case $answer in
YES|Yes|yes|y)
break;;
NO|No|no|n)
echo Aborting; exit;;
esac
done
echo
while :; do
echo "Should this server use crossplay?"
echo "Note: this is currenly highly experimental"
echo -n "[yes/no] (default: no) "
read -r answer
case $answer in
YES|Yes|yes|y)
CROSSPLAY_SUPPORT=true
break;;
NO|No|no|n|*)
CROSSPLAY_SUPPORT=false
break;;
esac
done
# Update and upgrade the system
mkdir -p ~/.cache
if [[ ! -f ~/.cache/valheim_server_setup ]]; then
info "First time setup"
info "Adding Architecture"
sudo apt -y update
sudo dpkg --add-architecture armhf
info "Updating and upgrading the OS"
sudo apt -y update
sudo apt -y upgrade && touch ~/.cache/valheim_server_setup
success "Updating and upgrading the OS - Done"
sudo reboot
warn "Rebooting..."
fi
# Prepare box86 and box64
info "Installing required packages"
sudo apt -y install \
build-essential \
cmake \
gcc-arm-linux-gnueabihf \
git \
libc6 \
libncurses6 \
libstdc++6 \
libpulse0 && \
success "Installing required packages - Done"
if uname -p | grep "aarch64" > /dev/null; then
notify "Found system to be 64bit Arm"
# Fetch and build Box 86 and 64
for ARCH in {86,64}; do
cd
if [[ ! -d "$HOME/box${ARCH}" ]]; then
info "Fetching Box${ARCH}"
git clone "https://github.com/ptitSeb/box${ARCH}" && \
mkdir -p "box${ARCH}/build"
success "Fetching Box${ARCH} - Done"
fi
info "Building Box${ARCH}"
cd "$HOME/box${ARCH}/build"
git fetch
if [[ $ARCH == 64 ]] && [[ -n $BOX64_VERSION ]]; then
TAG="${BOX64_VERSION}"
elif [[ $ARCH == 86 ]] && [[ -n $BOX86_VERSION ]]; then
TAG="${BOX86_VERSION}"
else
TAG="$(git tag | tail -n 1)"
fi
git checkout "${TAG}"
cmake .. -DRPI4ARM64=1 -DCMAKE_BUILD_TYPE=RelWithDebInfo
make -j"$(nproc)" && success "Building Box${ARCH} - Done"
info "Installing Box${ARCH}"
sudo make install && success "Installing Box${ARCH} - Done"
done
sudo systemctl restart systemd-binfmt.service
if ! grep -F '[valheim_server.x86_64] #box64 v0.2.6' ~/.box64rc; then
info "Adding box64 configuration"
cat <<-EOF | tee -a ~/.box64rc
[valheim_server.x86_64] #box64 v0.2.6
BOX64_DYNAREC_BLEEDING_EDGE=0
BOX64_DYNAREC_STRONGMEM=3
EOF
fi
fi
# Fetch and initialize steamcmd
if [[ ! -f ~/steamcmd/steamcmd.sh ]]; then
info "Fetching steamcmd"
mkdir -p ~/steamcmd && \
cd ~/steamcmd && \
curl -sqL "https://steamcdn-a.akamaihd.net/client/installer/steamcmd_linux.tar.gz" | tar zxvf -
./steamcmd.sh +quit && success "Fetching steamcmd - Done"
fi
# Add steamcmd steamclient.so symlink
info "Adding steamclient.so symlink"
mkdir -p ~/.steam/sdk64 && ln -frs ~/steamcmd/linux64/steamclient.so ~/.steam/sdk64/
# Install the Valheim Dedicated Server from Steam
if [[ ! -f ~/valheim_server/start_server.sh ]]; then
info "Installing Valheim Dedicated Server"
cd ~/steamcmd
./steamcmd.sh \
+@sSteamCmdForcePlatformType linux \
+force_install_dir "/home/$USER/valheim_server" \
+login anonymous \
+app_update 896660 validate \
+quit && \
success "Installing Valheim Dedicated Server - Done"
fi
# Add x86_64 version of libpulse-mainloop-glib.so.0
if [[ $CROSSPLAY_SUPPORT == true ]]; then
if [[ ! -f ~/valheim_server/linux64/libpulse-mainloop-glib.so.0 ]]; then
info "Installing libpulse-mainloop-glib.so.0:x86_64"
pushd "$(mktemp -d)"
wget http://mirrors.kernel.org/ubuntu/pool/main/p/pulseaudio/libpulse-mainloop-glib0_15.99.1+dfsg1-1ubuntu1_amd64.deb
dpkg -x libpulse-mainloop-glib0_15.99.1+dfsg1-1ubuntu1_amd64.deb ./ && \
cp usr/lib/x86_64-linux-gnu/libpulse-mainloop-glib.so.0 "/home/$USER/valheim_server/linux64/"
success "Installing libpulse-mainloop-glib.so.0:x86_64 - Done"
popd
fi
else
rm -f "/home/$USER/valheim_server/linux64/libpulse-mainloop-glib.so.0"
fi
# Initialize the Server Credentials file
if [[ ! -f ~/server_credentials ]]; then
info "Generating server_credentials file"
PASSWORD="$(tr -dc "a-zA-Z0-9" < /dev/urandom | fold -w "32" | head -n 1)"
cat <<-EOF > ~/server_credentials && success "Done"
SERVER_NAME="My server"
WORLD_NAME="My World"
# NOTE: Minimum password length is 5 characters & Password cant be in the server name.
PASSWORD="${PASSWORD}"
# If the server should be listed publically. (1=yes, 0=no)
PUBLIC=0
PORT=2456
EOF
success "Generating server_credentials file - Done"
fi
# Update firewall
info "Updating firewall rules"
RULES=(
"INPUT -p udp -m state --state NEW -m udp --dport 2456 -j ACCEPT"
"INPUT -p udp -m state --state NEW -m udp --dport 2457 -j ACCEPT"
"INPUT -p udp -m state --state NEW -m udp --dport 2458 -j ACCEPT"
)
FIREWALL_RULES_ADDED=false
for RULE in "${RULES[@]}"; do
sudo iptables -C ${RULE} 2> /dev/null || \
sudo iptables -I ${RULE} && FIREWALL_RULES_ADDED=true
done
if $FIREWALL_RULES_ADDED; then
sudo cp /etc/iptables/rules.v4{,.bak}
TMP_FILE=$(mktemp)
sudo iptables-save > "${TMP_FILE}"
sudo mv "${TMP_FILE}" /etc/iptables/rules.v4
fi
success "Done"
# Add Valheim helper
info "Creating Valheim Server helper"
mkdir -p /usr/sbin
cat <<-EOF | sudo tee /usr/sbin/valheim_server > /dev/null
#!/bin/bash
# Stop on error
set -e
function show_usage {
echo "Usage: \$(basename \$0) COMMAND"
echo
echo "Commands:"
echo " update Stops and Updates the Valheim server"
echo " start Start the Valheim server"
echo " stop Stops the Valheim server"
echo " restart Restart the Valheim server"
echo " logs Shows the logs of the Valheim server"
echo " logs-live Shows the live logs of the Valheim server"
echo " help Shows this help message"
echo
}
function start_server {
if ! systemctl --user --quiet is-active valheim_server; then
echo "Starting Server..."
systemctl --user start valheim_server
sleep 2 # sleeping to allow service to start up before validating.
systemctl --user --quiet is-active valheim_server && \
echo "Server Started"
else
echo "Server already running"
fi
}
function stop_server {
if systemctl --user --quiet is-active valheim_server; then
echo "Stopping Server, please wait..."
systemctl --user stop valheim_server
echo "Server Stopped"
else
echo "Server already stopped"
fi
}
function restart_server {
stop_server && start_server
}
case \$1 in
update)
stop_server
/home/\${USER}/steamcmd/steamcmd.sh \\
+@sSteamCmdForcePlatformType linux \\
+force_install_dir "/home/\${USER}/valheim_server" \\
+login anonymous \\
+app_update 896660 validate \\
+quit
echo
echo "Server updated."
echo "Start the server with \"valheim_server start\""
;;
start|stop|restart)
\${1}_server;;
logs)
journalctl --user -u valheim_server;;
logs-live)
journalctl --user -f -u valheim_server;;
help|--help|-h|*)
show_usage;;
esac
EOF
sudo chmod +x /usr/sbin/valheim_server
# Set up the Systemd Service
info "Setting up Systemd Service"
mkdir -p ~/.config/systemd/user/
[[ $CROSSPLAY_SUPPORT == true ]] && CROSSPLAY="-crossplay"
# Add servicefile
cat <<-EOF > ~/.config/systemd/user/valheim_server.service
[Unit]
Description=Valheim Dedicated Server
[Service]
KillSignal=SIGINT
TimeoutStopSec=30
Restart=always
RestartSec=5
WorkingDirectory=/home/${USER}/valheim_server
EnvironmentFile=/home/${USER}/server_credentials
Environment=SteamAppId=892970
Environment=LD_LIBRARY_PATH="./linux64:\$LD_LIBRARY_PATH"
ExecStart=/home/${USER}/valheim_server/valheim_server.x86_64 \\
-nographics \\
-batchmode \\
-port "\${PORT}" \\
-public "\${PUBLIC}" \\
-name "\${SERVER_NAME}" \\
-world "\${WORLD_NAME}" \\
-password "\${PASSWORD}" \\
${CROSSPLAY} \\
-savedir "/home/${USER}/valheim_data"
[Install]
WantedBy=default.target
EOF
# Enable Lingering Systemd user sessions
loginctl enable-linger
# Reload Systemd
systemctl --user daemon-reload
# Enable Valheim Systemd service allowing it to start automatically on boot
systemctl --user enable valheim_server.service && success "Setting up Systemd Service - Done"
info "Creating Readme"
cat <<-EOF > ~/Readme.md && success "Creating Readme - Done"
# Valheim Server Helper Commands
## Help
valheim_server help
## Start server
valheim_server start
## Stop server
valheim_server stop
## Updating the server
valheim_server upgrade
# Enable / Disable crossplay
This can be accomplished by re-running the setup script.
It will ask you of you want crossplay enabled or disabled.
EOF
# Start the Valheim Systemd service
success "Setup finished"
echo "A file named 'server_credentials' have been placed in the home directory"
echo "Edit as you see fit with f.ex 'nano ~/server_credentials'"
echo "Within nano, when done editing, press 'Ctrl+X', then 'y', finally 'Enter'"
echo
echo "Finally, to start the server, the following command can be used:"
echo " valheim_server start"
echo
echo "A readme with additional commands have also been placed in the home directory"
}
if [ `id -u` -eq 0 ]; then
error Please run this script as a regular user.
exit 1
fi
perform_self_update
# Pinned versions for Box 64 / 86
BOX64_VERSION="v0.2.6"
# BOX86_VERSION="v0.3.0"
main | tee install_valheim_server.log
@husjon
Copy link
Author

husjon commented Apr 15, 2023

@Dhrover multiple worlds would require running multiple server instances on the same virtual machine.
This is possible given the resources, however it would require some tinkering to adjust the port forwarding required for each world.
The install script as it stands currently does not support this, but creating multiple service files ~/.config/systemd/user/valheim_server.service could be one way of achieving this.

@evisnoopy
Copy link

evisnoopy commented Apr 15, 2023

every time I try to install I get this error:
Failed to connect to bus: No such file or directory

edit: I get the error after it says this

[+] Installing Valheim Dedicated Server - Done


[ ] Updating firewall rules
[+] Done


[ ] Creating Valheim Server helper


[ ] Setting up Systemd Service

and when I try running
bash ./setup_valheim_server.sh
I get

[!] Changes (< = removed  |  > = added):

[+] Updated setup script.


[!] Please re-run the setup script...

until I run rm ~/.cache/setup_valheim_server.etag

@husjon
Copy link
Author

husjon commented Apr 16, 2023

@evisnoopy could you create a gist with the output of ~/install_valheim_server.log and add the link here, then I could take a look.

@evisnoopy
Copy link

evisnoopy commented Apr 16, 2023

@husjon
Copy link
Author

husjon commented Apr 17, 2023

@evisnoopy yup that's correct.
I see that the log mentions using the /root directory, make sure that the script is run without using sudo and as a non-root user.

@taitouotila
Copy link

taitouotila commented Apr 26, 2023

Thank you for the guide! Server that has been up and running for a week randomly stopped working. I tried to reinstall everything following your guide on a new VM instance but it seems to not work either. It seems that the server is constantly restarting. Server and installation logs below:

https://gist.github.com/taitouotila/3ba5e7cf4b66f946cfd3d43bf700e6d2
https://gist.github.com/taitouotila/5b291d6d7f5cf4dd8d2851dd08d6c0ac

@beauchamp920
Copy link

Hello there,

Thank you for the guide because i know nothing about making a server.
However i can't see to make it work. I get the server starting but i can't connect to it in the game and i don't see it in the servers from Steam.

Id like to give you logs of my devices but i don't know how.

Anyhelp would be appreciated.

@husjon
Copy link
Author

husjon commented Apr 26, 2023

@beauchamp920 if you download the install_valheim_server.log from your server using f.ex FileZilla, then create a gist here https://gist.github.com/ (Press Add File in the bottom left), once done you can copy the link and add it here, hope this helps as a start.

@husjon
Copy link
Author

husjon commented Apr 26, 2023

@taitouotila this looks similar to the issue we saw before the the Box version pinning was in place.

Could you hop onto your VM navigate to ~/box86 and ~/box64 and do a git status.
Box86 should show HEAD on v0.3.0, while Box64 should be on v0.2.0.

The install script was updated in mid-march with version pinning, so you could try to update the script manually if you see the versions are differing, see #self-updating, then re-run it.

@beauchamp920
Copy link

beauchamp920 commented Apr 26, 2023

@beauchamp920 if you download the install_valheim_server.log from your server using f.ex FileZilla, then create a gist here https://gist.github.com/ (Press Add File in the bottom left), once done you can copy the link and add it here, hope this helps as a start.

I hope this is what you need:

https://gist.github.com/beauchamp920/270a91f243f08eb576cf074dbca2c0f2#file-install_valheim_server-log

edit: i made the gist public

@husjon
Copy link
Author

husjon commented Apr 26, 2023

@beauchamp920 yup, perfect.
Everything seem to be in order when it comes to the install sequence.
Did you run valheim_server start as mentioned at the end (just to make sure)? :)

No need to make them public but does not harm, once we've figured it out you can delete it or set it to private / secret (secret just means that you need the link to view the file) :)

@beauchamp920
Copy link

beauchamp920 commented Apr 26, 2023

@beauchamp920 yup, perfect. Everything seem to be in order when it comes to the install sequence. Did you run valheim_server start as mentioned at the end (just to make sure)? :)

No need to make them public but does not harm, once we've figured it out you can delete it or set it to private / secret (secret just means that you need the link to view the file) :)

Yes i did use : valheim_server start.

When i did it again (just now) it says: Server already running

@husjon
Copy link
Author

husjon commented Apr 26, 2023

@beauchamp920 alright, good start.
Could you run the following command on your VM and download and create a gist for it (you can leave it as secret).
journalctl --no-pager --since=-1d --user -u valheim_server > ~/valheim_server.systemd.log
It's from the #troubleshooting section I just added.
It pulls out the last 24 hours of the valheim server logs which could help us see if it's running as it should.

@beauchamp920
Copy link

beauchamp920 commented Apr 26, 2023

as requested:

https://gist.github.com/beauchamp920/613c18abcb09aaf18c2022cadddcdc57

edit: im sorry if i didnt see the Troubleshooting section

@taitouotila
Copy link

@husjon Cheers, I checked both and box86 is showing "HEAD detached at v0.3.0" and box64 is showing "HEAD detached at v0.2.0"

@husjon
Copy link
Author

husjon commented Apr 26, 2023

@beauchamp920 perfect and no problem, I added it after your initial comment.
It seem to be the same issue as taitouotila is having , could you check the following comment #gistcomment-4549613 and follow the same steps.

@husjon
Copy link
Author

husjon commented Apr 26, 2023

@taitouotila and @beauchamp920 I just updated my own server (valheim_server update) and I got the very same error.
It seem to be related to one of the latest versions of Valheim.

I'm gonna do some testing to see what could be causing it, hopefully I can get something going soon, but it's also getting late so I might need to look into it tomorrow.

@taitouotila
Copy link

Thank you for the quick troubleshooting help! Definitely late here aswell - looking forward to hear if you get it going!

@RaDiKalDev
Copy link

I have a couple of manual Valheim server builds on Oracle Cloud ARM servers which have run into the same issue. My intel server build updated and runs fine but the two ARM builds fail same as @taitouotila and @beauchamp920 . Some of the scuttlebutt on reddit noted that hitting the RAM limit can cause the same valheim log error. I'll share if I track down a root cause.

-Ron

@husjon
Copy link
Author

husjon commented Apr 26, 2023

@RaDiKalDev thanks for the heads up.
I've been looking for similar issues, some mention wrongly typed passwords etc can cause the same.
I've tried a few different things, updating the distro, using master branches of box and doing clean installs of the server files + purging world data, with no luck.

Monitoring the memory usage during server startup shows usage at about 1 GB out of 24GB, so I don't think it an issue of RAM limits :)

If you do find anything just tag me.

Cheers.

@husjon
Copy link
Author

husjon commented Apr 27, 2023

@RaDiKalDev, @taitouotila and @beauchamp920 I'm still looking into it and I might be able to get a fix in within the next hour.
The issue seem to be related to the packaged steamclient.so.
Just need to finalize the testing and come up with a permanent fix for it.

@husjon
Copy link
Author

husjon commented Apr 27, 2023

@RaDiKalDev, @taitouotila and @beauchamp920 Alright, so good news and bad news.
Bad news (but only a little): I got it running once while troubleshooting, messed it up and got stuck.

However, the good news is that I got it back up and running again now.
First I was focused on trying to get the server up and running with the previous version, just to make sure that we at least had something to go back to.

Once I got that up and running I focused on the latest version, the good thing was that I only had to change from the Beta branch default_old to the public branch.

So the steps to get it up and running again on ARM servers should be to only add a symlink from steamcmds steamclient.so to ~/.steam/sdk64/ using the following command:
mkdir -p ~/.steam/sdk64 && ln -frs ~/steamcmd/linux64/steamclient.so ~/.steam/sdk64/
Then restart the valheim server using valheim_server restart

Please add this symlink and get back to me with the results on your instances, if this works as expected on your end as well I'll be adding a patch to the setup script.
Thanks in advance.

edit: added missing instruction about restarting server

@taitouotila
Copy link

Sounds good! I've tried to connect to my server and it failed, then I ran the command you provided and did valheim_server restart and after waiting for a few minutes, I can now connect to my server! Great job on finding the fix, let's hope it stays working

@husjon
Copy link
Author

husjon commented Apr 27, 2023

@taitouotila Thanks for the feedback :)

@beauchamp920
Copy link

@husjon Same for me, i just typed : mkdir -p ~/.steam/sdk64 && ln -frs ~/steamcmd/linux64/steamclient.so ~/.steam/sdk64/
Then restart the valheim server using valheim_server restart. I waited a few minutes and i connected to my new server !

Thank you a lot ! I will be coming back if i got any more problems ;)

@beauchamp920
Copy link

@husjon if you allow me I also have another question. Is it possible to have backups of the world? Like a command that create a new folder every week in the VM with the .db and .fwl file in case there's a problem with it.

Thanks in advance

@RaDiKalDev
Copy link

I wasn't able to successfully apply your suggested patch to my hand-built instance, so I saved the world files, terminated the OCI instance and created a fresh new one following your specs above.

I generated SSH keys from within the OCI instance creation screen and can successfully log in (ubuntu@vh).

After the first run of your script, I am disconnected as expected but, on re-login, the second run of the script didn't do anything.

Last login: Thu Apr 27 19:00:06 2023 from 24.59.114.38
ubuntu@vh:~$ bash ./setup_valheim_server.sh


[ ] Checking for setup script updates
Setup script available, updating...


[!] Changes (< = removed  |  > = added):

[+] Updated setup script.


[!] Please re-run the setup script...

ubuntu@vh:~$ bash ./setup_valheim_server.sh


[ ] Checking for setup script updates
Setup script available, updating...


[!] Changes (< = removed  |  > = added):

[+] Updated setup script.


[!] Please re-run the setup script...

ubuntu@vh:~$ ls -l
total 32
-rw-rw-r-- 1 ubuntu ubuntu 16448 Apr 27 19:03 install_valheim_server.log
-rw------- 1 ubuntu ubuntu 11572 Apr 27 19:05 setup_valheim_server.sh
ubuntu@vh:~$

I ended up executing Sudo bash ./setup_valheim_server.sh which did successfully build the box64 and box86 components along with install steamcmd. However, it failed to generate the Valheim configuration file. Here's the tail of the run:

 Update state (0x81) verifying update, progress: 63.59 (996494184 / 1566988325)
Success! App '896660' fully installed.
[+] Installing Valheim Dedicated Server - Done


[ ] Generating server_credentials file
[+] Done
[+] Generating server_credentials file - Done


[ ] Updating firewall rules
[+] Done


[ ] Creating Valheim Server helper


[ ] Setting up Systemd Service
Failed to connect to bus: No such file or directory
ubuntu@vh:~$

I only have the setup_valheim_server.sh and install_valheim_server.log files in /home/ubuntu.

Happy to wipe it out and try again. I expected the server config file to be in the current directory. It isn't there (and isn't anywhere else on the server). I'm not sure the installation was placed in the expected location. - suggestions?

@husjon
Copy link
Author

husjon commented Apr 27, 2023

@beauchamp920 Brilliant and thanks for the feedback.

As for the backups:
The game will now save after you sleep, or at 30 minute intervals (from: https://steamdb.info/patchnotes/9384139)
It also keeps a 2 hour and a 12 hour backup by default so you should always have a save to go back to.
These can all be found at ~/valheim_data/worlds_local/.

As an example here's my worlds_local folder after a few weeks of playing:

ubuntu@instance-20221121-1413:~$ ls -ls ~/valheim_data/worlds_local/
total 3792
628 -rw-rw-r-- 1 ubuntu ubuntu 640390 Apr 26 13:28 'My World.db'
628 -rw-rw-r-- 1 ubuntu ubuntu 640390 Apr 26 12:58 'My World.db.old'
  4 -rw-rw-r-- 1 ubuntu ubuntu     44 Apr 26 13:28 'My World.fwl'
  4 -rw-rw-r-- 1 ubuntu ubuntu     44 Apr 26 12:58 'My World.fwl.old'
  4 -rw-rw-r-- 1 ubuntu ubuntu     44 Apr 25 09:27 'My World_backup_auto-20230425092755.fwl'
628 -rw-rw-r-- 1 ubuntu ubuntu 640390 Apr 25 09:57 'My World_backup_auto-20230425095756.db'
  4 -rw-rw-r-- 1 ubuntu ubuntu     44 Apr 25 19:28 'My World_backup_auto-20230425192815.fwl'
628 -rw-rw-r-- 1 ubuntu ubuntu 640390 Apr 25 19:58 'My World_backup_auto-20230425195817.db'
  4 -rw-rw-r-- 1 ubuntu ubuntu     44 Apr 26 05:28 'My World_backup_auto-20230426052836.fwl'
628 -rw-rw-r-- 1 ubuntu ubuntu 640390 Apr 26 05:58 'My World_backup_auto-20230426055837.db'
  4 -rw-rw-r-- 1 ubuntu ubuntu     44 Apr 26 11:28 'My World_backup_auto-20230426112848.fwl'
628 -rw-rw-r-- 1 ubuntu ubuntu 640390 Apr 26 11:58 'My World_backup_auto-20230426115850.db'

@husjon
Copy link
Author

husjon commented Apr 27, 2023

@RaDiKalDev the Failed to connect to bus: No such file or directory is because of running the setup script as root / using sudo as it does not have the context of a user systemd.
Try it again using the non-root user.

Edit:
Oh and I see you also got the update bug.
Could you first cat the content of the ~/.cache/setup_valheim_server.etag and paste it here, then delete that file and run it again (this bypasses the update), then it should work as expected.
As for the server files, since root / sudo was used, they are under /root :)

@RaDiKalDev
Copy link

Here's the etag:

ubuntu@vh:~$ cat ~/.cache/setup_valheim_server.etag
a493bbf43bad06ac53547e78d689838e9220f82ca002c61a361e88b1c11af921
ubuntu@vh:~$

Deleted it and reran the script w/out sudo. It ran successfully and, of course, the server crashes and restarts in a loop (expected). I then applied the patch from earlier today and restarted the server. It now runs fine and I'll move my saved world over later tonight. Thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment