Gitea is an efficient and user-friendly self-hosted Git server developed in Go. With its intuitive interface and feature-rich functionality, Gitea offers a wide range of capabilities including repository file editing, project issue tracking, user management, notifications, built-in wiki, and more. It provides a seamless and streamlined experience for managing your Git repositories.

Gitea is a lightweight and resource-efficient application that can be installed on low-powered systems. If you’re looking for a Gitlab alternative that offers a smaller memory footprint without sacrificing essential features, Gitea is worth considering. In this article, we will guide you through the process of installing and configuring Gitea on Ubuntu 20.04, providing you with a step-by-step tutorial to get started with this lightweight Git server solution.

Prerequisites

Gitea offers support for multiple database backends, including SQLite, PostgreSQL, and MySQL/MariaDB.

While SQLite is suitable for small installations, larger setups are recommended to use MySQL or PostgreSQL for improved performance and scalability.

In this tutorial, we will configure Gitea to use SQLite as the database backend. If SQLite is not installed on your Ubuntu system, you can easily install it by executing the following commands as a sudo user:

sudo apt update
sudo apt install sqlite3

Installing Gitea

Gitea offers various installation methods, including Docker images, source installation, binary installation, and package installation. In this guide, we will install Gitea from the binary distribution.

Install Git

The first step is to install Git on your server:

sudo apt update
sudo apt install git

Verify the installation by displaying the Git version:

git --version
Output
git version 2.25.1

Create a Git user

Create a new system user which will run the Gitea application by typing:

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


By running the command mentioned above, a new user and group named “git” will be created, and the home directory will be set to “/home/git”. The output will resemble the following:

