Manual Installation of Gitea as a Systemd Service on Ubuntu VPS for Self-Hosting Git Repositories

Introduction

At ByteHosting, we understand the importance of reliable, self-hosted Git repositories for development teams and individual developers alike. Setting up Gitea on your Ubuntu VPS provides a lightweight, efficient platform for managing your codebases. In this guide, we will walk you through the step-by-step process of installing Gitea as a systemd service on Ubuntu. This approach ensures that your Gitea instance runs reliably, restarts automatically on failures, and integrates seamlessly with your system's service management.

Prerequisites: VPS Setup and Required Packages

Before we begin, ensure you have an Ubuntu VPS from ByteHosting. Our cost-effective KVM-based VPS plans, such as the Xeon - 4GB or 6GB options, are perfect for hosting Gitea. You should also have root or sudo access to your server.

Next, update your system packages to ensure everything is current:

sudo apt update && sudo apt upgrade -y

Install essential packages needed for the installation:

sudo apt install -y git wget sqlite3

Download and Install Gitea Binary

We will download the latest Gitea binary from the official release page. Visit Gitea releases to find the latest version. For simplicity, we'll use the latest stable release in this example.

Download the binary:

wget -O /usr/local/bin/gitea https://github.com/go-gitea/gitea/releases/latest/download/gitea--linux-amd64

Replace <latest-version> with the actual version number, e.g., 1.20.4. Make the binary executable:

sudo chmod +x /usr/local/bin/gitea

Configure Gitea as a systemd Service

Next, create a dedicated user for Gitea to run under:

sudo adduser --system --group --disabled-login --gecos "" gitea

Create necessary directories for Gitea:

sudo mkdir -p /var/lib/gitea/{custom,data,log} /etc/gitea

Set ownership to the Gitea user:

sudo chown -R gitea:gitea /var/lib/gitea /etc/gitea

Now, create a systemd service file:

sudo nano /etc/systemd/system/gitea.service

Insert the following configuration:

[Unit]
Description=Gitea (Git with a cup of tea)
After=network.target

[Service]
User=gitea
Group=gitea
WorkingDirectory=/var/lib/gitea
ExecStart=/usr/local/bin/gitea web -p 3000
Restart=always
Environment=USER=gitea HOME=/var/lib/gitea

[Install]
WantedBy=multi-user.target

Start and Enable Gitea Service

Reload systemd to recognize the new service:

sudo systemctl daemon-reload

Start Gitea:

sudo systemctl start gitea

Enable Gitea to start on boot:

sudo systemctl enable gitea

You can check the status with:

sudo systemctl status gitea

Access Gitea Web Interface and Initial Setup

Open your browser and navigate to http://your-server-ip:3000. You should see the Gitea setup page. Follow the on-screen instructions to complete the initial configuration, including database setup, admin user creation, and repository preferences.

Verify Gitea is Running Correctly

Once configured, ensure Gitea is running smoothly by checking the service status:

sudo systemctl status gitea

Visit the web interface again to confirm you can log in and create repositories. Your self-hosted Git management platform is now ready for use.

Troubleshoot Common Issues

Service Not Starting

If Gitea fails to start, check the logs:

journalctl -u gitea

Common issues include permission errors or missing dependencies. Ensure the /usr/local/bin/gitea binary is executable and owned by the gitea user.

Port Conflicts

If port 3000 is already in use, either stop the conflicting service or change the port in the systemd service file by adding -p <port> to the ExecStart command.

Conclusion

Installing Gitea as a systemd service on Ubuntu provides a robust, self-managed Git hosting solution. With our step-by-step guide, you can set up Gitea quickly and ensure it runs reliably on your ByteHosting VPS. Whether you're managing open-source projects or private repositories, this setup offers a cost-effective and flexible platform for your development needs.

Read more