|
| 1 | +--- |
| 2 | +title: "Build with system-installed LLVM" |
| 3 | +weight: 4 |
| 4 | +description: > |
| 5 | + How to build TinyGo with a system-installed version of LLVM. |
| 6 | +--- |
| 7 | + |
| 8 | +⚠️ Halt! This is the system installed LLVM guide! Please check the following table and make sure you don't need the [manual LLVM install guide](./manual-llvm.md) instead! |
| 9 | + |
| 10 | + |
| 11 | +| You need to build LLVM manually in the following cases | |
| 12 | +|---| |
| 13 | +| You would like to use it for the ESP8266 or ESP32 chips | |
| 14 | +| You are using Windows. | |
| 15 | +| Your Linux distribution (if you use Linux) does not ship the right LLVM version. | |
| 16 | + |
| 17 | + |
| 18 | +Using a system-installed version of LLVM depends on your system, of course. |
| 19 | + |
| 20 | +For **Debian** or **Ubuntu** you can install LLVM by adding a new apt repository. For more information about this method, see [apt.llvm.org](https://apt.llvm.org/). *Before copying the command below, please replace `xxxxx` with your distribution's codename*. |
| 21 | + |
| 22 | +| Distro | Version | Codename | |
| 23 | +|--------|------- |-----------| |
| 24 | +| Ubuntu | 18.04 | `bionic` | |
| 25 | +| Ubuntu | 20.04 | `focal` | |
| 26 | +| Ubuntu | 20.10 | `groovy` | |
| 27 | +| Ubuntu | 21.04 | `hirsute` | |
| 28 | +| Ubuntu | 22.04 | `jammy` | |
| 29 | +| Debian | 10 | `buster` | |
| 30 | +| Debian | 11 | `bullseye`| |
| 31 | +| Debian | sid | `unstable`| |
| 32 | + |
| 33 | +```shell |
| 34 | +echo 'deb http://apt.llvm.org/xxxxx/ llvm-toolchain-xxxxx-16 main' | sudo tee /etc/apt/sources.list.d/llvm.list |
| 35 | +``` |
| 36 | + |
| 37 | +After adding the apt repository for your distribution you may install the LLVM toolchain packages: |
| 38 | + |
| 39 | +```shell |
| 40 | +wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add - |
| 41 | +sudo apt-get update |
| 42 | +sudo apt-get install clang-16 llvm-16-dev lld-16 libclang-16-dev |
| 43 | +``` |
| 44 | + |
| 45 | +For **MacOS**, you can install LLVM through [Homebrew](https://formulae.brew.sh/formula/llvm). The Clang/LLVM version from Apple is not supported by TinyGo. |
| 46 | + |
| 47 | +```shell |
| 48 | +brew install llvm |
| 49 | +``` |
| 50 | + |
| 51 | +For **Fedora** users you can install LLVM from the repository. Note that the version of LLVM [varies by Fedora version](https://packages.fedoraproject.org/pkgs/llvm/llvm-libs/), for example Fedora 37 has LLVM 15. |
| 52 | + |
| 53 | +```shell |
| 54 | +sudo dnf install llvm-devel lld-libs lld |
| 55 | +``` |
| 56 | + |
| 57 | +After LLVM has been installed, installing TinyGo should be as easy as running the following command: |
| 58 | + |
| 59 | +```shell |
| 60 | +go install |
| 61 | +``` |
| 62 | + |
| 63 | +You should now have a working TinyGo installation! |
| 64 | + |
| 65 | +If you have gotten this far, please refer to [Additional requirements](./additional-requirements) to further set up TinyGo. |
| 66 | + |
| 67 | +## Final notes |
| 68 | + |
| 69 | +### `go install` command inner workings |
| 70 | +The `go install` command will build the `tinygo` executable and store it to your currently set `$GOBIN` directory. A couple environment variables must be set for TinyGo to work after running this: |
| 71 | + |
| 72 | +* Your `GOBIN` should be set to where you want go binaries to be stored to. Below is an example (for linux systems) |
| 73 | + ```shell |
| 74 | + go env -w GOBIN=$HOME/local/bin |
| 75 | + ``` |
| 76 | +* The directory should exist! Below is a way to create the directory on linux. |
| 77 | + ```shell |
| 78 | + mkdir -p $HOME/local/bin |
| 79 | + ``` |
| 80 | +* Your $PATH environment variable should contain the directory so that you can run `tinygo` from the command line! A reliable way to achieve this can be found at the [Go install page](https://go.dev/doc/install#). For linux it consists of adding the line below to the bottom of your `/etc/profile` file (before the `exit 0`): |
| 81 | + ``` |
| 82 | + export PATH=$PATH:$HOME/local/bin |
| 83 | + ``` |
| 84 | + |
| 85 | + |
| 86 | +### GCC errors |
| 87 | +If you are getting an `gcc` or `g++ not found` error you most likely do not have a working C++ build environment. You'll need the `build-essential` package on Debian or `sudo dnf install make automake gcc gcc-c++` for Fedora based systems. |
| 88 | +
|
| 89 | +If you are getting a build error like this, LLVM is not installed as expected: |
| 90 | +
|
| 91 | +``` |
| 92 | +# tinygo.org/x/go-llvm |
| 93 | +../../../go/pkg/mod/tinygo.org/x/go-llvm@v0.0.0-20221028183034-8341240c0b32/analysis.go:16:10: fatal error: 'llvm-c/Analysis.h' file not found |
| 94 | +#include "llvm-c/Analysis.h" // If you are getting an error here you need to build or install LLVM, see https://tinygo.org/docs/guides/build/ |
| 95 | + ^~~~~~~~~~~~~~~~~~~ |
| 96 | +1 error generated. |
| 97 | +``` |
| 98 | +
|
| 99 | +This can often be fixed by specifying the LLVM version as a build tag, for example `-tags=llvm14` if you have LLVM 14 instead of LLVM 16. |
| 100 | +
|
| 101 | +### Additional notes |
| 102 | +Note that you should not use `make` when you want to build using a system-installed LLVM, just use the Go toolchain. |
0 commit comments