Back to Developer Tools
Developer Tools

How to Set Up a Self-Hosted Git Server with Gitea on Ubuntu

SedulousWeb
How to Set Up a Self-Hosted Git Server with Gitea on Ubuntu

Learn how to install and configure Gitea, a lightweight self-hosted Git server, on Ubuntu. Step-by-step guide for developers to manage repositories privately and efficiently.

Introduction

Self-hosting a Git server gives developers full control over their repositories, ensuring privacy, customization, and cost efficiency. Gitea is a lightweight, open-source Git service that is easy to set up and maintain, making it an ideal choice for small teams or individual developers. This guide walks you through installing and configuring Gitea on an Ubuntu server, enabling you to host your own Git repositories without relying on third-party services like GitHub or GitLab.

Prerequisites

  • A server running Ubuntu 20.04 or 22.04 (LTS versions recommended).
  • Root or sudo access to the server.
  • A domain name pointed to your server’s IP (optional but recommended for HTTPS).
  • Basic familiarity with the Linux command line.
  • Git installed on your local machine for testing.

Step 1: Update System Packages

Before installing Gitea, ensure your system is up to date. Run the following commands to update and upgrade existing packages:

sudo apt update && sudo apt upgrade -y

Reboot the server if a kernel update is applied:

sudo reboot

Step 2: Install Dependencies

Gitea requires a few dependencies, including Git, a database (SQLite, MySQL, or PostgreSQL), and a web server (optional, as Gitea includes a built-in server). For this guide, we’ll use SQLite for simplicity. Install the required packages:

sudo apt install -y git sqlite3 nginx certbot python3-certbot-nginx

Step 3: Create a Git User

For security, create a dedicated user to run Gitea. This user will not have shell access by default:

sudo adduser --system --group --disabled-password --shell /bin/bash --home /home/git --gecos 'Git Version Control' git

Step 4: Download and Install Gitea

Download the latest binary release of Gitea from the official website. Replace X.Y.Z with the latest version number (e.g., 1.21.0):

wget -O gitea https://dl.gitea.io/gitea/X.Y.Z/gitea-X.Y.Z-linux-amd64
sudo mv gitea /usr/local/bin/gitea
sudo chmod +x /usr/local/bin/gitea

Verify the installation by checking the Gitea version:

gitea --version

Step 5: Configure Gitea as a System Service

Create a systemd service file to manage Gitea as a background service:

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

Add the following content to the file:

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

[Service]
User=git
Group=git
WorkingDirectory=/home/git
ExecStart=/usr/local/bin/gitea web --config /etc/gitea/app.ini
Restart=always
Environment=USER=git HOME=/home/git

[Install]
WantedBy=multi-user.target

Reload the systemd daemon and start Gitea:

sudo systemctl daemon-reload
sudo systemctl enable --now gitea

Check the status to ensure Gitea is running:

sudo systemctl status gitea

Step 6: Configure Nginx as a Reverse Proxy (Optional)

To access Gitea via a domain name and enable HTTPS, configure Nginx as a reverse proxy. First, create an Nginx configuration file:

sudo nano /etc/nginx/sites-available/gitea

Add the following configuration, replacing yourdomain.com with your actual domain:

server {
    listen 80;
    server_name yourdomain.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Enable the configuration and test Nginx:

sudo ln -s /etc/nginx/sites-available/gitea /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

Step 7: Secure Gitea with HTTPS

Use Certbot to obtain a free SSL certificate from Let’s Encrypt:

sudo certbot --nginx -d yourdomain.com

Follow the prompts to configure HTTPS. Certbot will automatically update your Nginx configuration.

Step 8: Complete Gitea Setup via Web Interface

Open a web browser and navigate to http://yourdomain.com (or your server’s IP if no domain is configured). You’ll be greeted by the Gitea setup page. Fill in the following details:

  • Database Settings: Select SQLite3 (or your chosen database).
  • Application Name: Your Git server’s name (e.g.,

Stay Updated

Get top Indian news directly to you

Daily News Digest

No spam. Unsubscribe anytime.