Deploying a Git Repository Server in Ubuntu
July 4, 2008
Git is a wonderful source code management tool. I’ve been using it now for some weeks of development and I felt ready to deploy my very own repository out on the intarwebs. While there is a lot of documentation available for using git there is also remarkably very little written about the minefield of caveats that is git and Ubuntu. I’ve been trying to create a repository on the server for almost a day until I caved in and resorted to asking people how they did it.
There is currently more than a year old issue on ubuntu, where installing git-daemon-run package clearly does not work. The dependency to runit, which is configured in a rather wrong way, breaks the installation. Unfortunately for us, that means we need to create our own init script that starts up the git-deamon as a service.
As I aim to rectify the situation, let me tell you of a way on how to correctly install a git repo server on a ubuntu machine step-by-step. We need to do the following:
- Install gitosis. An easy to use repo manager.
- Create an init-file for git-deaemon.
- Profit… or maybe not.
What are we waiting for? Let’s get crackin’!
Gitosis is a python program that can manage bare git repositories with easy configuration. It also provides some support for git-daemon and gitweb, making the necessary configuration on the repos. The program is fairly easy and straight-forward to install on ubuntu. In fact I did the steps described in this blog post. So I’ll just write a short hand version of that post here. First make sure you have python setup tools installed:
sudo apt-get install python-setuptools
Then grab gitosis by cloning it from original source and into a fresh new workspace. After that we’ll install it.
cd ~ && mkdir src && src git clone git://eagain.net/gitosis.git cd gitosis python setup.py install
Next make a git user that will manage the git repositories.
sudo adduser \ --system \ --shell /bin/sh \ --gecos 'git version control' \ --group \ --disabled-password \ --home /home/git \ git
Next copy your public key, i.e the rsa_id.pub file, to the server (e.g. with SCP or something) and add it to the list of authorized keys.
scp <location of id_rsa.pub-file>/tmp sudo -H -u git gitosis-init < /tmp/id_rsa.pub
Now lets check out the configuration (it is ingeniously stored as a repository of its own in gitosis) and edit it a bit.
git clone git@YOUR_SERVER_HOSTNAME:gitosis-admin.git cd gitosis-admin sudoedit gitosis.conf
You can add custom development groups that can read and write to repositories. For now, just specify a new project and let your user account be the member of that group.
[group test] members = firstname.lastname@example.org writeable = test_project daemon = yes
When the file is saved, and committed to the repository…
commit -a -m "Added group test and test_project in group test."
… then gitosis will create a bare repository automatically. The last parameter, daemon, specifies that git-daemon is free to show the repo in question. So lets try that out:
sudo -u git git-daemon --base-path=/home/git/repositories/ --export-all
You should now be able to access the repository in your local machine such as cloning the project. If you have a firewall, it might be a good idea to port forward the git port to the server. The default port in question is 9418.
Making git-daemon submit to our desires
All is fine and dandy now but the fancy part is making git-daemon run as a service in Ubuntu. As noted earlier if you tried installing the git-daemon-run package you will notice that it won’t work. Instead we’ll have to write our own damn init script. So lets start with creating the right file for the init script at /etc/init.d/git-daemon.
sudo edit /etc/init.d/git-daemon
And paste the following script at this pastie link (thanks to fujin on #git @ Freenode IRC for providing the pastie). But change the DAEMON_OPTS line so it includes the correct base-path and export-all attributes, like this:
DAEMON_OPTS="--base-path=/home/git/repositories --verbose --syslog --detach --pid-file=$PIDFILE --user=git --group=nogroup --export-all"
All that is left is to change modes and run the darn service.
sudo chmod +x /etc/init.d/git-daemon sudo invoke-rc.d git-daemon start
You should now be able to do your git stuff with your server at a git://yourserver/your_repo.git addy. I hope this has helped you out to run your very own git repository server.