Output
Adding system user `git' (UID 112) ...
Adding new group `git' (GID 118) ...
Adding new user `git' (UID 112) with group `git' ...
Creating home directory `/home/git' ...

Download Gitea binary


To download the latest Gitea binary for your architecture, visit the Gitea Download page. Ensure you select the appropriate version for your system. In the following command, we’ll assume the version is 1.10.2, but you should replace it with the actual version you intend to download:

VERSION=1.14.1
sudo wget -O /tmp/gitea https://dl.gitea.io/gitea/${VERSION}/gitea-${VERSION}-linux-amd64

This command utilizes wget to download the Gitea binary and saves it in the /tmp directory. Make sure to update the version number accordingly based on the latest available version.

You can run the gitea binary from any location. We’ll follow the convention and move the binary to the /usr/local/bin directory:

sudo mv /tmp/gitea /usr/local/bin

Make the binary executable:

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

You can create the necessary directories and set the appropriate permissions and ownership by running the following commands:

sudo mkdir -p /var/lib/gitea/{custom,data,log}
sudo chown -R git:git /var/lib/gitea/
sudo chmod -R 750 /var/lib/gitea/
sudo mkdir /etc/gitea
sudo chown root:git /etc/gitea
sudo chmod 770 /etc/gitea

The directory structure above is recommended by the official Gitea documentation.

The permissions of the /etc/gitea directory are set to 770 so that the installation wizard can create the configuration file. Once the installation is complete, we’ll set more restrictive permissions.

Create a Systemd Unit File

We’ll run Gitea as a systemd service.

Download the sample systemd unit file to the /etc/systemd/system directory by typing:

sudo wget https://raw.githubusercontent.com/go-gitea/gitea/main/contrib/systemd/gitea.service -P /etc/systemd/system/

You don’t need to edit the file, it is configured to match our setup.

Enable and start the Gitea service:

sudo systemctl daemon-reloadsudo systemctl enable --now giteaCopyCopy

Verify that Gitea is running:

sudo systemctl status gitea
● gitea.service - Gitea (Git with a cup of tea)
     Loaded: loaded (/etc/systemd/system/gitea.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2021-05-06 05:32:04 UTC; 7s ago
   Main PID: 77781 (gitea)
      Tasks: 6 (limit: 470)
     Memory: 130.6M
     CGroup: /system.slice/gitea.service
             └─77781 /usr/local/bin/gitea web --config /etc/gitea/app.ini
...

Configure Gitea

To allow traffic on port 3000 for Gitea, you can use the following command if you’re using UFW firewall:

sudo ufw allow 3000/tcp

This command will open port 3000 in the firewall, allowing incoming connections to Gitea.

After running the command, you can proceed with accessing the Gitea web interface to finalize the installation and configuration.

To access the Gitea web interface, open your preferred browser and enter the following URL: http://YOUR_DOMAIN_OR_IP:3000

Once you enter the URL, you should see a screen similar to the one shown below:

From there, you can proceed with the necessary setup and configuration steps to complete the Gitea installation.

Database Settings:

Database Type: SQLite3
Path: Use an absolute path, /var/lib/gitea/data/gitea.db
Application General Settings:

Site Title: Enter your organization name.
Repository Root Path: Leave the default var/lib/gitea/data/gitea-repositories.
Git LFS Root Path: Leave the default /var/lib/gitea/data/lfs.
Run As Username: git
SSH Server Domain: Enter your domain or server IP address.
SSH Port: 22, change it if SSH is listening on other Port
Gitea HTTP Listen Port: 3000
Gitea Base URL: Use http and your domain or server IP address.
Log Path: Leave the default /var/lib/gitea/log

You can change the settings at any time by editing the Gitea configuration file.

To begin the installation process, click on the “Install Gitea” button.

The installation will be completed instantly, and you will then be redirected to the login page.

Click on the “Need an account? Register now.” link. The first registered user is automatically added to the Admin group.

Change the permissions of the Gitea configuration file to read-only using:

sudo chmod 750 /etc/gitea
sudo chmod 640 /etc/gitea/app.ini

That’s it. Gitea has been installed on your Ubuntu machine.

Configuring Nginx as SSL Termination Proxy

This step is optional, but it is highly recommended. SSL termination means that Nginx will acts as an intermediary point between the Gitea application and web clients so you can access Gitea via HTTPS.

To use Nginx as a reverse proxy, you need to have a domain or subdomain pointing to your server’s public IP. In this example, we will use git.example.com.

First, install Nginx and generate a free Let’s Encrypt SSL certificate using the guides below:

How To Install Nginx on Ubuntu 20.04
Secure Nginx with Let’s Encrypt on Ubuntu 20.04
Once done, open your text editor and edit the domain server block file:

sudo nano /etc/nginx/sites-enabled/git.example.com
/etc/nginx/sites-enabled/git.example.com
server {
    listen 80;
    server_name git.example.com;

    include snippets/letsencrypt.conf;
    return 301 https://git.example.com$request_uri;
}

server {
    listen 443 ssl http2;
    server_name git.example.com;

    proxy_read_timeout 720s;
    proxy_connect_timeout 720s;
    proxy_send_timeout 720s;

    client_max_body_size 50m;

    # Proxy headers
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;

    # SSL parameters
    ssl_certificate /etc/letsencrypt/live/git.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/git.example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/git.example.com/chain.pem;
    include snippets/letsencrypt.conf;
    include snippets/ssl.conf;

    # log files
    access_log /var/log/nginx/git.example.com.access.log;
    error_log /var/log/nginx/git.example.com.error.log;

    # Handle / requests
    location / {
       proxy_redirect off;
       proxy_pass http://127.0.0.1:3000;
    }
}

Make sure to replace “git.example.com” with your actual Gitea domain and set the correct path to the SSL certificate files. This configuration ensures that all HTTP traffic is redirected to HTTPS.

Restart the Nginx service to apply the changes and make them take effect.

sudo systemctl restart nginx

Next, change the Gitea domain and root url. To do so, open the configuration file and edit the following lines:

sudo nano /etc/gitea/app.ini
/etc/gitea/app.ini
[server]
DOMAIN           = git.example.com
ROOT_URL         = https://git.example.com/

Restart the Gitea service by typing:

sudo systemctl restart gitea

At this stage, the Gitea proxy is configured successfully, and you can now access it at the following URL: https://git.example.com.

Configuring Email Notifications

To enable email notifications in your Gitea instance, you have two options: either install Postfix on your server or utilize a transactional mail service like SendGrid, MailChimp, MailGun, or SES.

To configure email notifications, open the Gitea configuration file and make the following changes to the respective lines:

sudo nano /etc/gitea/app.ini
/etc/gitea/app.ini
[mailer]
ENABLED = true
HOST    = SMTP_SERVER:SMTP_PORT
FROM    = SENDER_EMAIL
USER    = SMTP_USER
PASSWD  = YOUR_SMTP_PASSWORD

Make sure you put the correct SMTP server information.

Restart the Gitea service for changes to take effect:

sudo systemctl restart gitea

To verify the settings and send a test email, log in to Gitea, and go to: Site Administration > Configuration > SMTP Mailer Configuration.

Gitea also allows you to connect to Slack by creating a web webhook and send notifications to your Slack channels .

Upgrading Gitea

To upgrade to the latest Gitea version, simply download and replace the binary.

Stop the Gitea service

sudo systemctl stop gitea

Download the latest Gitea version and move it to the /usr/local/bin directory:

VERSION=<THE_LATEST_GITEA_VERSION>
wget -O /tmp/gitea https://dl.gitea.io/gitea/${VERSION}/gitea-${VERSION}-linux-amd64
sudo mv /tmp/gitea /usr/local/bin

Make the binary executable

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

Restart the Gitea service

sudo systemctl restart gitea

Conclusion

This tutorial provided a step-by-step guide to installing Gitea on Ubuntu 20.04. For further details on configuring your Gitea instance and creating your initial project, please refer to the Gitea documentation page.

If you have any questions or need assistance, please don’t hesitate to leave a comment below. We’re here to help!

Comments to: How to Install Gitea on Ubuntu 20.04

    Your email address will not be published. Required fields are marked *