Post

Installing Valheim Server and Running it as a systemd Service

Short tutorial to get a Valheim server up and running on linux and a reminder to myself of how my current Valheim server installation is setup.

Services and Required Knowledge

steamcmd is a command line tool required to manage any and all steam games you wish to host locally on a linux based server, including Valheim. Ensure it is installed before proceeding. If you do not see steamcmd listed in the output of the first command use the second to install it.

Examples are assuming you have a Debian based linux distro using the apt package manager. Please use your provided distrobution’s package manager and its syntax.

1
2
3
sudo apt list --installed | grep steamcmd

sudo apt install steamcmd -y

Each steam game is identified on steam with a unique identifier called an “App ID,” or colloquially a, “Steam ID.” You will need to find the proper ID and pass that into steamcmd when requesting to download or update the game. Be careful, as you will want to ensure you are using the Steam ID for Valheim’s dedicated server version of the game, not the standard game installed on your gaming PC. You can find any Steam ID you need simply from searching google, or you can use the steamdb website. Valheim Server’s Steam ID is: 896660.

Once you have steamcmd installed and have the necessary Steam ID you can run the command below to download the game files:

For ease of use later with systemd setup use a path with NO spaces or special characters.

1
steamcmd +login anonymous +force_install_dir /PATH/YOU/WANT/GAME/FILES +app_update 896660 validate +exit

Once the game files have been downloaded save this command for later within a script. It can be run at a later time to update the game whenever the Valheim team releases a new version. I saved mine under ~/bin/update-valheim. Mark your file as executable with chmod and you can stop there if you like. If you leave out the file extension, as I have done, you can add the script to your path and run the script as if it were a built-in command in your shell. Add this to the bottom of your .bashrc file:

1
2
# adding custom scripts within the ~/bin directory to function simillar to built-in commands
export PATH=$PATH:~/bin

If followed exactly you can type update-valheim into your terminal from anywhere and the steamcmd command will run to update the game.

Important Files and File Structure

All of your important game files will be stored under the path you specify within the initial command for the download. e.g. Mine are stored under ~/steam/steamapps/common/valheim, this holds all important binaries and other assets required to run the game. The save files for your world, along with the list of in game admins and banned players are found in: ~/.config/unity3d/IronGate/Valheim.

The file used to start the server comes along with the download of the game, start_server.sh. The file itself come along with instructions written inside in the form of comments. Open up the file in your preferred text editing software and follow the instructions. It is highly recommended that you save a copy of the file locally incase of Steam deciding it wants to overwrite the file. I’ve never ran into that issue but it is always better to be safe than sorry.

Should you want to install mods to improve upon and change the vanilla Valheim experience in anyway; that will require the installation of BepInEx on top of your Valheim install. You can download the files from either the BepInEx website, or from the GtiHub page for the project. I have found the GitHub page more straight forward to follow. Simply download the .zip file designated for your operating system and subsequent architecture. After downloading, unzip the file and copy the contents to the root of your game istallation. Following my own installation that is ~/steam/steamapps/common/valheim. I do not remember if BepInEx comes with a pre-configured start up script. In the case it does not I have my script below as well.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env bash
# BepInEx-specific settings
# NOTE: Do not edit unless you know what you are doing!
####
export DOORSTOP_ENABLE=TRUE
export DOORSTOP_INVOKE_DLL_PATH=./BepInEx/core/BepInEx.Preloader.dll
export DOORSTOP_CORLIB_OVERRIDE_PATH=./unstripped_corlib

export LD_LIBRARY_PATH="./doorstop_libs:$LD_LIBRARY_PATH"
export LD_PRELOAD="libdoorstop_x64.so:$LD_PRELOAD"
####


export LD_LIBRARY_PATH="./linux64:$LD_LIBRARY_PATH"
export SteamAppId=892970

echo "Starting server PRESS CTRL-C to exit"

# Tip: Make a local copy of this script to avoid it being overwritten by steam.
# NOTE: Minimum password length is 5 characters & Password cant be in the server name.
# NOTE: You need to make sure the ports 2456-2458 is being forwarded to your server through your local router & firewall.
exec ./valheim_server.x86_64 -name "NAME OF THE SERVER DISPLAYED IN GAME" -port 2456 -world "NAME FOR WORLD SAVE FILE" -password "PASSWORD USED IN GAME"

At the time of writing the steps for installation when downloading BepInEx from their website seem to differ slightly. I have never followed this route. If you wish too please follow the documentation listed there.

To add mods to your server the configuration files can be found under ~/steam/steamapps/common/valheim/BepInEx. In here you will find a plugins folder for storing specific binaries and additional files required to get mods up and running. Each individual mod will need its own folder to hold those files required for the mod to function. Using my installation as an example the path will look something like this: ~/steam/steamapps/common/valheim/BepInEx/plugins/FORLDER-FOR-MOD/MOD-FILE(s). Typically every mod is packaged as a .zip file and taking the output folder from unzipping it and adding it to the plugins folder will suffice.

systemd

Running the server through systemd will allow you to easily keep the game up and running 24/7. Starting the server straight from your command prompt introduces some issues. Namely, you are stuck leaving your terminal session open, and will not be able to interact with your server in any other capacity, assuming your game server is not running on a linux desktop. You will need to create a systemd unit file and drop it into the /etc/systemd/system directory. Below is what my file looks like.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[Unit]
Description=Valheim Modded service
Wants=network.target
After=syslog.target network-online.target

[Service]
Type=simple
Restart=on-failure
RestartSec=10
User=USER
WorkingDirectory=/home/USER/steam/steamapps/common/valheim/
ExecStart=/bin/sh /home/USER/steam/steamapps/common/valheim/start_server_bepinex.sh

[Install]
WantedBy=multi-user.target

If you have followed my installation exactly make sure to replace all instances of “USER” with the name of your user on the linux server. Other wise be sure to to change the “WorkingDirectory” with the path to your game installation, and the “ExecStart” path to the path of your start up script. Afterwards you can run sudo systemctl enable valheim_modded. Use sudo systemctl start valheim_modded to start up the server for the first time. After the first start you won’t ever have to do that again unless you stop the server manually with sudo systemctl stop valheim_modded. To check if the service is running properly use sudo systemctl status valheim_modded. If for what ever reason your server fails to start, you can use journalctl to see the logs within the valheim service. i.e. sudo journalctl -u valheim_modded -n 20 will show you the last 20 lines worth of the log.

Final Touches

You are just about ready to connect and play with your friends! The last few things you will need to do to connect are:

  • Open ports 2456-2458 on your systems firewall (if the firewall is enabled)
  • Port forward those same ports through your gateway to your host machine (if hosted locally)
  • Give the public Ip assigned to your machine (if hosted in the cloud) or the public Ip of your home networks gateway (if hosted locally) to players
  • Enter that Ip address into the correct field on Valhiem and you are all set!

References

Harre.dev Valheim Server on Linux

BepInEx

steamcmd

systemd

This post is licensed under CC BY 4.0 by the author.