GitHub Self-Hosted Runner Setup on Ubuntu with systemctl Service
This guide provides steps to set up a GitHub self-hosted runner on an Ubuntu server and run it as a service using systemctl.
Prerequisites
- Ubuntu server with root or
sudoaccess. - GitHub repository or organization where the runner will be connected.
- A GitHub personal access token with the necessary permissions to add self-hosted runners.
Step 1: Download and Configure the GitHub Runner
-
Create a directory for the runner:
mkdir -p ~/actions-runner && cd ~/actions-runner -
Download the GitHub runner binary: Replace
REPO_OWNERandREPO_NAMEwith the owner and repository names, or use your organization name if you're adding a runner to an organization.# Replace version with the latest from https://github.com/actions/runner/releases curl -o actions-runner-linux-x64.tar.gz -L https://github.com/actions/runner/releases/download/v2.305.0/actions-runner-linux-x64-2.305.0.tar.gz -
Extract the runner:
tar xzf actions-runner-linux-x64.tar.gz -
Configure the runner: You can find the registration token in the GitHub repository or organization settings under Settings > Actions > Runners.
./config.sh --url https://github.com/REPO_OWNER/REPO_NAME --token YOUR_REGISTRATION_TOKEN
Step 2: Create the run.sh Script
-
Create
run.shto ensure the runner restarts automatically in case of failure.nano ~/actions-runner/run.sh -
Add the following content to
run.sh:#!/bin/bash cd /home/$USER/actions-runner ./run.sh --once -
Make
run.shexecutable:chmod +x ~/actions-runner/run.sh
Step 3: Set Up systemctl Service
-
Create a new systemd service file for the runner:
sudo nano /etc/systemd/system/github-runner.service -
Add the following configuration to the service file:
[Unit] Description=GitHub Actions Runner After=network.target [Service] ExecStart=/home/$USER/actions-runner/run.sh User=$USER WorkingDirectory=/home/$USER/actions-runner Restart=always RestartSec=10 [Install] WantedBy=multi-user.target[Unit] Description=GitHub Self-Hosted Runner After=network.target [Service] # Change these paths to your runner's installation directory ExecStart=/home/ubuntu/paytring-runners/magic/run.sh User=ubuntu WorkingDirectory=/home/ubuntu/paytring-runners/magic Environment="RUNNER_ALLOW_RUNASROOT=1" # If you want to restart the service automatically in case of failure Restart=always RestartSec=10 [Install] WantedBy=multi-user.targetReplace
$USERwith your actual username or use$USERif applicable in this context.NOTE:Github runner cannot be run as root -
Reload the systemd daemon to recognize the new service:
sudo systemctl daemon-reload -
Enable and start the service:
sudo systemctl enable github-runner sudo systemctl start github-runner -
Verify the runner is active:
sudo systemctl status github-runnerYou should see an "active (running)" status if the service is correctly set up.
Step 4: Confirm Runner Connection in GitHub
Go to your GitHub repository or organization settings under Settings > Actions > Runners to check if the runner is connected and ready.
Managing the GitHub Runner Service
- Start the service:
sudo systemctl start github-runner - Stop the service:
sudo systemctl stop github-runner - Restart the service:
sudo systemctl restart github-runner - Check service status:
sudo systemctl status github-runner
Troubleshooting
- Check the runner logs:
journalctl -u github-runner -f - Make sure your GitHub registration token is up to date.
Unregistering the Runner
If you need to unregister the runner, stop the service and remove it:
-
Stop the service:
sudo systemctl stop github-runner -
Remove the service:
sudo systemctl disable github-runner sudo rm /etc/systemd/system/github-runner.service -
Unregister the runner in GitHub:
./config.sh remove --token YOUR_REGISTRATION_TOKEN -
Clean up by deleting the runner directory:
rm -rf ~/actions-runner