How To Install Gogs Git service on Ubuntu 20.04|18.04

Posted on 89 views

This tutorial will cover all the steps required to Install Gogs self-hosted Git service on Ubuntu 20.04|18.04|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 Git service on Ubuntu 20.04|18.04

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

Step 1: Install MariaDB Database Server

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

sudo apt update
sudo apt install mariadb-server mariadb

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
GRANT ALL PRIVILEGES ON gogs.* TO 'gogs'@'localhost' IDENTIFIED BY "StrongPassword";
FLUSH PRIVILEGES;
\q

Step 2: Download Gogs from Github

Check Gogs releases page  https://github.com/gogs/gogs/releases for the latest release.

sudo apt install wget curl vim -y
curl -s https://api.github.com/repos/gogs/gogs/releases/latest | grep browser_download_url | grep '\linux_amd64.tar.gz' | cut -d '"' -f 4 | wget -i -

Extract the downloaded file

tar xvf gogs_*_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

You can edit the file to set customize the settings:

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

If you have a different service using port,3000 you can set a custom port

ExecStart=/home/git/gogs web -port 3001

Move the gogs directory content inside /home/git

sudo mv 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

$ systemctl status gogs
● gogs.service - Gogs
     Loaded: loaded (/etc/systemd/system/gogs.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2021-10-26 23:12:31 UTC; 13s ago
   Main PID: 3404 (gogs)
      Tasks: 8 (limit: 2343)
     Memory: 41.4M
     CGroup: /system.slice/gogs.service
             └─3404 /home/git/gogs/gogs web

Oct 26 23:12:31 jmutai-ubuntu-01 gogs[3404]: 2021/10/26 23:12:31 [TRACE] Log mode: Console (Trace)
Oct 26 23:12:31 jmutai-ubuntu-01 gogs[3404]: 2021/10/26 23:12:31 [ INFO] Gogs 0.12.3
Oct 26 23:12:31 jmutai-ubuntu-01 gogs[3404]: 2021/10/26 23:12:31 [TRACE] Work directory: /home/git/gogs
Oct 26 23:12:31 jmutai-ubuntu-01 gogs[3404]: 2021/10/26 23:12:31 [TRACE] Custom path: /home/git/gogs/custom
Oct 26 23:12:31 jmutai-ubuntu-01 gogs[3404]: 2021/10/26 23:12:31 [TRACE] Custom config: /home/git/gogs/custom/conf/app.ini
Oct 26 23:12:31 jmutai-ubuntu-01 gogs[3404]: 2021/10/26 23:12:31 [TRACE] Log path: /home/git/gogs/log
Oct 26 23:12:31 jmutai-ubuntu-01 gogs[3404]: 2021/10/26 23:12:31 [TRACE] Build time: 2020-10-07 03:03:48 UTC
Oct 26 23:12:31 jmutai-ubuntu-01 gogs[3404]: 2021/10/26 23:12:31 [TRACE] Build commit: f0e3cd90f8d7695960eeef2e4e54b2e717302f6c
Oct 26 23:12:31 jmutai-ubuntu-01 gogs[3404]: 2021/10/26 23:12:31 [ INFO] Run mode: Development
Oct 26 23:12:31 jmutai-ubuntu-01 gogs[3404]: 2021/10/26 23:12:31 [ INFO] Listen on http://0.0.0.0:3000

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

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

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

VERSION:
   0.12.3

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

install-gogs-git-service-ubuntu-18.04-configure-mysql-min-1024x427

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.

install-gogs-git-service-ubuntu-18.04-configure-application-min-1024x674

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.

install-gogs-git-service-ubuntu-18.04-disable-self-registration-min-1024x465

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

install-gogs-git-service-ubuntu-18.04-configure-admin-account-min-1024x388

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

install-gogs-git-service-ubuntu-18.04-configure-open-admin-panel-min-1024x692

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

install-gogs-git-service-ubuntu-18.04-add-user-accounts-min-1024x384

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': 
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

gogs-manage-ssh-keys-min-1024x269

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

gogs-add-ssh-keys-min-1024x463

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;
        server_name git.example.com;
	ssl_certificate /etc/letsencrypt/live/git.example.com/fullchain.pem;
	ssl_certificate_key /etc/letsencrypt/live/git.example.com/privkey.pem;

        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.

coffee

Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.