Sourcegraph is an open source code search and navigation tool that enables developers to do fast global searches, use intelligent code recognition, enhance GitHub code hosting, serve as an extension API for easier third-party integration, and much more. Once you have it up and running, it can empower code searching in ways you might never before experienced.
But how do you deploy Sourcegraph? There are a few ways to make this happen:
- Try a public instance on any open source repository.
- Install the Chrome browser extension.
- Deploy as a Docker container.
I want to walk you through the deployment of Sourcegraph via a Docker container and then how to add a repository to be used for code searching.
SEE: Vendor comparison: Microsoft Azure, Amazon AWS, and Google Cloud (Tech Pro Research)
What you need
I’ll demonstrate on Ubuntu Server 18.04. The hosting platform doesn’t matter, so long as it can run Docker. You will also need to connect to a repository. I’ll demonstrate with a GitHub repository, and to do that you need an access token and the scope for a repository (both of which are found within your GitHub account). Finally, you need a user that is capable of running the docker command without using sudo (for security purposes).
Deploying the container
First, lets deploy the container. This can be done with a single command. We’ll deploy the container on internal and external port 7080 and mounting data to the directory /.sourcegraph/config:/etc/sourcegraph. To do this, open a terminal window and issue the command:
docker run --publish 7080:7080 -d --rm --volume ~/.sourcegraph/config:/etc/sourcegraph --volume ~/.sourcegraph/data:/var/opt/sourcegraph --volume /var/run/docker.sock:/var/run/docker.sock sourcegraph/server:2.13.5
Once that command completes, a welcome screen will appear informing you how to reach the Sourcegraph web gui (Figure A).
Unless the hosting server has a GUI, you won’t be reaching Sourcegraph via the 127.0.0.1 localhost address. Instead, point your web browser to http://SERVER_IP:7080 (Where SERVER_IP is the IP address of the hosting server). That will bring up the admin user creation page (Figure B).
After creating a user, you’ll be logged in and can start configuring Sourcegraph.
Adding a repository
Now it’s time to add a repository. From the Sourcegraph main page (Figure C), click on the Configuration button.
In the Configuration window (Figure D), click the Add GitHub repositories button, which will produce the necessary configuration for that repository.
In the “token” line, delete the content between the quotes (the highlighted section) and add the Access Token you retrieved from your GitHub account. Once you’ve pasted that token, click Save changes.
With the GitHub information added, click on the Repositories button, and you should see all of the repositories associated with the access token (Figure E).
Click to Enable any/all repositories you want to add to Sourcegraph. Once they are enabled, Sourcegraph will clone them so they are ready to be searched. You can then click on one of the repositories and start searching through your code. If you’re dealing with thousands to millions of lines of code, Sourcegraph will quickly become your best friend.