(Last Updated On: September 23, 2018)

This tutorial will cover all the steps required to Install Gogs self-hosted Git service on Ubuntu 18.04 and Ubuntu 16.04 server. Gogs project which is written in Go aims to build a simple, stable and extensible self-hosted Git service with a painless setup process. Gogs is distributed as an independent binary distribution and runs on all platforms that Go supports, including Linux, macOS, Windows, and ARM.

Gogs has excellent performance and is very light. It has very minimal RAM and CPU utilization. A VPS with 1 CPU core and 256MB RAM is enough to start using the service.

Features of Gogs

Gogs self-hosted Git service has the following set of features ( from Git )

  • Activity timeline
  • SMTP/LDAP/Reverse proxy authentication
  • Account/Organization/Repository management
  • Repository Git hooks/deploy keys
  • SSH and HTTP/HTTPS protocols
  • Reverse proxy with sub-path
  • Add/Remove repository collaborators
  • Web editor for repository files and wiki
  • Migrate and mirror repository and its wiki
  • Repository/Organization webhooks (including Slack and Discord)
  • Repository issues pull requests, wiki, and protected branches
  • Two-factor authentication
  • Gravatar and Federated avatar with custom source
  • Jupyter Notebook
  • Administration panel
  • Mail service
  • Supports MySQL, PostgreSQL, SQLite3, MSSQL and TiDB (via MySQL protocol)
  • Multi-language support (29 languages)

Gogs Open Source Components

Gogs components include:

Install Gogs self-hosted Git service on Ubuntu 18.04

Follow these steps to have Gogs installed on your Ubuntu 18.04 server.

Step 1: Install MariaDB Database Server

Gogs requires a MySQL server to store data. We will install MariaDB server using the following guide:

Install MariaDB 10.3 on Ubuntu 18.04 and CentOS 7

Once the database server has been installed, login to MariaDB console as root user

$ mysql -u root -p

Set global variables below

SET GLOBAL innodb_file_per_table = ON;

Then create gogs database

DROP DATABASE IF EXISTS gogs;
CREATE DATABASE IF NOT EXISTS gogs CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

Create a dedicated database user to manage gogs database.

$ mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 48
Server version: 10.3.9-MariaDB-1:10.3.9+maria~bionic-log mariadb.org binary distribution

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> GRANT ALL PRIVILEGES ON gogs.* TO 'gogs'@'localhost' IDENTIFIED BY "StrongPassword";
Query OK, 0 rows affected (0.002 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> \q
Bye

Step 2: Download Gogs from Github

Check Gogs releases page  https://github.com/gogs/gogs/releases for the latest release. As of the writing of this article, version 0.11.66 is the latest.

sudo apt install wget
export VER=0.11.66 
wget https://github.com/gogs/gogs/releases/download/v${VER}/linux_amd64.tar.gz

Extract the downloaded file

tar xvf linux_amd64.tar.gz
rm -f linux_amd64.tar.gz

Step 3: Configure Gogs self-hosted Git service

Create git user to run gogs service

$ sudo adduser git
Adding user `git' ...
Adding new group `git' (1001) ...
Adding new user `git' (1001) with group `git' ...
Creating home directory `/home/git' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
Changing the user information for git
Enter the new value, or press ENTER for the default
	Full Name []: 
	Room Number []: 
	Work Phone []: 
	Home Phone []: 
	Other []: 
Is the information correct? [Y/n] y

Create logs directory

sudo mkdir /var/log/gogs
sudo chown -R git:git /var/log/gogs/

Copy gogs systemd service file to /etc/systemd/system directory

sudo cp gogs/scripts/systemd/gogs.service /etc/systemd/system

Edit the file to set correct variables

sudo vim /etc/systemd/system/gogs.service

Edit like below

[Unit]
Description=Gogs self-hosted Git service
After=syslog.target
After=network.target
After=mysql.service

[Service]
# Modify these two values and uncomment them if you have
# repos with lots of files and get an HTTP error 500 because
# of that
###
#LimitMEMLOCK=infinity
#LimitNOFILE=65535
Type=simple
User=git
Group=git
WorkingDirectory=/home/git
ExecStart=/home/git/gogs web
Restart=always
Environment=USER=git HOME=/home/git

# Some distributions may not support these hardening directives. If you cannot start the service due
# to an unknown option, comment out the ones not supported by your version of systemd.
ProtectSystem=full
PrivateDevices=yes
PrivateTmp=yes
NoNewPrivileges=true

[Install]
WantedBy=multi-user.target
If you have a different service using port,3000 you can set a custom port
ExecStart=/home/git/gogs web -port 3001
Copy gogs directory content inside /home/gogs
sudo rsync -avz gogs/* /home/git/

Change ownership of /home/git/to user.git

sudo chown -R git:git /home/git/

Reload systemd and start gogs service

sudo systemctl daemon-reload
sudo systemctl start gogs

Enable the service to start on boot

sudo systemctl enable gogs

You can check service status by running

sudo systemctl status gogs

Typing commandgogs on the terminal as git user should print help page.

$ ./gogs
NAME:
   Gogs - A painless self-hosted Git service

USAGE:
   gogs [global options] command [command options] [arguments...]

VERSION:
   0.11.66.0916

COMMANDS:
     web      Start web server
     serv     This command should only be called by SSH shell
     hook     Delegate commands to corresponding Git hooks
     cert     Generate self-signed certificate
     admin    Perform admin operations on command line
     import   Import portable data as local Gogs data
     backup   Backup files and database
     restore  Restore files and database from backup
     help, h  Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --help, -h     show help
   --version, -v  print the version

Step 4: Configure Gogs self-hosted Git service

Open the URL http://serverip:3000/install to finish the installation of Gogs

Set database authentication

The username and password provided should match the ones provided in the Database configuration section. If the database server is on a different host, provide the IP address under the Host section.

Set Application General Settings

Provide application URL, this can be a routable server IP address or domain name that resolves to the IP. The same should be set for SSH.

Disable User self-registration

You can disable User self-registration under “Server and Other Services Settings”. This means the admin user will manually create user accounts.

You can optionally create an admin user account. By default, root user will gain admin access automatically.

When done with the configurations, click the “Install Gogs” button to finish the installation. On a successful installation, you should be logged into Gogs administration console.

Create additional user accounts

To add user accounts, Open Admin Panel at the top right panel

You can then use “Create New Account” link to add a new user to Gogs.

Step: 5 Test Gogs self-hosted Git service

Create a new repository called test-repo

Try cloning the repo

$ git clone http://192.168.18.40:3000/jmutai/test-repo.git
Cloning into 'test-repo'...
Username for 'http://192.168.18.40:3000': jmutai
Password for 'http://[email protected]:3000': <Enter Password>
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (4/4), done.

To avoid Password Authentication, add your SSH public key under “Your Profile > SSH keys > Add Key

Provide key name, and paste your SSH public key into the content box.

An ssh git clone should not ask for a password

$ git clone [email protected]:jmutai/test-repo.git
Cloning into 'test-repo'...
Warning: Permanently added '192.168.18.40' (ECDSA) to the list of known hosts.
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (4/4), done.

Step 6: Configure Nginx Reverse Proxy

If you would like to access Gogs using a domain name without a port, you need to configure Apache or NGINX reverse proxy

sudo apt install nginx

Create a VirtualHost reverse proxy configuration file

sudo vim /etc/nginx/conf.d/gogs.conf

Add:

server {
    listen 80;
    server_name git.example.com;

    location / {
        proxy_pass http://localhost:3000;
    }
}

If using SSL and would like to have http to https redirect, use

server {
	listen 80;
        server_name  git.example.com;
	location / {
        	rewrite     ^ https://git.example.com$request_uri? permanent;
    }
}

server {
        listen       443 ssl;
	ssl_certificate /etc/letsencrypt/live/git.example.com/fullchain.pem;
	ssl_certificate_key /etc/letsencrypt/live/git.example.com/privkey.pem;
        server_name git.example.com;

        location / {
            proxy_pass http://localhost:3000;
        }
}

Replace git.example.com with your actual domain name. For more reading on Gogs administration and usage, check out the Official Documentation.