How to Run Private Git Repositories with Gogs

How to Run Private Git Repositories with Gogs

comments

Gogs is a popular self-hosted Git service written in the Go programming language. It allows you to run as many private Git repositories as your server can handle.

It's fast and lightweight. There's people who even run it on a Raspberry Pi. In fact, you can install Gogs on any system that the Go programming language can compile on - Windows, Linux, Mac and ARM.

If you'd like to test Gogs out before you install it, there's a demo available. Just register a account and you'll be able to test out its functionality.

For those who are experienced with GitHub, you'll notice Gogs has a similar interface.

gogs-project

Guide requirements

  • Ubuntu (with root access)
  • MySQL, PostgreSQL or SQLite

Step 1: Install dependencies

sudo apt-get install curl git

For the database, you can choose between MySQL, PostgreSQL, and SQLite

sudo apt-get install mysql-server
sudo apt-get install postgresql
sudo apt-get install sqlite3

Step 2: Create user account

sudo adduser --disabled-login --gecos 'Gogs' gogs
sudo su - gogs

Step 3: Install Gogs

You have two options, you can either install Gogs from source or install it as a precompiled binary. This guide will focus on installing Gogs from source.

Download the latest version of the Go programming language

mkdir /home/gogs/local
curl -kLo ~/go1.9.2.linux-amd64.tar.gz https://redirector.gvt1.com/edgedl/go/go1.9.2.linux-amd64.tar.gz
tar -C /home/gogs/local -xvzf go1.9.2.linux-amd64.tar.gz
rm -rf ~/go1.9.2.linux-amd64.tar.gz

Set up the environment

echo 'export GOROOT=$HOME/local/go' >> $HOME/.bashrc
echo 'export GOPATH=$HOME/go' >> $HOME/.bashrc
echo 'export PATH=$PATH:$GOROOT/bin:$GOPATH/bin' >> $HOME/.bashrc
source $HOME/.bashrc

Type go version. If Go was successfully installed, it will return the version number. Example: go version go1.9.2 linux/amd64

Download Gogs

go get -u github.com/gogits/gogs

Compile Gogs

cd ~/go/src/github.com/gogits/gogs
go build

Step 4: Create database

MySQL:

mysql -u root -pyour_password
CREATE DATABASE gogs CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE USER 'gogs'@'localhost' IDENTIFIED BY 'my_password';
GRANT ALL PRIVILEGES ON gogs.* TO 'gogs'@'localhost';

PostgreSQL:

CREATE ROLE gogs LOGIN ENCRYPTED PASSWORD 'my_password' NOINHERIT VALID UNTIL 'infinity';
CREATE DATABASE gogs WITH ENCODING='UTF8' OWNER=gogs;

Step 5: Test installation

Manually launch Gogs

/home/gogs/go/src/github.com/gogits/gogs/gogs web

Navigate to your_server_ip:3000 in your web browser. You should see a installation page where you can define the settings for Gog.

Go ahead and fill the proper settings in.

gogs-config

Step 6: Create Gogs service

sudo cp /home/gogs/go/src/github.com/gogits/gogs/scripts/systemd/gogs.service /etc/systemd/system/
sudo nano /etc/systemd/system/gogs.service

Modify values to:

User=gogs
Group=gogs
WorkingDirectory=/home/gogs/go/src/github.com/gogits/gogs/
ExecStart=/home/gogs/go/src/github.com/gogits/gogs/gogs web
Environment=USER=gogs HOME=/home/gogs

Enable and start gogs service

sudo systemctl enable gogs
sudo systemctl start gogs

Congratulations! You've successfully installed Gogs. Did you have trouble during one of the steps? Do you prefer GitLab or Bitbucket over Gogs? Let us know in the comments below.