How to Install Gogs Go Git Service on Ubuntu 16.04

Gogs is free and open source Git service written in Go language. Gogs is a painless self-hosted git service that allows you to create and run your own Git server on a minimal hardware server. Gogs web-UI is very similar to GitHub and offers support for MySQL, PostgreSQL, and SQLite database.

In this tutorial, we will show you step-by-step how to install and configure your own Git service using Gogs on Ubuntu 16.04. This tutorial will cover details including, how to install Go on Ubuntu system, install PostgreSQL, and install and configure Nginx web server as a reverse proxy for Go application.

Prerequisites

Ubuntu 16.04

Root privileges

What we will do

Update and Upgrade System

Install and Configure PostgreSQL

Install Go and Git

Install Gogs

Configure Gogs

Running Gogs as a Service

Install and Configure Nginx as a Reverse Proxy

Testing

Step 1 – Update and Upgrade System

Before going any further, update all Ubuntu repositories and upgrade all packages.

Step 2 – Install and Configure PostgreSQL

In this guide, we will be using PostgreSQL as a database for our Gogs installations.

Install PostgreSQL using the apt command below.

sudo apt install -y postgresql postgresql-client libpq-dev

After the installation is complete, start the PostgreSQL service and enable it to launch everytime at system boot.

systemctl start postgresqlsystemctl enable postgresql

PostgreSQL database has been installed on an Ubuntu system.

Next, we need to create a new database and user for Gogs.

Login as the ‘postgres’ user and run the ‘psql’ command to get the PostgreSQL shell.

su – postgrespsql

Create a new user named ‘git’, and give the user privileges for ‘CREATEDB’.

CREATE USER git CREATEDB;\password git

Create a database named ‘gogs_production’, and set the ‘git’ user as the owner of the database.

CREATE DATABASE gogs_production OWNER git;

New PostgreSQL database ‘gogs_production’ and user ‘git’ for Gogs installation has been created.

Step 3 – Install Go and Git

Install Git from the repository using the apt command below.

sudo apt install git

Now add new user ‘git’ to the system.

sudo adduser –disabled-login –gecos ‘Gogs’ git

Login as the ‘git’ user and create a new ‘local’ directory.

su – gitmkdir -p /home/git/local

Go to the ‘local’ directory and download ‘Go’ (the latest version) using the wget command as shown below.

cd ~/localwget https://dl.google.com/go/go1.9.2.linux-amd64.tar.gz

Extract the go compressed file, then remove it.

tar -xf go1.9.2.linux-amd64.tar.gzrm -f go1.9.2.linux-amd64.tar.gz

‘Go’ binary file has been downloaded in the ‘~/local/go’ directory. Now we need to setup the environment – we need to define the ‘GOROOT’ and ‘GOPATH directories so we can run a ‘go’ command on the system under ‘git’ user.

[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=65535Type=simpleUser=gitGroup=gitWorkingDirectory=/home/git/go/src/github.com/gogits/gogsExecStart=/home/git/go/src/github.com/gogits/gogs/gogs webRestart=alwaysEnvironment=USER=git HOME=/home/git

[Install]WantedBy=multi-user.target

Save and exit.

Now reload the systemd services.

systemctl daemon-reload

Start gogs service and enable it to launch everytime at system boot using the systemctl command.

systemctl start gogssystemctl enable gogs

Gogs is now running as a service on Ubuntu system.

Check it using the commands below.

netstat -plntusystemctl status gogs

And you should get the result as shown below.

Step 7 – Configure Nginx as a Reverse Proxy for Gogs

In this step, we will configure Nginx as a reverse proxy for Gogs. We will be using Nginx packages from its own repository.

Add Nginx repository using the add-apt command.

sudo add-apt-repository -y ppa:nginx/stable

Now update all Ubuntu repositories and install Nginx using the apt command below.