DEV Community

Cover image for Easy Self-Hosted Git Installation on Ubuntu Server
mortylen
mortylen

Posted on • Originally published at mortylen.hashnode.dev

Easy Self-Hosted Git Installation on Ubuntu Server

Welcome to a quick guide detailing the installation and setup of a self-hosted git server designed for beginners. We'll start by installing Ubuntu server, a popular choice known for its ease of use. Next, we'll set up PostgreSQL, a powerful and reliable open-source database management system, to ensure seamless data management for our git server. Finally, we'll integrate Gitea, a lightweight self-hosted git service, enabling you to effortlessly host your repositories and collaborate with your team members. For advanced configurations, detailed manuals for each service will be referenced later in the text. Let's proceed, step by step.

Ubuntu Server

To begin, we'll need an operating system. For this guide, Ubuntu Server is our choice. If you already have a Linux system installed, feel free to skip this step. Installing Ubuntu Server is straightforward—simply follow these steps:

  1. Download the Ubuntu Server ISO image from the official website: https://ubuntu.com/download/server

  2. Create a bootable drive and proceed with the installation, or create a new virtual machine from the ISO file.

  3. Install the server according to your preferences. Detailed installation instructions can be found on the official website: https://ubuntu.com/tutorials/install-ubuntu-server

For this tutorial I have chosen the following settings:

  • Language: English
  • Keyboard: English (US)
  • The base for the installation: Ubuntu Server
  • Network: Choose according to your own preferences
  • Storage and file system: Choose according to your own preferences
  • Profile setup: Choose your name and password
    • name: administrator
    • server name: git_server
    • password: choose your strong password.
  • OpenSSH: Install OpenSSH server

After installing the server, log in with your login to the system.

PostgreSQL

Before we proceed with installing Gitea, it's essential to set up the database. Gitea supports various databases, including PostgreSQL, MySQL, MariaDB, and MSSQL. In this guide, we'll focus on installing and configuring PostgreSQL on the local server.

  1. First, ensure your system is up to date:

    $ sudo apt update

  2. Install PostgreSQL and its additional components:

    $ sudo apt install postgresql postgresql-contrib

    or

    $ sudo apt install postgresql

  3. Enable PostgreSQL to start automatically on system boot:

    $ sudo systemctl enable postgresql.service

  4. Check the status of the PostgreSQL service:

    $ sudo systemctl status postgresql.service

PostgreSQL service status

  1. Switch to the postgres user:

    $ sudo su - postgres

  2. View the postgresql.conf file to check the 'password_encryption' attribute. You can use a text editor like nano. Find the path to the configuration file using the 'SHOW config_file' command.

    $ psql -c 'SHOW config_file'

    $ nano [FILE-NAME]

    Ensure 'password_encryption' is set to 'scram-sha-256':

    password_encryption = scram-sha-256

  3. Restart PostgreSQL to apply the changed settings:

    $ systemctl restart postgresql

  4. Access the PostgreSQL terminal:

    $ psql

  5. Create a database user. Replace 'mypassword' with a strong password. Don't forget the semicolon at the end:

    CREATE ROLE gitea WITH LOGIN PASSWORD 'mypassword';

Create role

  1. Create the 'giteadb' database. Later, you connect to the database using the name of that database and the name of the user created above:

    CREATE DATABASE giteadb WITH OWNER gitea TEMPLATE template0 ENCODING UTF8 LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8';

  2. Exit the PostgreSQL terminal:

    \q

  3. Stay logged in with 'postgres' user and add an authentication rule to the 'pg_hba.conf' file. You can use a text editor like nano. Find the path to the configuration file using the 'SHOW hba_file' command:

    $ psql -c 'SHOW hba_file'

    $ nano [FILE-NAME]

    Add the following line, where 'giteadb' is the database name and 'gitea' is the user name:

    local giteadb gitea scram-sha-256

  4. Restart PostgreSQL to apply the changed settings:

    $ systemctl restart postgresql

  5. Test the connection to the database, where 'gitea' is the user name and 'giteadb' is the database name:

    $ psql -U gitea -d giteadb

Test database connection

  1. Exit the PostgreSQL terminal and switch user with a user with sudo privileges:

    \q

    $ su - administrator

Gitea

For self-hosted git I chose Gitea. It's a powerful tool for smaller teams that provides many advanced features such as Git hosting, code review, team collaboration, CI/CD, and more. For a detailed feature comparison, visit https://docs.gitea.com/installation/comparsion. Additionally, you'll find a comprehensive installation guide there.

  1. Install Gitea using wget. Replace the URL with the desired version. We recommend opting for the latest version. Here's an example for 64-bit Linux, version 1.22. For the full list, visit https://dl.gitea.com/gitea/:

    $ wget -O giteahttps://dl.gitea.com/gitea/1.22/gitea-1.22-linux-amd64

    $ chmod +x gitea

  2. Verify if Git is installed:

    $ git --version

