Gerrit Code Review Installation

// June 12th, 2009 // Development, Guides

I ran into a lot of trouble when I was installing Gerrit on my home server and decided I needed to publish a supplement to the Gerrit2 – Installation Guide to help others so they don’t spend a day beating their head into a wall like I did.  My home server is running Ubuntu 9.04, so you may need to adjust a few of the commands if your OS of choice is different.  The Gerrit installation is one of the worst procedures I’ve seen, but it is worth it once installed.  Let’s get started!

Step 1: You need to install an SQL database on your system if you haven’t already.  I chose to use MySQL because that is what I’m more familiar with.  The rest of this guide will assume you also chose MySQL.  You can install MySQL using apt-get in Ubuntu.

Step 2: You need to create a new user on your system for Gerrit.  This user will be used to run Gerrit.  You can create the user with this command:

sudo adduser --system --shell /bin/sh --gecos 'Gerrit Code Review' --group --disabled-password --home /home/gerrit2 gerrit2

Step 3: Download the latest version of Gerrit from the Gerrit Downloads page.  Keep this file somewhere in your home directory for now.  It will be moved in a later step.

Step 4: Create a Gerrit specific user within the database and assign it a password, create a database, and give the user full rights.  I “cheated” and used phpMyAdmin to do this.  When creating the user, phpMyAdmin gives you the option to create a database with the same name as the new user and grant all privileges to that user.  That’s exactly what we needed!  (phpMyAdmin is also available in Ubuntu’s package manager.)

Step 5: Next we need to initialize the Gerrit2 tables.  To do that, first run the following two commands from the directory containing the war file downloaded in step 3:

java -jar gerrit-*.war --cat extra/GerritServer.properties_example >GerritServer.properties

curl -O http://repo1.maven.org/maven2/mysql/mysql-connector-java/5.0.8/mysql-connector-java-5.0.8.jar

Step 6: Open the GerritServer.properties file in your favorite text editor.  Scroll down to the MySQL 5.0 section at the bottom of the file.  Uncomment the three database lines.  Modify the database.url line to match the database, user, and password you created in step 4.  For example, my line looks like this:

database.url = jdbc:mysql://localhost/gerrit2?user=gerrit2&password=secretcode

Step 7: We’re now ready to initialize the Gerrit2 tables.  Run the following command and cross your fingers:

java -jar gerrit-*.war CreateSchema

I got two info prints when I issued that command, which can be ignored.

Step 8: A script should be run to create the query indexes, so Gerrit can avoid table scans when looking up information. Run the index script through the MySQL query tool:

java -jar gerrit-*.war --cat sql/index_generic.sql | mysql -u gerrit2 gerrit2 -p
java -jar gerrit-*.war --cat sql/mysql_nextval.sql | mysql -u gerrit2 gerrit2 -p

Step 9: Now the site_path must be configured:

sudo -u gerrit2 mkdir /home/gerrit2/cfg
mysql -u gerrit2 -p
use gerrit2;
UPDATE system_config SET site_path='/home/gerrit2/cfg';
exit

Step 10: Create the Git repository base, which will hold the Git repositories that Gerrit knows about and can service:

sudo -u gerrit2 mkdir /home/gerrit2/repositories

sudo -u gerrit2 git config --file /home/gerrit2/cfg/gerrit.config gerrit.basePath /home/gerrit2/repositories

Step 11: Download the latest version of Jetty (version 6.1.18 at the time of this writing) from the Jetty Downloads page and unzip to /home/gerrit2/jetty.

Step 12: The required JDBC drivers need to be installed.  Download the c3p0 JDBC driver and unzip to a temporary directory.  Take the c3p0-0.9.1.2.jar from the lib folder and the mysql-connector-java-5.0.8.jar from step 5 and move them into /home/gerrit2/jetty/lib/plus.

Step 13: Move Gerrit into the Jetty deployment:

java -jar gerrit-*.war --cat extra/jetty_gerrit.xml >gerrit.xml
sudo mv gerrit-*.war /home/gerrit2/jetty/webapps/gerrit.war
sudo chown gerrit2:gerrit2 /home/gerrit2/jetty/webapps/gerrit.war
sudo rm -f /home/gerrit2/jetty/contexts/test.xml

Step 14: Open the gerrit.xml file that was created in the previous step in your favorite text editor.  Uncomment out the MySQL section.  Modify the jdbcUrl value to have the correct database, username and password.  Also, at the time of this writing, there is a syntax error on that line.  The ampersand should be “&” (without the quotes).  Without this fix, Gerrit will not start.  My edited jdbcUrl value looks like this:

jdbc:mysql://localhost/gerrit2?user=gerrit2&password=secretkey

Save and close the file.  Move to /home/gerrit2/jetty/contexts/gerrit.xml

Step 15: Finally, create the startup script for Gerrit.  Create /etc/init.d/gerrit2-jetty with the following contents:

#!/bin/sh

export JETTY_HOST=127.0.0.1
export JETTY_PORT=8081
export JETTY_USER=gerrit2
export JETTY_PID=/var/run/jetty$JETTY_PORT.pid
export JETTY_HOME=/home/$JETTY_USER/jetty
export JAVA_HOME=/usr/lib/jvm/java-6-sun-1.6.0.13/jre

JAVA_OPTIONS=""
JAVA_OPTIONS="$JAVA_OPTIONS -Djetty.host=$JETTY_HOST"
export JAVA_OPTIONS

C="jetty-logging jetty"
[ -f "$JETTY_HOME/etc/jetty_sslproxy.xml" ] && C="$C jetty_sslproxy"

exec $JETTY_HOME/bin/jetty.sh "$@" $C

You’ll need to update JETTY_HOST with the correct hostname or IP address.  You’ll also need to verify the JAVA_HOME path is correct for your system.  Save and close the file, then make the file executable.

Step 16: Start Gerrit!

sudo /etc/init.d/gerrit2-jetty start

Now, wait a minute.  It take a little bit to bring Gerrit up.  If having problems, you can check the stderrout.log in /home/gerrit2/jetty/logs.

Step 17: Congratulations, Gerrit is now running.  You now need to setup accounts and projects.  However, the rest of the Gerrit installation instructions are much more clear, so my tutorial will end here.  You can continue with Administrator Setup in the Gerrit2 – Installation Guide.

Note: I did skip the Apache2 Reverse Proxy step because I didn’t need it.  If this is something you need, read the Apache2 Reverse Proxy instructions.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • MySpace
  • StumbleUpon
  • Technorati
  • Twitter

Comments are closed.