How to Install Gitea on Ubuntu 20.04 with SQLite 3

Gitea is an open-source forge software package for hosting software development version control using Git and other collaborative features like bug tracking, wikis and code review. It supports self-hosting but also provides a free public first-party instance hosted in China on DiDi’s cloud. Gitea also has a much smaller memory footprint than Gitlab and is a highly recommended software for companies and developers on low-powered systems.

In the following guide, you will learn how to install and configure Gitea on Ubuntu 20.04 using the SQLite 3 database. Suppose you have not installed SQLite 3 before. Feel free to check out our guide on Installing SQLite 3 and SQLite 3 Browser in Ubuntu 20.04.

Prerequisites

Check and update your Ubuntu system.

sudo apt update && sudo apt upgrade -y

Install SQLite 3, following our guide for better details.

sudo apt install sqlite3

Install Git. Follow our guide on how to install the latest version of Git on Ubuntu 20.04.

sudo apt install git

Last, make sure either curl or wget is installed.

sudo apt install wget curl

Create Git user to run Gitea

Before jumping too far ahead, the first step is to create a Git user that will run the Gitea application. To create a new Git user account, type the following command.

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

You should get the following output:

~$ sudo adduser \
   --system \
      --shell /bin/bash \
      --gecos 'Git Version Control' \
      --group \
      --disabled-password \
      --home /home/git \
      git
   [sudo] password for bytesboss: 
   Adding system user git' (UID 129) ... Adding new groupgit' (GID 136) …
   Adding new user git' (UID 129) with groupgit' …
   Creating home directory `/home/git' … 

Download Gitea

Next, visit the Gitea download’s page and find the version that suits Ubuntu architecture. Currently, version 1.14.3 is the latest version of Gitea. However, this will change over time. Once you find the updated link, download the package using the wget command as follows.

wget https://dl.gitea.io/gitea/${VERSION}/gitea-${VERSION}-linux-amd64

The download is roughly just over 100MB. Once done, move the binary file into its final location and give it execute permission so you can proceed to the install phase.

sudo mv gitea-1.14.3-linux-amd64 /usr/local/bin \
 sudo mv gitea-1.14.3-linux-amd64 gitea \
 sudo chmod +x gitea

Create Gitea Directories

You will need to create the directories yourself for Gitea. Below is the recommended directory structure that is “recommended” by Gitea. Enter the following to make your directories.

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 \

Note, the “/etc/gitea” directory is currently on chmod 777 permission. This is done so the installation can proceed smoothly without any hiccups. Once done, you will need to change this to a more secure chmod permission.

Create Gitea Systemd Service

Next, to install and run Gitea, you will need to make a Systemd file. Download the one provided by Gitea requires no editing whatsoever by entering the following command.

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

Now, reload your daemon and enable the Gitea systemd service.

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

Next, verify that Gitea is running and that no errors have occurred. The most common issue is people forgetting to set up directories. If you see this, then go back a few steps and repeat the daemon-reload process.

The output should be similar to the below if set up correctly.

$ 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 Sat 2021-06-26 22:47:23 PDT; 7s ago
    Main PID: 3214 (gitea)
       Tasks: 8 (limit: 4617)
      Memory: 184.9M
      CGroup: /system.slice/gitea.service
              └─3214 /usr/local/bin/gitea web --config /etc/gitea/app.ini

Set-Up Gitea through Web UI

Once the Gitea service is running smoothly, the following process launches the web UI to finish off the installation. By default, Gitea listens to port 3000 on all network interfaces.

UFW

If you have UFW, you need to allow the port as per below.

sudo ufw allow 3000/tcp

Web UI

Open your Internet Browser and type in the server’s IP address hosted on with:3000 as the port.

Example:

http://192.168.53.101:3000

If you have set up the port allow rule correctly, you will come to the following screen below.

ubuntu 20.04 gitea web ui first setting page

You will need to edit the settings as per below. Note that these can be changed easily later on.

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

Once complete, you can hit the “Install Gitea” button located at the bottom of the settings page.

gita install now ubuntu 20.04

Once the installation is complete, you will be re-directed to a login page. You will need to register an account that will automatically be made the administrator’s first account sign up.

ubuntu 20.04 lts gitea admin account

You have created an admin account on the Gitea service, and you have everything set up. It would help if you now secured your folders. As explained at the start of the guide, they were made 777 for a smooth installation. Next, enter the following command to change the permissions secure.

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

Congratulations, you have successfully installed Gitea on your Ubuntu 20.04 LTS operating system.

Set-Up Gitea Email Notifications

Gitea can send notifications by email. You can install Postfix or any other package that uses standard mail commands. To enable the notifications to open up the following file using nano editor:

sudo nano /etc/gitea/app.ini

Next, edit the following template below with your username, password and SMTP server details.

[mailer]
 ENABLED = true
 HOST    = SMTP_SERVER:SMTP_PORT
 FROM    = SENDER_EMAIL
 USER    = SMTP_USER
 PASSWD  = YOUR_SMTP_PASSWORD

Once completed, restart the Gitea service.

sudo systemctl restart gitea

It is recommended to go to the web UI, log in to your admin account, and send a test email to verify that everything is working correctly. The path to do this is Administration > Configuration > SMTP Mailer Configuration.

Set-Up Nginx as a Reverse SSL Proxy for Gitea

The next step is recommended if serving Gitea outside an internal network. Anything with outside connections coming in should be set up with SSL and secured when communicating with your backend.

First, create a new configuration file for your Gitea site or modify an existing one:

sudo nano /etc/nginx/sites-enabled/gitea.conf

Then enter the following. This is a guide only. SSL can be done with services like Cloudflare or Let’s Encrypt.

server {
   listen 80;
   server_name git.example.com;
   include snippets/letsencrypt.conf;
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;
  }
 }

How to Upgrade Gitea

To update your Gitea installation, all that needs to be done is re-download the newest binary file of Gitea, repeat the first steps at the start of the guide. An example of the rundown as per below:

Stop the Gitea service:

sudo systemctl stop gitea

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

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

Make Gitea binary file executable as per the start of the instructions of this guide.

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

Restart the Gitea service:

sudo systemctl restart gitea

Comments and Conclusion

The guide has shown you how to install Gitea on Ubuntu 20.04 LTS and the basic requirements in setting up. Overall, Gitea is a solid piece of software that is actively developed with a very active community. Some of the extra features include:

  • Easy to manage repositories.
  • Uses HTTP / HTTPS, Git and SSH protocols.
  • Periodically updated.
  • Issue management.
  • Time track for code.
  • Visual, simple to use.
  • Encourages collaboration by integrating pull requests and branches.

If you have questions, feel free to leave a comment below.



Follow LinuxCapable.com!

Like to get automatic updates? Follow us on one of our social media accounts!