You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

from-source.en-us.md 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. ---
  2. date: "2016-12-01T16:00:00+02:00"
  3. title: "Installation from source"
  4. slug: "install-from-source"
  5. sidebar_position: 30
  6. toc: false
  7. draft: false
  8. aliases:
  9. - /en-us/install-from-source
  10. menu:
  11. sidebar:
  12. parent: "installation"
  13. name: "From source"
  14. sidebar_position: 30
  15. identifier: "install-from-source"
  16. ---
  17. # Installation from source
  18. You should [install go](https://golang.org/doc/install) and set up your go
  19. environment correctly. In particular, it is recommended to set the `$GOPATH`
  20. environment variable and to add the go bin directory or directories
  21. `${GOPATH//://bin:}/bin` to the `$PATH`. See the Go wiki entry for
  22. [GOPATH](https://github.com/golang/go/wiki/GOPATH).
  23. Next, [install Node.js with npm](https://nodejs.org/en/download/) which is
  24. required to build the JavaScript and CSS files. The minimum supported Node.js
  25. version is @minNodeVersion@ and the latest LTS version is recommended.
  26. **Note**: When executing make tasks that require external tools, like
  27. `make misspell-check`, Gitea will automatically download and build these as
  28. necessary. To be able to use these, you must have the `"$GOPATH/bin"` directory
  29. on the executable path. If you don't add the go bin directory to the
  30. executable path, you will have to manage this yourself.
  31. **Note 2**: Go version @minGoVersion@ or higher is required. However, it is recommended to
  32. obtain the same version as our continuous integration, see the advice given in
  33. [Hacking on Gitea](development/hacking-on-gitea.md)
  34. ## Download
  35. First, we must retrieve the source code. Since, the advent of go modules, the
  36. simplest way of doing this is to use Git directly as we no longer have to have
  37. Gitea built from within the GOPATH.
  38. ```bash
  39. git clone https://github.com/go-gitea/gitea
  40. ```
  41. (Previous versions of this document recommended using `go get`. This is
  42. no longer necessary.)
  43. Decide which version of Gitea to build and install. Currently, there are
  44. multiple options to choose from. The `main` branch represents the current
  45. development version. To build with main, skip to the [build section](#build).
  46. To work with tagged releases, the following commands can be used:
  47. ```bash
  48. git branch -a
  49. git checkout v@version@
  50. ```
  51. To validate a Pull Request, first enable the new branch (`xyz` is the PR id;
  52. for example `2663` for [#2663](https://github.com/go-gitea/gitea/pull/2663)):
  53. ```bash
  54. git fetch origin pull/xyz/head:pr-xyz
  55. ```
  56. To build Gitea from source at a specific tagged release (like v@version@), list the
  57. available tags and check out the specific tag.
  58. List available tags with the following.
  59. ```bash
  60. git tag -l
  61. git checkout v@version@ # or git checkout pr-xyz
  62. ```
  63. ## Build
  64. To build from source, the following programs must be present on the system:
  65. - `go` @minGoVersion@ or higher, see [here](https://golang.org/dl/)
  66. - `node` @minNodeVersion@ or higher with `npm`, see [here](https://nodejs.org/en/download/)
  67. - `make`, see [here](development/hacking-on-gitea.md#installing-make)
  68. Various [make tasks](https://github.com/go-gitea/gitea/blob/main/Makefile)
  69. are provided to keep the build process as simple as possible.
  70. Depending on requirements, the following build tags can be included.
  71. - `bindata`: Build a single monolithic binary, with all assets included. Required for production build.
  72. - `sqlite sqlite_unlock_notify`: Enable support for a
  73. [SQLite3](https://sqlite.org/) database. Suggested only for tiny
  74. installations.
  75. - `pam`: Enable support for PAM (Linux Pluggable Authentication Modules). Can
  76. be used to authenticate local users or extend authentication to methods
  77. available to PAM.
  78. - `gogit`: (EXPERIMENTAL) Use go-git variants of Git commands.
  79. Bundling all assets (JS/CSS/templates, etc) into the binary. Using the `bindata` build tag is required for
  80. production deployments. You could exclude `bindata` when you are developing/testing Gitea or able to separate the assets correctly.
  81. To include all assets, use the `bindata` tag:
  82. ```bash
  83. TAGS="bindata" make build
  84. ```
  85. In the default release build of our continuous integration system, the build
  86. tags are: `TAGS="bindata sqlite sqlite_unlock_notify"`. The simplest
  87. recommended way to build from source is therefore:
  88. ```bash
  89. TAGS="bindata sqlite sqlite_unlock_notify" make build
  90. ```
  91. The `build` target is split into two sub-targets:
  92. - `make backend` which requires [Go @minGoVersion@](https://golang.org/dl/) or greater.
  93. - `make frontend` which requires [Node.js @minNodeVersion@](https://nodejs.org/en/download/) or greater.
  94. If pre-built frontend files are present it is possible to only build the backend:
  95. ```bash
  96. TAGS="bindata" make backend
  97. ```
  98. Webpack source maps are by default enabled in development builds and disabled in production builds. They can be enabled by setting the `ENABLE_SOURCEMAP=true` environment variable.
  99. ## Test
  100. After following the steps above, a `gitea` binary will be available in the working directory.
  101. It can be tested from this directory or moved to a directory with test data. When Gitea is
  102. launched manually from command line, it can be killed by pressing `Ctrl + C`.
  103. ```bash
  104. ./gitea web
  105. ```
  106. ## Changing default paths
  107. Gitea will search for a number of things from the _`CustomPath`_. By default this is
  108. the `custom/` directory in the current working directory when running Gitea. It will also
  109. look for its configuration file _`CustomConf`_ in `$(CustomPath)/conf/app.ini`, and will use the
  110. current working directory as the relative base path _`AppWorkPath`_ for a number configurable
  111. values. Finally the static files will be served from _`StaticRootPath`_ which defaults to the _`AppWorkPath`_.
  112. These values, although useful when developing, may conflict with downstream users preferences.
  113. One option is to use a script file to shadow the `gitea` binary and create an appropriate
  114. environment before running Gitea. However, when building you can change these defaults
  115. using the `LDFLAGS` environment variable for `make`. The appropriate settings are as follows
  116. - To set the _`CustomPath`_ use `LDFLAGS="-X \"code.gitea.io/gitea/modules/setting.CustomPath=custom-path\""`
  117. - For _`CustomConf`_ you should use `-X \"code.gitea.io/gitea/modules/setting.CustomConf=conf.ini\"`
  118. - For _`AppWorkPath`_ you should use `-X \"code.gitea.io/gitea/modules/setting.AppWorkPath=working-path\"`
  119. - For _`StaticRootPath`_ you should use `-X \"code.gitea.io/gitea/modules/setting.StaticRootPath=static-root-path\"`
  120. - To change the default PID file location use `-X \"code.gitea.io/gitea/cmd.PIDFile=/run/gitea.pid\"`
  121. Add as many of the strings with their preceding `-X` to the `LDFLAGS` variable and run `make build`
  122. with the appropriate `TAGS` as above.
  123. Running `gitea help` will allow you to review what the computed settings will be for your `gitea`.
  124. ## Cross Build
  125. The `go` compiler toolchain supports cross-compiling to different architecture targets that are supported by the toolchain. See [`GOOS` and `GOARCH` environment variable](https://golang.org/doc/install/source#environment) for the list of supported targets. Cross compilation is helpful if you want to build Gitea for less-powerful systems (such as Raspberry Pi).
  126. To cross build Gitea with build tags (`TAGS`), you also need a C cross compiler which targets the same architecture as selected by the `GOOS` and `GOARCH` variables. For example, to cross build for Linux ARM64 (`GOOS=linux` and `GOARCH=arm64`), you need the `aarch64-unknown-linux-gnu-gcc` cross compiler. This is required because Gitea build tags uses `cgo`'s foreign-function interface (FFI).
  127. Cross-build Gitea for Linux ARM64, without any tags:
  128. ```
  129. GOOS=linux GOARCH=arm64 make build
  130. ```
  131. Cross-build Gitea for Linux ARM64, with recommended build tags:
  132. ```
  133. CC=aarch64-unknown-linux-gnu-gcc GOOS=linux GOARCH=arm64 TAGS="bindata sqlite sqlite_unlock_notify" make build
  134. ```
  135. Replace `CC`, `GOOS`, and `GOARCH` as appropriate for your architecture target.
  136. You will sometimes need to build a static compiled image. To do this you will need to add:
  137. ```
  138. LDFLAGS="-linkmode external -extldflags '-static' $LDFLAGS" TAGS="netgo osusergo $TAGS" make build
  139. ```
  140. This can be combined with `CC`, `GOOS`, and `GOARCH` as above.
  141. ### Adding bash/zsh autocompletion (from 1.19)
  142. A script to enable bash-completion can be found at [`contrib/autocompletion/bash_autocomplete`](https://raw.githubusercontent.com/go-gitea/gitea/main/contrib/autocompletion/bash_autocomplete). This should be altered as appropriate and can be `source` in your `.bashrc`
  143. or copied as `/usr/share/bash-completion/completions/gitea`.
  144. Similarly, a script for zsh-completion can be found at [`contrib/autocompletion/zsh_autocomplete`](https://raw.githubusercontent.com/go-gitea/gitea/main/contrib/autocompletion/zsh_autocomplete). This can be copied to `/usr/share/zsh/_gitea` or sourced within your
  145. `.zshrc`.
  146. YMMV and these scripts may need further improvement.
  147. ## Compile or cross-compile using Linux with Zig
  148. Follow [Getting Started of Zig](https://ziglang.org/learn/getting-started/#installing-zig) to install zig.
  149. - Compile (Linux ➝ Linux)
  150. ```sh
  151. CC="zig cc -target x86_64-linux-gnu" \
  152. CGO_ENABLED=1 \
  153. CGO_CFLAGS="-O2 -g -pthread" \
  154. CGO_LDFLAGS="-linkmode=external -v"
  155. GOOS=linux \
  156. GOARCH=amd64 \
  157. TAGS="bindata sqlite sqlite_unlock_notify" \
  158. make build
  159. ```
  160. - Cross-compile (Linux ➝ Windows)
  161. ```sh
  162. CC="zig cc -target x86_64-windows-gnu" \
  163. CGO_ENABLED=1 \
  164. CGO_CFLAGS="-O2 -g -pthread" \
  165. GOOS=windows \
  166. GOARCH=amd64 \
  167. TAGS="bindata sqlite sqlite_unlock_notify" \
  168. make build
  169. ```
  170. ## Compile or cross-compile with Zig using Windows
  171. Compile with `GIT BASH`.
  172. - Compile (Windows ➝ Windows)
  173. ```sh
  174. CC="zig cc -target x86_64-windows-gnu" \
  175. CGO_ENABLED=1 \
  176. CGO_CFLAGS="-O2 -g -pthread" \
  177. GOOS=windows \
  178. GOARCH=amd64 \
  179. TAGS="bindata sqlite sqlite_unlock_notify" \
  180. make build
  181. ```
  182. - Cross-compile (Windows ➝ Linux)
  183. ```sh
  184. CC="zig cc -target x86_64-linux-gnu" \
  185. CGO_ENABLED=1 \
  186. CGO_CFLAGS="-O2 -g -pthread" \
  187. CGO_LDFLAGS="-linkmode=external -v"
  188. GOOS=linux \
  189. GOARCH=amd64 \
  190. TAGS="bindata sqlite sqlite_unlock_notify" \
  191. make build
  192. ```