Linux

How to Install and Use Gogs for Git Hosting on Ubuntu

How to Install and Use Gogs for Git Hosting on Ubuntu

Gogs is a lightweight, self-hosted Git service that provides a simple way to manage repositories. It’s known for being easy to set up and run with minimal resources. Gogs is an excellent solution for individuals or teams looking to host their own Git repositories without relying on external services like GitHub or GitLab. This guide will walk you through the process of installing and using Gogs on Ubuntu, covering every necessary step from server setup to repository management.

Prerequisites
Before we dive into the installation process, make sure your server meets the following requirements:

  • Ubuntu 20.04 or 22.04: This guide assumes you’re using a fresh installation of Ubuntu.
  • A non-root user: Set up with sudo privileges.
  • A server: With at least 512MB of RAM and a reasonable amount of storage for your repositories.
  • MySQL, PostgreSQL, or SQLite: Gogs supports multiple databases, but this guide will focus on using SQLite for simplicity.

Step 1: Update Your System
First, ensure your server is up-to-date by running the following commands:

sudo apt update
sudo apt upgrade

This will ensure all your packages and system libraries are the latest versions.

Step 2: Install Git
Gogs is a Git service, so you need to have Git installed on your system. You can install it using the command:

sudo apt install git

Once installed, verify the installation by checking the Git version:

git –version

You should see the installed version of Git, confirming that it is correctly set up.

Step 3: Download and Install Gogs
Gogs provides pre-built binaries for multiple platforms, including Ubuntu. You can download the latest version of Gogs from their official website, but we will use the terminal to fetch it.

First, go to the /opt directory, where we’ll place Gogs:

cd /opt

Then, download the Gogs binary:

wget https://dl.gogs.io/0.12.10/gogs_0.12.10_linux_amd64.tar.gz

Once downloaded, extract the tarball:

tar -xvf gogs_0.12.10_linux_amd64.tar.gz

This will create a gogs directory in /opt.

Step 4: Create a Gogs User
For security purposes, it’s recommended to run Gogs under a separate, non-root user. You can create this user with the following command:

sudo adduser –system –shell /bin/bash –group –disabled-password –home /home/git git

This creates a git user with its home directory set to /home/git. Now, change the ownership of the Gogs directory to the git user:

sudo chown -R git:git /opt/gogs

Step 5: Configure Gogs
Next, we’ll configure Gogs before launching it. Switch to the git user:

sudo su – git

Navigate to the Gogs directory:

cd /opt/gogs

Start Gogs for the first time to generate the necessary configuration files:

./gogs web

After running the command, Gogs will start a local web server, accessible at http://:3000. Open this URL in your browser to continue the setup.

Step 6: Web-Based Setup
When you access Gogs for the first time, you’ll be greeted by a setup page. The configuration options are straightforward:

  • Database Type: Select SQLite3 for simplicity, unless you’re using MySQL or PostgreSQL.
  • Application URL: Set this to http://:3000.
  • Admin Account: Provide an administrator username, email, and password.

Once you’ve filled out the form, click the Install button. Gogs will set up the database and create the necessary files.

Step 7: Run Gogs as a Service
To ensure Gogs runs automatically when your server starts, you should create a systemd service. Exit from the git user shell and create a new service file:

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

Paste the following configuration into the file:

[Unit]
Description=Gogs Git Service
After=syslog.target
After=network.target

[Service]
Type=simple
User=git
Group=git
WorkingDirectory=/opt/gogs
ExecStart=/opt/gogs/gogs web
Restart=always
Environment=USER=git HOME=/home/git

[Install]
WantedBy=multi-user.target
Save and close the file, then enable and start the Gogs service:

 

sudo systemctl enable gogs
sudo systemctl start gogs

You can verify that Gogs is running with the following command:

sudo systemctl status gogs

Step 8: Using Gogs
With Gogs installed and running, you can now start managing your Git repositories. Visit http://:3000 to log in with the admin account you created during setup.

Creating a Repository
Once logged in, you can create a new repository by clicking the New Repository button. Fill in the repository name, description, and other details. You can choose to make the repository public or private.

Managing Users
Gogs allows you to manage multiple users easily. As an admin, you can invite others to create accounts or register them manually. You can also set user permissions, such as read-only access or admin rights.

Working with Git
To clone a repository hosted on Gogs, use the following command:

git clone http://:3000//.git

You can then push, pull, and manage your Git repositories as you would with any other Git hosting service.

Step 9: Enable HTTPS (Optional)
For security, it’s recommended to run Gogs over HTTPS. You can either set up a reverse proxy using Nginx or Apache or use Let’s Encrypt to get a free SSL certificate.

We appreciate your visit to our page! If you’re interested in exploring more articles about Linux systems and using Nagios, feel free to check out the links below.

How to Use Nagios to Monitor Linux Systems on Ubuntu

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button