Install Sourcegraph code search engine on Ubuntu 22.04|20.04|18.04|16.04

ComputingPost
5 min readOct 20, 2022

--

This guide is for installing Sourcegraph on Ubuntu 22.04|20.04|18.04|16.04. 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 22.04|20.04|18.04|16.04

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: 20.10.12

API version: 1.41

Go version: go1.16.12

Git commit: e91ed57

Built: Mon Dec 13 11:45:33 2021

OS/Arch: linux/amd64

Context: default

Experimental: true



Server: Docker Engine - Community

Engine:

Version: 20.10.12

API version: 1.41 (minimum version 1.12)

Go version: go1.16.12

Git commit: 459d0df

Built: Mon Dec 13 11:43:42 2021

OS/Arch: linux/amd64

Experimental: false

containerd:

Version: 1.4.12

GitCommit: 7b11cfaabd73bb80907dd23182b9347b4245eb5d

runc:

Version: 1.0.2

GitCommit: v1.0.2-0-g52b36a2

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:3.36.3

List running containers:

$ docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

00a7f61363df sourcegraph/server:3.36.3 "/sbin/tini -- /usr/…" 2 seconds ago Up 2 seconds 127.0.0.1:3370->3370/tcp, 0.0.0.0:7080->7080/tcp, :::7080->7080/tcp sourcegraph

Manage Sourcegraph with Systemd ( recommended)

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:3.36.3



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.

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-1024x655

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-1024x396

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-1024x168

Like:

"token": "9bc5a980e443373b9ffd5e46e7dce3cbe863021b"

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

sourcegraph-repositories-min-1024x153

Enable these repositories to start searching on them.

sourcegraph-enable-repositories-min-1024x250

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

https://www.computingpost.com/install-sourcegraph-code-search-engine-on-ubuntu-22-0420-0418-0416-04/?feed_id=15336&_unique_id=6351349841567

--

--

ComputingPost

ComputingPost — Linux Howtos, Tutorials, Guides, News, Tips and Tricks.