2020-01-24 23:59:16 +01:00
---
title: Build Harbor from Source Code
---
2019-10-17 15:47:25 +02:00
This guide provides instructions for developers to build and run Harbor from source code.
## Step 1: Prepare for a build environment for Harbor
Harbor is deployed as several Docker containers and most of the code is written in Go language. The build environment requires Docker, Docker Compose and golang development environment. Please install the below prerequisites:
| Software | Required Version |
| -------------- | ---------------- |
| docker | 17.05 + |
| docker-compose | 1.18.0 + |
| python | 2.7 + |
| git | 1.9.1 + |
| make | 3.81 + |
| golang\* | 1.7.3 + |
\*optional, required only if you use your own Golang environment.
## Step 2: Getting the source code
```sh
2020-01-27 19:15:44 +01:00
git clone https://github.com/goharbor/harbor
2019-10-17 15:47:25 +02:00
```
## Step 3: Building and installing Harbor
### Configuration
2020-01-24 23:59:16 +01:00
Edit the file **make/harbor.yml** and make necessary configuration changes such as hostname, admin password and mail server. Refer to the [Installation and Configuration Guide ](../installation-guide.md#configuring-harbor ) for more info.
2019-10-17 15:47:25 +02:00
```sh
2020-01-24 23:59:16 +01:00
cd harbor
vi make/harbor.yml
2019-10-17 15:47:25 +02:00
```
### Compiling and Running
You can compile the code by one of the three approaches:
#### I. Build with official Golang image
- Get official Golang image from docker hub:
2020-01-27 19:15:44 +01:00
```sh
docker pull golang:1.12.5
```
2019-10-17 15:47:25 +02:00
- Build, install and bring up Harbor without Notary:
2020-01-27 19:15:44 +01:00
```sh
make install GOBUILDIMAGE=golang:1.12.5 COMPILETAG=compile_golangimage
```
2019-10-17 15:47:25 +02:00
- Build, install and bring up Harbor with Notary:
2020-01-27 19:15:44 +01:00
```sh
make install GOBUILDIMAGE=golang:1.12.5 COMPILETAG=compile_golangimage NOTARYFLAG=true
```
2019-10-17 15:47:25 +02:00
- Build, install and bring up Harbor with Clair:
2020-01-24 23:59:16 +01:00
```sh
make install GOBUILDIMAGE=golang:1.12.5 COMPILETAG=compile_golangimage CLAIRFLAG=true
```
2019-10-17 15:47:25 +02:00
#### II. Compile code with your own Golang environment, then build Harbor
2020-01-27 19:15:44 +01:00
- Move source code to `$GOPATH`
2019-10-17 15:47:25 +02:00
2020-01-27 19:15:44 +01:00
```sh
mkdir $GOPATH/src/github.com/goharbor/
cd ..
mv harbor $GOPATH/src/github.com/goharbor/.
```
2019-10-17 15:47:25 +02:00
- Build, install and run Harbor without Notary and Clair:
2020-01-27 19:15:44 +01:00
```sh
cd $GOPATH/src/github.com/goharbor/harbor
$ make install
```
2019-10-17 15:47:25 +02:00
- Build, install and run Harbor with Notary and Clair:
2020-01-27 19:15:44 +01:00
```sh
cd $GOPATH/src/github.com/goharbor/harbor
make install -e NOTARYFLAG=true CLAIRFLAG=true
```
2019-10-17 15:47:25 +02:00
### Verify your installation
2020-01-24 23:59:16 +01:00
If everything worked properly, you will see this message:
2019-10-17 15:47:25 +02:00
```sh
2020-01-24 23:59:16 +01:00
...
Start complete. You can visit harbor now.
2019-10-17 15:47:25 +02:00
```
2020-01-27 19:15:44 +01:00
Refer to the [Installation and Configuration Guide ](../installation-guide.md#managing-harbors-lifecycle ) for more information about managing your Harbor instance.
2019-10-17 15:47:25 +02:00
## Appendix
- Using the Makefile
The `Makefile` contains these configurable parameters:
| Variable | Description |
| ------------------- | ---------------------------------------------------------------- |
| BASEIMAGE | Container base image, default: photon |
| DEVFLAG | Build model flag, default: dev |
| COMPILETAG | Compile model flag, default: compile_normal (local golang build) |
| NOTARYFLAG | Notary mode flag, default: false |
| CLAIRFLAG | Clair mode flag, default: false |
| HTTPPROXY | NPM http proxy for Clarity UI builder |
| REGISTRYSERVER | Remote registry server IP address |
| REGISTRYUSER | Remote registry server user name |
| REGISTRYPASSWORD | Remote registry server user password |
| REGISTRYPROJECTNAME | Project name on remote registry server |
| VERSIONTAG | Harbor images tag, default: dev |
| PKGVERSIONTAG | Harbor online and offline version tag, default:dev |
- Predefined targets:
| Target | Description |
| ---------------------- | --------------------------------------------------------------------------------------------------------------------------- |
| all | prepare env, compile binaries, build images and install images |
| prepare | prepare env |
| compile | compile ui and jobservice code |
| compile_portal | compile portal code |
| compile_ui | compile ui binary |
| compile_jobservice | compile jobservice binary |
| build | build Harbor docker images (default: using build_photon) |
| build_photon | build Harbor docker images from Photon OS base image |
| install | compile binaries, build images, prepare specific version of compose file and startup Harbor instance |
| start | startup Harbor instance (set NOTARYFLAG=true when with Notary) |
| down | shutdown Harbor instance (set NOTARYFLAG=true when with Notary) |
| package_online | prepare online install package |
| package_offline | prepare offline install package |
| pushimage | push Harbor images to specific registry server |
| clean all | remove binary, Harbor images, specific version docker-compose file, specific version tag and online/offline install package |
| cleanbinary | remove ui and jobservice binary |
| cleanimage | remove Harbor images |
| cleandockercomposefile | remove specific version docker-compose |
| cleanversiontag | remove specific version tag |
| cleanpackage | remove online/offline install package |
#### EXAMPLE:
#### Push Harbor images to specific registry server
```sh
2020-01-24 23:59:16 +01:00
make pushimage -e DEVFLAG=false REGISTRYSERVER=[$SERVERADDRESS] REGISTRYUSER=[$USERNAME] REGISTRYPASSWORD=[$PASSWORD] REGISTRYPROJECTNAME=[$PROJECTNAME]
2019-10-17 15:47:25 +02:00
```
**Note**: need add "/" on end of REGISTRYSERVER. If REGISTRYSERVER is not set, images will be pushed directly to Docker Hub.
```sh
2020-01-24 23:59:16 +01:00
make pushimage -e DEVFLAG=false REGISTRYUSER=[$USERNAME] REGISTRYPASSWORD=[$PASSWORD] REGISTRYPROJECTNAME=[$PROJECTNAME]
2019-10-17 15:47:25 +02:00
```
#### Clean up binaries and images of a specific version
```sh
2020-01-24 23:59:16 +01:00
make clean -e VERSIONTAG=[TAG]
2019-10-17 15:47:25 +02:00
```
2020-01-27 19:15:44 +01:00
{{< note > }}
If new code has been added to Github, the git commit TAG will change. Better use this command to clean up images and files of previous TAG.
{{< / note > }}
2019-10-17 15:47:25 +02:00
#### By default, the make process create a development build. To create a release build of Harbor, set the below flag to false.
```sh
2020-01-24 23:59:16 +01:00
make XXXX -e DEVFLAG=false
2019-10-17 15:47:25 +02:00
```