This guide is for installing Sourcegraph on Ubuntu Linux system. Sourcegraph is a fast, open-source, fully-featured code search and navigation engine. Sourcegraph is used in indexing millions of open source repositories across several code hosts. Being open source solution means you can install it now and start using it.

Features of Sourcegraph

Sourcegraph has the following features:

Install Sourcegraph code search tool on Ubuntu

There are two ways to run Sourcegraph on Ubuntu Linux system

  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 Install Docker CE on Ubuntu / Debian / Fedora / Arch / CentOS

After installation of Docker CE, add user to docker group:

sudo usermod -aG docker $USER
newgrp docker

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

$ docker version
Client: Docker Engine - Community
 Version:           27.2.0
 API version:       1.47
 Go version:        go1.21.13
 Git commit:        3ab4256
 Built:             Tue Aug 27 14:15:15 2024
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          27.2.0
  API version:      1.47 (minimum version 1.24)
  Go version:       go1.21.13
  Git commit:       3ab5c7d
  Built:            Tue Aug 27 14:15:15 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.7.21
  GitCommit:        472731909fa34bd7bc9c087e4c27943f9835f111
 runc:
  Version:          1.1.13
  GitCommit:        v1.1.13-0-g58aa920
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Step 2: Start Sourcegraph container

Once Docker is running, you can start Sourcegraph  container by executing commands below on your terminal.

Use the commands below to run an instance of Sourcegraph locally using Docker. The Sourcegraph image tags can be checked from Docker hub.

docker run -d --name sourcegraph \
  --publish 7080:7080 \
  --publish 127.0.0.1:3370:3370 \
  --rm \
  --volume ~/.sourcegraph/config:/etc/sourcegraph \
  --volume ~/.sourcegraph/data:/var/opt/sourcegraph \
  sourcegraph/server:5.6.3798

List running containers:

$ docker ps
CONTAINER ID   IMAGE                         COMMAND                  CREATED          STATUS          PORTS                                                                 NAMES
7aa213f16152   sourcegraph/server:5.6.3798   "/sbin/tini -- /serv…"   55 seconds ago   Up 53 seconds   127.0.0.1:3370->3370/tcp, 0.0.0.0:7080->7080/tcp, :::7080->7080/tcp   sourcegraph

It is possible to manage sourcegraph docker container with systemd. First delete the container running:

$ docker rm -f sourcegraph
sourcegraph

$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

Create a service unit file under:

sudo vim /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 \
-p 127.0.0.1:3370:3370 \
-v /var/sourcegraph/config:/etc/sourcegraph \
-v /var/sourcegraph/data:/var/opt/sourcegraph \
-v /var/run/docker.sock:/var/run/docker.sock  \
sourcegraph/server:5.6.3798

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 – PostgreSQLredis and repository data.

Start and run service:

sudo systemctl daemon-reload
sudo systemctl start sourcegraph.service

Confirm status:

$ systemctl status sourcegraph.service
 sourcegraph.service - Sourcegraph code search and navigation engine
     Loaded: loaded (/etc/systemd/system/sourcegraph.service; static; vendor preset: enabled)
     Active: active (running) since Fri 2022-02-04 09:57:59 UTC; 8s ago
       Docs: https://about.sourcegraph.com/docs
    Process: 86961 ExecStartPre=/usr/bin/docker kill sourcegraph (code=exited, status=1/FAILURE)
    Process: 86982 ExecStartPre=/usr/bin/docker rm sourcegraph (code=exited, status=1/FAILURE)
   Main PID: 86989 (docker)
      Tasks: 7 (limit: 4677)
     Memory: 16.5M
     CGroup: /system.slice/sourcegraph.service
             └─86989 /usr/bin/docker run --name sourcegraph -p 7080:7080 -p 127.0.0.1:3370:3370 -v /var/sourcegraph/config:/etc/sourcegraph -v /var/sourcegraph/data:/var/opt/sourcegraph -v /var/r>

Feb 04 09:58:03 ubuntu-20-04-01 docker[86989]: 09:58:03                  frontend |            ┌╦╪╪╪╪╪╪╪╪╪╪╪╝╙╬╬╬╬╬╬╬╬╬╬       └╙╩╬╪╪╝╜
Feb 04 09:58:03 ubuntu-20-04-01 docker[86989]: 09:58:03                  frontend |           ╦╪╪╪╪╪╪╪╪╪╪╪╪┘  ╠╬╬╬╬╬╬╬╬╬╬
Feb 04 09:58:03 ubuntu-20-04-01 docker[86989]: 09:58:03                  frontend |          ╬╪╪╪╪╪╪╪╪╪╪╪┘     ╬╬╬╬╬╬╬╬╬╬┐
Feb 04 09:58:03 ubuntu-20-04-01 docker[86989]: 09:58:03                  frontend |          ╙╪╪╪╪╪╪╪╪╪╜       ╘╬╬╬╬╬╬╬╬╬╬
Feb 04 09:58:03 ubuntu-20-04-01 docker[86989]: 09:58:03                  frontend |           └╩╪╪╪╪╝╙          ╬╬╬╬╬╬╬╬╬╬╕
Feb 04 09:58:03 ubuntu-20-04-01 docker[86989]: 09:58:03                  frontend |                             └╬╬╬╬╬╬╬╬╬╛
Feb 04 09:58:03 ubuntu-20-04-01 docker[86989]: 09:58:03                  frontend |                               ╩╬╬╬╬╬╬┘
Feb 04 09:58:03 ubuntu-20-04-01 docker[86989]: 09:58:03                  frontend |
Feb 04 09:58:03 ubuntu-20-04-01 docker[86989]: 09:58:03                  frontend | ✱ Sourcegraph is ready at: http://127.0.0.1:7080
Feb 04 09:58:07 ubuntu-20-04-01 docker[86989]: 09:58:07                  frontend | t=2022-02-04T09:58:07+0000 lvl=warn msg="slow http request" method=POST url=/.internal/repos/index code=200 dur>

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.

sourcegraph create admin account min

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 .

github create new token min

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.

sourcegraph add github access token min

Like:

"token": "9bc5a980e443373b9ffd5e46e7dce3cbe863021b"

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

sourcegraph repositories min

Enable these repositories to start searching on them.

sourcegraph enable repositories min

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

LEAVE A REPLY

Please enter your comment!
Please enter your name here