I’m the first to admit that I absolutely love writing code and applications, I’m also a huge fan of GitHub, GitHub is a fantastic tool for hosting and collaborating on public projects such as open-source projects however and although you can pay to enable you to host private Git repositories on GitHub if you have more than a few you’ll soon start paying big bucks!
Recently I found a fantastic ‘clone’ of GitHub, it’s called GitLab and pretty much has all the features that GitHub does and like GitHub, GitLab is written in Ruby (on Rails).
In this blog post, I will demonstrate how to configure you’re own GitLab server specifically on Ubuntu Server 12.04 LTS – for those that follow my blog posts quite closely you’ll not doubt of realised by now that I do love Ubuntu Server (I prefer the debian based distros over the likes of the RedHat derivatives!)
So, this is a little longer than most of my other blog posts on here so I hope you can keep up as we delve into getting the raft of dependencies installed and everything up and running!
Lets get going…
From the console of your server we need to download and install some initial packages, execute the following commands:
sudo apt-get update sudo apt-get upgrade sudo apt-get install -y build-essential zlib1g-dev libyaml-dev libssl-dev libgdbm-dev libreadline-dev libncurses5-dev libffi-dev curl git-core openssh-server redis-server checkinstall libxml2-dev libxslt-dev libcurl4-openssl-dev libicu-dev
Next, by default Ubuntu 12.04 LTS comes bundled with Ruby 1.8 we need to replace this with Ruby 2.0, so lets remove the old version and then we’ll build the version 2.0 from source… (this bit can take some time, you might want to consider running the rest of the installation in a screen session incase you get an SSH idle connection timeout!)
mkdir /tmp/ruby && cd /tmp/ruby curl --progress ftp://ftp.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p247.tar.gz | tar xz cd ruby-2.0.0-p247 ./configure make sudo make install
Now, lets confirm that we are now running version 2.0 of Ruby:
We will now create a git user for GitLab to use, lets add the new system account like so:
sudo adduser --disabled-login --gecos 'GitLab' git
Next, as we will be installing our RoR dependencies using Bundler, we’ll download the Bundler Gem like so:
sudo gem install bundler --no-ri --no-rdoc
Ok, so that was the first bit out of the way, now lets move on to installing GitLab Shell, we’ll download GitLab Shell with the following commands:-
cd /home/git sudo -u git -H git clone https://github.com/gitlabhq/gitlab-shell.git cd gitlab-shell sudo -u git -H git checkout v1.8.0 sudo -u git -H cp config.yml.example config.yml
At the time of writing, the version of GitLab Shell was v1.8.0, you may want to replace the branch version above with the latest as per the branches found here: https://github.com/gitlabhq/gitlab-shell/releases
Now that we have GitLab Shell installed on the server, we should now edit the config.yml file and set the ‘gitlab_url’ property so that it points to our server, so using Nano we’ll edit and update the file:
Update this line to reflect your server’s FQDN:
Now we finalise the installation of GitLab shell but running the installer:
sudo -u git -H ./bin/install
Setting up MySQL
It is now time to prepare the MySQL database, you can also use Postgres if you wish but I will not be covering that in this post.
So, lets install the MySQL server and client libraries (when prompted for the MySQL root password be sure to remember it as you will need to use it shortly!):
sudo apt-get install -y mysql-server mysql-client libmysqlclient-dev
We will now create a dedicated MySQL user and create a database for GitLab, so lets now login to MySQL like so:
mysql -u root -p
and now execute the following queries, be sure to change the ‘YOUR_PASSWORD_HERE’ string for your own random password, this will be used by the dedicated GitLab DB account.
CREATE USER 'gitlab'@'localhost' IDENTIFIED BY 'YOUR_PASSWORD_HERE'; CREATE DATABASE IF NOT EXISTS `gitlabhq_production` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`; GRANT SELECT, LOCK TABLES, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON `gitlabhq_production`.* TO 'gitlab'@'localhost'; exit;
Fantastic! – Now lets just make sure that you can login as expected to MySQL using the new ‘gitlab’ account:
mysql -u gitlab -p
If all went well, you should be able to execute ‘show databases;‘ command at the MySQL prompt you should see the gitlabhq_production database in the list!
We’ve now got everything prepared so we can now start the actual installation of the GitLab application!
So lets now clone GitLab and switch to the latest ‘stable’ branch:
cd /home/git sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-ce.git gitlab cd /home/git/gitlab sudo -u git -H git checkout 6-4-stable sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml
Like with GitLab Shell, at the time of writing the latest stable release is 6.4, so hence why the above code checked out ‘6-4-stable’ you should change this to the latest if you prefer!
Again, we have some more editing to do in the GitLab configuration file, like before we need to set the server’s FQDN, lets open the file first:-
sudo -u git -H nano config/gitlab.yml
Now lets find the section below and update it accordingly, basically you should be replacing ‘localhost‘ with your server’s FQDN, for example ‘gitlab.mydomain.com‘, this should match the same FQDN that we set up earlier in the GitLab Shell config.yml file…
gitlab: ## Web server settings host: localhost port: 80 https: false
Ok, now we are going to set some directory permissions and configure the ‘git’ user’s Git configuration; simple stuff just copy and execute the following commands on your server…
cd /home/git/gitlab sudo chown -R git log/ sudo chown -R git tmp/ sudo chmod -R u+rwX log/ sudo chmod -R u+rwX tmp/ sudo -u git -H mkdir /home/git/gitlab-satellites sudo -u git -H mkdir tmp/pids/ sudo -u git -H mkdir tmp/sockets/ sudo chmod -R u+rwX tmp/pids/ sudo chmod -R u+rwX tmp/sockets/ sudo -u git -H mkdir public/uploads sudo chmod -R u+rwX public/uploads sudo -u git -H cp config/unicorn.rb.example config/unicorn.rb sudo -u git -H git config --global user.name "GitLab" sudo -u git -H git config --global user.email "gitlab@localhost" sudo -u git -H git config --global core.autocrlf input
sudo -u git -H cp config/initializers/rack_attack.rb.example config/initializers/rack_attack.rbsudo -u git cp config/database.yml.mysql config/database.yml
We now need to configure the database connection file with our MySQL user account details we created earlier…
sudo -u git -H nano config/database.yml
By default, the file looks like as follows, update the ‘production‘ section to match your database username and password that we configured earlier, you should only need to change the username and password…
production: adapter: mysql2 encoding: utf8 reconnect: false database: gitlabhq_production pool: 10 username: root password: "secure password"
Now that we’ve set that up, lets secure the file but setting some more restrictive file permissions:
sudo -u git -H chmod o-rwx config/database.yml
Ok, we are now ready to start the GitLab application install (don’t panic when you see ‘postgres’ in the command below, notice the operator actually says ‘–without’ so this is 100% correct! :D)…
cd /home/git/gitlab sudo -u git -H bundle install --deployment --without development test postgres aws
After that is complete, lets run this command, and type ‘yes’ when prompted:
sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=production
After the above command completes, you will be shown the default administrator credentials, it should look like this:
Administrator account created: login.........firstname.lastname@example.org password......5iveL!fe
Lets now configure GitLab to start automatically whenever the server is booted, to do this execute the following commands:
sudo cp lib/support/init.d/gitlab /etc/init.d/gitlab sudo chmod +x /etc/init.d/gitlab sudo update-rc.d gitlab defaults 21
Lets keep the log files in check, lets enable logrotate:-
sudo cp lib/support/logrotate/gitlab /etc/logrotate.d/gitlab
Lets now check that everything installed ok on the server and we are not getting any error messages that we should be concerned about:-
sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production
We can now start GitLab and hopefully all will work without any issues:
sudo service gitlab start
Lets compile the assets:
sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production
….this should speed up the initial loading time!
Time to install Nginx
Ok, GitLab is now running but we now need to install a web server, my personal favorite for speed and stability is Nginx, so that is what I will be installing…
sudo apt-get -y install nginx cd /home/git/gitlab sudo cp lib/support/nginx/gitlab /etc/nginx/sites-available/gitlab sudo ln -s /etc/nginx/sites-available/gitlab /etc/nginx/sites-enabled/gitlab
We now need to edit the Nginx gitlab configuration file to set the server’s FQDN and again, this should match the ones we added to both ‘GitLab Shell’ and GitLab’s config.yml files earlier, so:
sudo nano /etc/nginx/sites-available/gitlab
and now restart Nginx like so:
sudo service nginx restart
I’m sure you’ll be over the moon to hear that we are finished!!!
Having issues? 502 error(s)?
First of all, its a good idea to run this command to ensure that everything that should be ok, is ok…
sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production
I actually found that on my installation, it was complaining that the version of git was out of date, this didn’t really matter too much so I just left it!
I found that the first time I tried accessing GitLab after installation, the CPU and memory was battered by ‘unicorn_rails’ process, to get around this I updated the ‘timeout’ value in the /home/git/gitlab/config/unicorn.rb file to ‘300’ and then restarted both Nginx and GitLab, once complete I tried accessing GitLab once again, it took a while but after it was complete it loaded and all was good! – See more infomation about this issue here: https://groups.google.com/forum/#!topic/gitlabhq/u9AMESyd-N0
I found that after installation whenver I tried to connect to my server, I was being thrown a 502 error by Nginx, I did some digging around on Google and found that to resolve the issue I had to add my servers FQDN to the /etc/hosts file on the 127.0.0.1 line!
So if you have the same issue, change the two lines in /etc/hosts from:
127.0.0.1 localhost ap2
127.0.0.1 localhost ap2 gitlab.mydomain.com
and restart nginx and GitLab and you should then be good to go!
If you are having other issues, it is well worth knowing where the log files are so that you can view them and get clues as to what may be causing the issue, I’d recommend checking the following log files for errors:-
tail /var/log/nginx/gitlab_error.log -n50 tail /var/log/syslog -n50 tail /home/git/gitlab/log/application.log -n50 tail /home/git/gitlab/log/production.log -n50 tail /home/git/gitlab/log/unicorn.stderr.log -n50 tail /home/git/gitlab/log/unicorn.stdout.log -n50