Getting started with developing Sourcegraph

Have a look around, our code is on GitHub.



Sourcegraph server is a collection of smaller binaries. The development server, dev/, initializes the environment and starts a process manager that runs all of the binaries. See the Architecture doc for a full description of what each of these services does. The sections below describe the dependencies you need to run dev/

Step 1: Install dependencies

Sourcegraph has the following dependencies:

The following are two recommendations for installing these dependencies:


  1. Install Homebrew.

  2. Install Docker for Mac.

    optionally via brew

    brew cask install docker
    1. Install Go, Node Version Manager, PostgreSQL, Redis, Git, NGINX, golang-migrate, Comby, and SQLite tools with the following command: bash brew install go nvm yarn redis postgresql git gnu-sed nginx golang-migrate comby sqlite pcre FiloSottile/musl-cross/musl-cross
  3. Install the current recommended version of Node JS using:

    nvm install
    1. Configure PostgreSQL and Redis to start automatically bash brew services start postgresql brew services start redis (You can stop them later by calling stop instead of start above.)
  4. Ensure psql, the PostgreSQL command line client, is on your $PATH. Homebrew does not put it there by default. Homebrew gives you the command to run to insert psql in your path in the “Caveats” section of brew info postgresql. Alternatively, you can use the command below. It might need to be adjusted depending on your Homebrew prefix (/usr/local below) and shell (bash below).

    hash psql || { echo 'export PATH="/usr/local/opt/postgresql/bin:$PATH"' >> ~/.bash_profile }
    source ~/.bash_profile
    1. Open a new Terminal window to ensure psql is now on your $PATH.


    2. Add package repositories:

      # Go
      sudo add-apt-repository ppa:longsleep/golang-backports
      # Docker
      curl -fsSL | sudo apt-key add -
      sudo add-apt-repository "deb [arch=amd64] $(lsb_release -cs) stable"
      # Yarn
      curl -sS | sudo apt-key add -
      echo "deb stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
  5. Update repositories:

    sudo apt-get update
    1. Install dependencies:

      sudo apt install -y make git-all postgresql postgresql-contrib redis-server nginx libpcre3-dev libsqlite3-dev pkg-config golang-go musl-tools docker-ce docker-ce-cli yarn
      # install golang-migrate (you must move the extracted binary into your $PATH)
      curl -L | tar xvz
      # install comby (you must move the extracted binary into your $PATH)
      curl -L | tar xvz
      # nvm (to manage Node.js)
      curl -o- | bash
      # in repo dir: install current recommendend version of Node JS
      nvm install
  6. Configure startup services

    sudo systemctl enable postgresql
    sudo systemctl enable redis-server.service
    1. (optional) You can also run Redis using Docker In this case you should not enable the redis-server.service from the previous step.

      dockerd # if docker isn't already running
      docker run -p 6379:6379 -v $REDIS_DATA_DIR redis
      # $REDIS_DATA_DIR should be an absolute path to a folder where you intend to store Redis data

      You need to have Redis running when you start the dev server later on. If you have issues running Docker, try adding your user to the docker group, and/or updating the socket file persimissions, or try running these commands under sudo.

Step 2: Initialize your database

You need a fresh Postgres database and a database user that has full ownership of that database.

  1. Create a database for the current Unix user

    # For Linux users, first access the postgres user shell
    sudo su - postgres
  2. Create the Sourcegraph user and password

    createuser --superuser sourcegraph
    psql -c "ALTER USER sourcegraph WITH PASSWORD 'sourcegraph';"
    1. Create the Sourcegraph database bash createdb --owner=sourcegraph --encoding=UTF8 --template=template0 sourcegraph
  3. Configure database settings in your environment

    The Sourcegraph server reads PostgreSQL connection configuration from the PG* environment variables.

    Add these, for example, in your ~/.bashrc:

    export PGPORT=5432
    export PGHOST=localhost
    export PGUSER=sourcegraph
    export PGPASSWORD=sourcegraph
    export PGDATABASE=sourcegraph
    export PGSSLMODE=disable

    You can also use a tool like envdir or a .dotenv file to source these env vars on demand when you start the server.

    More info

    For more information about data storage, read our full PostgreSQL Guide page. Migrations are applied automatically.

    Step 3: (macOS) Start Docker

    Option A: Docker for Mac

    This is the easy way - just launch and wait for it to finish loading.

    Option B: docker-machine

    The Docker daemon should be running in the background, which you can test by running docker ps. If you’re on OS X and using docker-machine instead of Docker for Mac, you may have to run:

    docker-machine start default
    eval $(docker-machine env)

Step 4: Get the code

git clone

Step 5: Start the Server

cd sourcegraph

This will continuously compile your code and live reload your locally running instance of Sourcegraph.

Navigate your browser to http://localhost:3080 to see if everything worked.


Problems with node_modules or Javascript packages

Noticing problems with node_modules/ or package versions? Try running this command to clear the local package cache.

yarn cache clean
rm -rf node_modules web/node_modules
cd web

dial tcp connect: connection refused

This means the frontend server failed to start, for some reason. Look through the previous logs for possible explanations, such as failure to contact the redis server, or database migrations failing.

Database migration failures

While developing Sourcegraph, you may run into:

frontend | failed to migrate the DB. Please contact [email protected] for further assistance:Dirty database version 1514702776. Fix and force version.

You may have to run migrations manually. First, install the Go migrate CLI, then run dev/ up