Gitea version

  1. Create a user for Gitea:

    $ sudo adduser --system --shell /bin/bash --gecos 'Git Version Control' --group --disabled-password --home /home/git git

Create user

  1. Set up the directory structure:

    $ mkdir -p /var/lib/gitea/{custom,data,log}

    $ chown -R git:git /var/lib/gitea/

    $ chmod -R 750 /var/lib/gitea/

    $ mkdir /etc/gitea

    $ chown root:git /etc/gitea

    $ chmod 770 /etc/gitea

  2. Copy Gitea to a global location:

    $ sudo cp gitea /usr/local/bin/gitea

  3. Create a system service. Download the file and save it to /etc/systemd/system/ or view the raw file in a browser and replace the URL with the version of Gitea you installed. You can find the list on https://github.com/go-gitea/gitea/blob/release/v1.22/contrib/systemd/gitea.service:

    $ sudo wget https://raw.githubusercontent.com/go-gitea/gitea/release/v1.22/contrib/systemd/gitea.service -P /etc/systemd/system/

  4. Enable automatic start of the service:

    $ sudo systemctl enable gitea --now

Enable gitea service

If you have a problem running the service, check the contents of the file: `/etc/systemd/system/gitea.service`
Enter fullscreen mode Exit fullscreen mode
  1. Verify the status of the Gitea service:

    $ systemctl status gitea

Logging in to the Gitea Web Interface

If the installation was successful and the gitea service is running, it's time to log in to Gitea via the web interface. Launch the web browser and enter the IP of your new git server and port 3000 in the address field. You should see the Initial Configuration page.

Example: 192.168.52.129:3000

Initial Configuration - Database Settings

In this section we will configure the connection between git and your PostgreSQL database.

Database settings

  • Database Type: Select PostgreSQL

  • Host: Since we have installed the database as local, we choose local IP and default port for PostgreSQL. 127.0.0.1:5432

  • Username: The PostgreSQL database user (role) we created. In this case gitea.

  • Password: Password for the PostgreSQL database user we created. In this case mypassword.

  • Database Name: The name of the database we created. In this case giteadb.

  • SSL: Securing the communication between the gitea and the PostgreSQL database. In this case Disable since the database server is installed locally.

  • Schema: Leave blank, the database is in the default Public scheme.

Initial Configuration - General Settings

Gitea configuration. We can use the default settings, or change them according to our needs. It is worth mentioning the path to the repositories, LFS and logs. Change to a more reasonable repository if necessary.

General settings

Initial Configuration - Optional Settings

In this section it is possible to set up a mailer for giteu, set up a server and third party service and set up an administrator account.

Optional settings

For this quick tutorial we can leave it in the default settings and click the Install Gitea button. All settings can be changed later in the Gitea configuration file /etc/gitea/app.ini

Install Gitea button

Sign In to Gitea

If you have created an administrator account in Initial Configuration - Optional Settings, you can now log in with it. If not, register a new account. The first user registered to Gitea becomes the admin.

Sign in to gitea

And that's it, your self-hosted git server is ready to use.

Let's Test Your Self-Hosted Git Using MS Visual Stodio.

We have no choice but to try git sesrver. First we create a new test repository in Gitea and then link it to the project in Visual Studio.

Create New Repository

After logging into Gitea, we create a new repository.

Create repository

Fill in all the settings according to your preferences and click the Create Repository button.

Visual Studio and Git

First, we'll copy the URL link from our new test repository in Gitea.

Repository url link

Now in the Visual Studio project, go to the Git tab and select Create Git repository.... On the left hand side under the Other category, select Existing remote. In the Push your code to an existing remote section, paste your copied URL link and click on Create and Push. Visual Studio prompts you for a username and password.

Visual Studio repository

In the View tab, select Git Changes, using this window you can work with the linked git. If Commit doesn't work, you probably don't have a user name set. Check this in Git -> Settings. In Visual Studio 2022 I noticed a small bug when VS had to be restarted, otherwise it didn't allow me to commit changes even if I had the user set up.

Now you should be able to see all your commits in your git repository in Gitte using the web browser.

Conclusion

In this simple tutorial, I tried to show you how to install git server using Gitea in a simple step-by-step way. It was installed on an Ubuntu server using PostgreSQL as the datastore. I didn't go into any detailed setup of Linux, the database server or Giteo itself, the goal was to show how easy it is to run a self-hosted git server. I have tested all the steps in the tutorial in a virtual environment, there may be slight changes depending on the versions of each component installed. I hope you have fun discovering the new features.

Cover Photo by Scott Rodgerson on Unsplash


My github profile GitHub
My blog page Hashnode

Top comments (0)