(Last Updated On: October 14, 2018)

This guide is for installing Sourcegraph on Ubuntu 18.04 / Ubuntu 16.04 LTS. Sourcegraph is a fast, open-source, fully-featured code search and navigation engine.

Features of Sourcegraph

Sourcegraph has the following features:

Install Sourcegraph code search tool on Ubuntu 18.04 / Ubuntu 16.04 LTS

There are two ways to run Sourcegraph on Ubuntu 18.04 & Ubuntu 16.04 LTS.

  1. Running Sourcegraph in Docker
  2. Installing Sourcegraph on your local system

The method we’ll use in this guide is running Sourcegraph in Docker, which is the easiest and recommended.

Step 1: Install Docker Engine

You need Docker Engine installed and running on your system, use our Docker installation guide How to install Docker CE on Ubuntu / Debian / Fedora / Arch / CentOS

You can check the installed version of Docker using the command:

# docker version
Client:
 Version:           18.06.1-ce
 API version:       1.38
 Go version:        go1.10.3
 Git commit:        e68fc7a
 Built:             Tue Aug 21 17:24:51 2018
 OS/Arch:           linux/amd64
 Experimental:      false

Server:
 Engine:
  Version:          18.06.1-ce
  API version:      1.38 (minimum version 1.12)
  Go version:       go1.10.3
  Git commit:       e68fc7a
  Built:            Tue Aug 21 17:23:15 2018
  OS/Arch:          linux/amd64
  Experimental:     false

Step 2: Start Sourcegraph container

Once Docker is running, you can start Sourcegraph  container by executing commands below on your terminal  ( Not recommended method; use systemd)

docker run --publish 7080:7080 --rm \
--volume ~/.sourcegraph/config:/etc/sourcegraph \
--volume ~/.sourcegraph/data:/var/opt/sourcegraph \
--volume /var/run/docker.sock:/var/run/docker.sock \
sourcegraph/server

Manage Sourcegraph with Systemd ( recommended)

It is possible to manage sourcegraph docker container with systemd. Create a service unit file under /etc/systemd/system/sourcegraph.service

Add the following contents:

[Unit]
Description=Sourcegraph code search and navigation engine
Documentation=https://about.sourcegraph.com/docs
After=network-online.target
Wants=network-online.target

[Service]
Restart=on-failure
TimeoutStartSec=5
ExecStartPre=-/usr/bin/docker kill sourcegraph
ExecStartPre=-/usr/bin/docker rm sourcegraph

ExecStart=/usr/bin/docker run \
--name sourcegraph \
-p 7080:7080 \
-v /var/sourcegraph/config:/etc/sourcegraph \
-v /var/sourcegraph/data:/var/opt/sourcegraph \
-v /var/run/docker.sock:/var/run/docker.sock  \
sourcegraph/server

ExecStop=-/usr/bin/docker kill sourcegraph
ExecStop=-/usr/bin/docker rm sourcegraph

Docker options used:

  • --name sourcegraph: Name of the container
  •  -p 7080:7080 : Map port 7080 on the host to port 7080 on the container
  • -v /var/sourcegraph/config:/etc/sourcegraph: Volume mapping for storing Sourcegraph configurations files
  • -v /var/sourcegraph/data:/etc/sourcegraph: Volume mapping for storing Sourcegraph data – PostgreSQL, redis and repository data.

When Sourcegraph is ready, access the web interface by visiting.http://serverip:7080

On first access, you’ll be prompted to create an admin account.

Step 3: Add Repositories

As an example, we’ll add Github repository to Sourcegraph. For this, you need to create a new Access Token from https://github.com/settings/tokens/new .

Give the token a name and access privileges. The repo scope should be enough for Sourcegraph to access and clone the repositories on your Github account.

Make sure to copy your new personal access token after creation since you won’t be able to see it again!.

Once you have the Github token ID, Login to Sourcegraph Dashboard and navigate to Admin > Configuration > Add Github.com repositories. Add your token ID under the token quotes section and save the changes.

Like:

"token": "9bc5a980e443373b9ffd5e46e7dce3cbe863021b"

After a short while, your Github repositories should appear under the Repositories section but disabled.

Enable these repositories to start searching on them.

You can learn more on how to Add Repositories to Sourcegraph. Refer the Official Documentation for other Configurations and tweaks.