How to host your own local Git repository with Gogs

Hosting your own code repository has many benefits, such as security and ease of use. Jack Wallen shows you how to deploy a local repository to your data center with Gogs.

Software developer freelancer woman working at night

Image: iStockphoto/monstArrr_

Your company might have code that you'd rather not risk hosting on a third party. Although the likes of GitHub should generally be trusted, you simply never know when disaster might strike. Of course, in this modern era, that could happen anytime, anywhere. Or, maybe you want to hand over the control of code collaboration to your developers, and give them all of the tools they need to make that work.

One such tool is the local code repository. There are a number of routes you can take to make this a reality for your development team--one of which is Gogs

Gogs is an easy tool to deploy that includes a user-friendly web-based GUI. You can run it on Linux, macOS, or Windows, from a standalone binary, or install it from source.

I'm going to walk you through the process of deploying Gogs on Ubuntu Server 20.04, so you can get this service up and running in your data center, be it on-premise or a cloud-hosted.

SEE: Kubernetes security guide (free PDF) (TechRepublic)

What you'll need

The only things you'll need to get Gogs up and running are:

  • A running instance of Ubuntu Server 20.04

  • A user with sudo privileges

How to install and create the database

The first thing we must do is install the database. You can use SQLite3, but if your projects tend to be larger, you'll want to have a database available to handle the scale.

Install MariaDB with the command:

sudo apt-get install mariadb-server -y

Once installed, secure the database server with the command:

sudo mysql_secure_installation

Answer the required questions and you're ready to create your database.

Log in to the MariaDB console with the command:

sudo mariadb

Create the Gogs database:

CREATE SCHEMA `gogs` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

Create the gogs user:

GRANT ALL PRIVILEGES ON gogs.* TO 'gogs'@'localhost' IDENTIFIED BY 'PASSWORD';

Where PASSWORD is a strong/unique password.

Flush the privilege table with the command:

FLUSH PRIVILEGES;

Exit from the database console with:

exit

How to create a new user and unpack Gogs

We're going to run Gogs from a new system user, named git. First, we'll create the user:

sudo adduser --system --group --disabled-password --shell /bin/bash --home /home/git --gecos 'Gogs User' git

It's now time to download and unpack Gogs. Before you do this, make sure to check the Gogs download page for the latest release number. As of this writing, that will be 0.12.3.

Download the latest file with the command:

wget https://dl.gogs.io/0.12.3/gogs_0.12.3_linux_amd64.tar.gz

Extract the archive and move it to /opt/gogs with the command:

sudo tar xf /tmp/gogs_*_linux_amd64.tar.gz -C /home/git

Change the ownership of the new Gogs directory with:

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

How to create the systemd file and start the service

The Gogs download includes a prefab systemd file that you can easily copy with the command:

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

Once the file is in place, start and enable Gogs with the commands:

sudo sytemctl start gogs
sudo systemctl enable gogs

How to complete the installation from the web GUI

Open a web browser and point it to http://SERVER:3000 (where SERVER is the IP address or domain of your server). You will be greeted by the Gogs installation page, where you must configure the database parameters (Figure A).

Figure A

gogsinstaller.jpg

The Gogs web-based installer is ready to go.

Make sure to select MySQL from the database dropdown and fill out the following:

  • Host: 127.0.0.1:3306

  • User: gogs

  • Password: The password you created along with the gogs user

  • Database Name: gogs

  • Domain: Domain or IP address of your hosting server

You shouldn't have to change anything else. Click Install Gogs and the installation will start and complete fairly quickly. Once the installation completes, the login page will appear. Click the Register button. Register your account, log in with the credentials you just created, and you'll find yourself at the Gogs main page (Figure B).

Figure B

gogsmain.jpg

Gogs is installed and ready to host your code.

That's it, Gogs is now up and running in your data center. You can customize it to perfectly meet your needs and share it out with your developer team, so they can start collaborating.

Subscribe to TechRepublic's How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.

Also see