Set up a self-hosted CI/CD server on a dedicated machine
A self-hosted CI/CD server is a dedicated machine that runs your build and test jobs instead of renting build minutes from a hosted provider. You register it as a self-hosted GitHub Actions runner, a GitLab runner, or both, and your pipelines schedule work onto hardware you control.
This guide sets one up end to end on Ubuntu 24.04 LTS: sizing, Docker, both runner types, build caching that actually uses your NVMe, and the security posture that keeps an internet-facing build box from becoming a liability. Every command is copy-and-run.
Why teams move CI to dedicated hardware
Three things push teams off hosted runners. Queue time: on a busy repo you wait for a runner to free up before your job even starts, and that wait is invisible in the build log but very real in your day. Cache locality: hosted runners are ephemeral and usually start cold, so every job re-downloads dependencies and rebuilds layers over the network. Cost per build minute: once a team runs thousands of minutes a month, especially on larger runner classes, a fixed monthly machine often costs less than the metered bill.
A dedicated machine fixes all three. Jobs start instantly because the runner is always there, caches live on local NVMe and survive between jobs, and the price is flat no matter how many minutes you burn. You also get full control of the toolchain: specific compiler versions, GPU access, large local Docker layer caches, none of which hosted runners give you cleanly.
Be honest about when hosted runners are the right call. For a small team with light, bursty CI, the free tier is genuinely enough and carries zero maintenance. Hosted runners also give you clean isolation for public open-source repos, where running untrusted pull-request code on your own hardware is a real risk. Self-hosting wins when build volume is high, builds are slow and cache-heavy, or you need hardware and tools the hosted classes do not offer.
Sizing the machine
CI is a different load profile from a web server. Pick the machine around how your jobs actually spend time. Our CI/CD runners category lists configurations built for this, high core counts paired with fast NVMe, and the notes below map the parts to the workload.
Cores vs clock
Compile jobs parallelise across cores (make -j, cargo, bazel), so more cores shorten a single build. But some steps are single-threaded and gated by clock, so a balanced part beats a slow many-core chip. For mixed pipelines, favour high core count with a decent per-core clock.
RAM for parallel jobs
Each concurrent job and each build container needs headroom. Linkers, test suites and language servers are memory-hungry. Size RAM for your peak concurrency: roughly 2 to 4 GB per parallel job as a floor, more for JVM, Rust or large container builds.
NVMe for cache and churn
CI writes constantly: checkouts, dependency trees, Docker layers, test artifacts, then deletes them. That churn destroys spinning disks and eats SATA SSD endurance. NVMe keeps cache reads fast and survives the write volume, which is why it is the default for build boxes.
Base setup and Docker
Before anything else, harden the machine. Follow our getting-your-server-online guide first: change the delivered credentials, create an admin user with an SSH key, disable password login, apply updates, and turn on unattended security patches. A build server holds tokens and source code, so treat it as sensitive from minute one.
Almost every modern CI job runs inside a container, so Docker Engine is the foundation. Install it from Docker's official apt repository rather than the older Ubuntu package, so you get current releases and Buildx. These commands are for Ubuntu 24.04.
Add Docker's repository and GPG key, then install the engine, CLI, containerd, Buildx and Compose.
sudo apt-get update
sudo apt-get install -y ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Confirm the engine runs.
sudo docker run --rm hello-world
A self-hosted GitHub Actions runner
Run the runner as its own unprivileged user, never as root. Add it to the docker group so it can start build containers, and keep its home directory as the only place it writes.
Create the runner user and give it Docker access.
sudo useradd -m -s /bin/bash actions
sudo usermod -aG docker actions
Switch to that user with "sudo -iu actions", then download and register the runner. Set RUNNER_VERSION to the latest release from github.com/actions/runner/releases. Replace YOUR_ORG/YOUR_REPO with your repository, and YOUR_RUNNER_TOKEN with the short-lived registration token shown under Settings, Actions, Runners, New self-hosted runner. That token expires in about an hour and is only used once to register.
cd ~
RUNNER_VERSION=2.328.0
curl -o actions-runner.tar.gz -L \
https://github.com/actions/runner/releases/download/v${RUNNER_VERSION}/actions-runner-linux-x64-${RUNNER_VERSION}.tar.gz
tar xzf actions-runner.tar.gz
./config.sh --url https://github.com/YOUR_ORG/YOUR_REPO \
--token YOUR_RUNNER_TOKEN \
--labels self-hosted,linux,x64,dedicated \
--unattended
Install it as a systemd service with the bundled svc.sh so it starts on boot and restarts on failure. Run these from the runner directory as a sudo-capable user, passing the runner account name.
sudo ./svc.sh install actions
sudo ./svc.sh start
sudo ./svc.sh status
Two flags decide how safe this runner is. Labels (here self-hosted, linux, x64, dedicated) are how workflows target the machine. Ephemeral runners, enabled by adding --ephemeral at registration, process exactly one job and then deregister, so no state leaks from one build into the next. Ephemeral matters most on public repositories: without it, a malicious pull request could leave behind a modified toolchain or harvested cache that poisons later jobs. A persistent runner is fine for private repos where you trust the code; anything public should be ephemeral. Ephemeral runners exit after each job, so for a standing pool you re-register from a small wrapper or use an organisation-level runner group rather than the single systemd service above.
Route jobs to the runner by matching its labels with runs-on in your workflow.
Targeting the runner from a workflow
Add the labels to the job's runs-on array. GitHub schedules the job onto any runner that carries all of them.
A job pinned to your dedicated runner.
name: CI
on: [push, pull_request]
jobs:
build:
runs-on: [self-hosted, linux, dedicated]
steps:
- uses: actions/checkout@v4
- name: Build
run: docker build -t myapp:ci .
The GitLab Runner alternative
If you are on GitLab, the runner is a separate agent with the same idea. Install it from GitLab's apt repository, then register it with the docker executor so each job runs in a fresh container.
Add the repository and install the runner package.
curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash
sudo apt-get install -y gitlab-runner
sudo usermod -aG docker gitlab-runner
Register with the docker executor. Get YOUR_RUNNER_TOKEN (it starts with glrt-) from your project or group under Settings, CI/CD, Runners, New project runner. On self-managed GitLab, swap the URL for your instance.
sudo gitlab-runner register \
--non-interactive \
--url https://gitlab.com/ \
--token YOUR_RUNNER_TOKEN \
--executor docker \
--docker-image ubuntu:24.04
Set concurrency and point the cache at NVMe in the runner config. The concurrent value caps total parallel jobs across all runners on the host; size it to your cores and RAM. The mounted /cache volume lets the runner reuse dependencies between jobs.
concurrent = 6
check_interval = 3
[[runners]]
name = "dedicated-docker"
url = "https://gitlab.com/"
executor = "docker"
[runners.docker]
image = "ubuntu:24.04"
privileged = false
volumes = ["/mnt/nvme/gitlab-cache:/cache:rw"]
Build caching that uses the NVMe
The whole point of local hardware is that caches persist and stay fast. Put them on the NVMe volume and wire your builds to reuse them. Create a dedicated cache directory first, for example /mnt/nvme, owned by the build user.
Docker layer cache with Buildx, reading and writing a local cache directory instead of rebuilding every layer. mode=max caches intermediate stages too.
docker buildx create --use --name ci-builder
docker buildx build \
--cache-from type=local,src=/mnt/nvme/buildx-cache \
--cache-to type=local,dest=/mnt/nvme/buildx-cache,mode=max \
-t myapp:ci --load .
For compiled languages, ccache turns repeat compiles into cache hits. Point its directory at NVMe, cap its size, and put its shim ahead of the real compiler on PATH.
sudo apt-get install -y ccache
export CCACHE_DIR=/mnt/nvme/ccache
export CCACHE_MAXSIZE=50G
export PATH="/usr/lib/ccache:$PATH"
ccache --zero-stats
# ... run your build ...
ccache --show-stats
For GitHub Actions, the actions/cache action normally round-trips artifacts to GitHub's servers, which is slow over the network. On a persistent (non-ephemeral) runner you can skip that entirely: mount a stable host path such as /mnt/nvme/deps into your build and let the package manager read straight from local disk. That is the cache locality advantage hosted runners cannot match.
Security posture
A build server executes code by design, which makes it a target. A few rules keep it contained.
Never run untrusted public pull-request builds with root privileges. Keep containers unprivileged (privileged = false, no --privileged), and use ephemeral runners for anything public so one job cannot influence the next. Isolate every job in Docker rather than running steps straight on the host, so a compromised build sees a throwaway container, not your machine.
Keep secrets in the CI system, not on disk. Store deploy keys and API tokens as GitHub Actions secrets or GitLab CI/CD variables, injected as environment variables at job time, never committed to the runner's filesystem or a config file. Rotate them if a runner is ever rebuilt.
The firewall is simple because a runner is outbound-only. It reaches out to GitHub or GitLab over HTTPS and holds the connection open; the platform pushes jobs down that link. There are no inbound ports to open, so the host firewall should default-deny inbound and allow only your SSH management port. That single fact removes most of the attack surface a public service would carry.
Keeping it healthy
CI fills disks. Old images, dangling layers and stopped containers pile up until a build fails with no space left. Automate the cleanup with a systemd timer rather than trusting yourself to remember.
The prune job: remove unused images, networks and build cache older than a week.
[Unit]
Description=Prune unused Docker data
[Service]
Type=oneshot
ExecStart=/usr/bin/docker system prune -af --filter "until=168h"
The schedule: run it weekly, catching up if the machine was off.
[Unit]
Description=Run docker system prune weekly
[Timer]
OnCalendar=Sun 04:00
Persistent=true
[Install]
WantedBy=timers.target
Enable the timer, then watch disk and Docker usage. Alert on the NVMe volume from your monitoring (node exporter, or whatever agent you set up earlier) well before it fills.
sudo systemctl daemon-reload
sudo systemctl enable --now docker-prune.timer
df -h /mnt/nvme
docker system df
Runner updates differ by platform. The GitHub Actions runner auto-updates itself while connected, so you mostly leave it alone. GitLab Runner is a normal apt package: apt-get upgrade gitlab-runner on your patch schedule keeps it current. Re-check both after any major OS upgrade.
Hosted runners vs self-hosted on dedicated
The tradeoff in one view. Neither column is wrong; the right choice follows your build volume and trust boundary.
| Hosted runners | Self-hosted on dedicated | |
|---|---|---|
| Cost model | Per build minute, scales with usage | Flat monthly, unlimited minutes |
| Queue time | Wait for a free runner at peak | Always available, jobs start instantly |
| Cache | Cold start, fetched over the network | Warm on local NVMe, persists between jobs |
| Maintenance | None, fully managed | You patch, prune and monitor |
| Isolation | Clean per job, safe for public repos | You enforce it: ephemeral plus containers |
| Best for | Light or bursty CI, small teams | High volume, slow cache-heavy builds |
The short version
If your CI bill is climbing and your builds are cache-heavy, a dedicated runner pays for itself in both money and waiting. Size it around cores and NVMe, isolate every job in Docker, use ephemeral runners for public code, keep the box outbound-only, and let a weekly prune and disk alert do the housekeeping. Start from the CI/CD runners category, harden the machine, and you have a build server that is faster and cheaper than metered minutes.