If you get something like error: Dirty database version 1514702776. Fix and force version., you need to roll things back and start from scratch.

dev/ drop
dev/ up

If you receive errors while migrating, try dropping the database

dev/ up

Internal Server Error

If you see this error when opening the app:

500 Internal Server Error template: app.html:21:70: executing "app.html" at <version "styles/styl...>: error calling version: open ui/assets/styles/app.bundle.css: no such file or directory

that means Webpack hasn’t finished compiling the styles yet (it takes about 3 minutes). Simply wait a little while for a message from webpack like web | Time: 180000ms to appear in the terminal.

Increase maximum available file descriptors.

./dev/ may ask you to run ulimit to increase the maximum number of available file descriptors for a process. You can make this setting permanent for every shell session by adding the following line to your .*rc file (usually .bashrc or .zshrc):

# increase max number of file descriptors for running a sourcegraph instance.
ulimit -n 10000

On Linux, it may also be necessary to increase sysctl -n fs.inotify.max_user_watches, which can be done by running one of the following:

echo 524288 | sudo tee -a /proc/sys/fs/inotify/max_user_watches

# If the above doesn't work, you can also try this:
sudo sysctl fs.inotify.max_user_watches=524288

If you ever need to wipe your local database and Redis, run the following command.


How to Run Tests

See for details.

CPU/RAM/bandwidth/battery usage

On first install, the program will use quite a bit of bandwidth to concurrently download all of the Go and Node packages. After packages have been installed, the Javascript assets will be compiled into a single Javascript file, which can take up to 5 minutes, and can be heavy on the CPU at times.

After the initial install/compile is complete, the Docker for Mac binary uses about 1.5GB of RAM. The numerous different Go binaries don’t use that much RAM or CPU each, about 5MB of RAM each.

If you notice heavy battery and CPU usage running gulp --color watch, please first double check that Spotlight is not indexing your Sourcegraph repository, as this can lead to additional, unnecessary, poll events.

If you’re running macOS 10.15.x (Catalina) reinstalling the Xcode Command Line Tools may be necessary as follows:

  1. Uninstall the Command Line Tools with rm -rf /Library/Developer/CommandLineTools
  2. Reinstall it with xcode-select --install
  3. Go to sourcegraph/sourcegraph’s root directory and run rm -rf node_modules
  4. Re-run the launch script (./dev/

How to debug live code

How to debug a program with Visual Studio Code:

Debug TypeScript code

Requires “Debugger for Chrome” extension.

  • Quit Chrome
  • Launch Chrome (Canary) from the command line with a remote debugging port:
    • Mac OS: /Applications/Google\ Chrome\\ Chrome\ Canary --remote-debugging-port=9222
    • Windows: start chrome.exe –remote-debugging-port=9222
    • Linux: chromium-browser --remote-debugging-port=9222
  • Go to http://localhost:3080
  • Open the Debugger in VSCode: “View” > “Debug”
  • Launch the (ui) http://localhost:3080/* debug configuration
  • Set breakpoints, enjoy

Debug Go code

Install Delve:

xcode-select --install
go get -u

Then install pgrep:

brew install proctools

Make sure to run env DELVE=true dev/ to disable optimizations during compilation, otherwise Delve will have difficulty stepping through optimized functions (line numbers will be off, you won’t be able to print local variables, etc.).

Now you can attach a debugger to any Go process (e.g. frontend, searcher, go-langserver) in 1 command:

dlv attach $(pgrep frontend)

Delve will pause the process once it attaches the debugger. Most used commands:

  • b cmd/frontend/db/access_tokens.go:52 to set a breakpoint on a line (bp lists all, clearall deletes all)
  • c to continue execution of the program
  • Ctrl-C pause the program to bring back the command prompt
  • n to step over the next statement
  • s to step into the next function call
  • stepout to step out of the current function call
  • Ctrl-D to exit

Go dependency management

We use Go modules to manage Go dependencies in this repository.


The Sourcegraph repository relies on code generation triggered by go generate. Code generation is used for a variety of tasks:

  • generating code for mocking interfaces
  • generate wrappers for interfaces (e.g., ./server/internal/middleware/* packages)
  • pack app templates and assets into binaries

To generate everything, just run:


Note: Sometimes, there are erroneous diffs. This occurs for a few reasons, none of which are legitimate (i.e., they are tech debt items we need to address):

  • The codegen tools might emit code that depends on system configuration, such as the system timezone or packages you have in your GOPATH. We need to submit PRs to the tools to eliminate these issues.
  • You might have existing but gitignored files that the codegen tools read on your disk that other developers don’t have. (This occurs for app assets especially.)

If you think a diff is erroneous, don’t commit it. Add a tech debt item to the issue tracker and assign the person who you think is responsible (or ask).

Windows support

Running Sourcegraph on Windows is not actively tested, but should be possible within the Windows Subsystem for Linux (WSL). Sourcegraph currently relies on Unix specifics in several places, which makes it currently not possible to run Sourcegraph directly inside Windows without WSL. We are happy to accept contributions here! :)

Other nice things

Offline development

Sometimes you will want to develop Sourcegraph but it just so happens you will be on a plane or a train or perhaps a beach, and you will have no WiFi. And you may raise your fist toward heaven and say something like, “Why, we can put a man on the moon, so why can’t we develop high-quality code search without an Internet connection?” But lower your hand back to your keyboard and fret no further, for the year is 2019, and you can develop Sourcegraph with no connectivity by setting the OFFLINE environment variable:

OFFLINE=true dev/