Configuring a development environment for Bun can take 10-30 minutes depending on your internet connection and computer speed. You will need ~10GB of free disk space for the repository and build artifacts. If you are using Windows, you must use a WSL environment as Bun does not yet compile on Windows natively. Before starting, you will need to already have a release build of Bun installed, as we use our bundler to transpile and minify our code. {% codetabs %} ```bash#Native $ curl -fsSL https://bun.sh/install | bash # for macOS, Linux, and WSL ``` ```bash#npm $ npm install -g bun # the last `npm` command you'll ever need ``` ```bash#Homebrew $ brew tap oven-sh/bun # for macOS and Linux $ brew install bun ``` ```bash#Docker $ docker pull oven/bun $ docker run --rm --init --ulimit memlock=-1:-1 oven/bun ``` ```bash#proto $ proto install bun ``` {% /codetabs %} ## Install LLVM Bun requires LLVM 15 and Clang 15 (`clang` is part of LLVM). This version requirement is to match WebKit (precompiled), as mismatching versions will cause memory allocation failures at runtime. In most cases, you can install LLVM through your system package manager: {% codetabs %} ```bash#macOS (Homebrew) $ brew install llvm@15 ``` ```bash#Ubuntu/Debian $ # LLVM has an automatic installation script that is compatible with all versions of Ubuntu $ wget https://apt.llvm.org/llvm.sh -O - | sudo bash -s -- 15 all ``` ```bash#Arch $ sudo pacman -S llvm clang lld ``` {% /codetabs %} If none of the above solutions apply, you will have to install it [manually](https://github.com/llvm/llvm-project/releases/tag/llvmorg-15.0.7). Make sure LLVM 15 is in your path: ```bash $ which clang-15 ``` If not, run this to manually link it: {% codetabs %} ```bash#macOS (Homebrew) # use fish_add_path if you're using fish $ export PATH="$PATH:$(brew --prefix llvm@15)/bin" $ export LDFLAGS="$LDFLAGS -L$(brew --prefix llvm@15)/lib" $ export CPPFLAGS="$CPPFLAGS -I$(brew --prefix llvm@15)/include" ``` {% /codetabs %} ## Install Dependencies Using your system's package manager, install the rest of Bun's dependencies: {% codetabs %} ```bash#macOS (Homebrew) $ brew install automake ccache cmake coreutils esbuild gnu-sed go libiconv libtool ninja pkg-config rust ``` ```bash#Ubuntu/Debian $ sudo apt install cargo ccache cmake git golang libtool ninja-build pkg-config rustc esbuild ``` ```bash#Arch $ pacman -S base-devel ccache cmake esbuild git go libiconv libtool make ninja pkg-config python rust sed unzip ``` {% /codetabs %} {% details summary="Ubuntu — Unable to locate package esbuild" %} The `apt install esbuild` command may fail with an `Unable to locate package` error if you are using a Ubuntu mirror that does not contain an exact copy of the original Ubuntu server. Note that the same error may occur if you are not using any mirror but have the Ubuntu Universe enabled in the `sources.list`. In this case, you can install esbuild manually: ```bash $ curl -fsSL https://esbuild.github.io/dl/latest | sh $ chmod +x ./esbuild $ sudo mv ./esbuild /usr/local/bin ``` {% /details %} In addition to this, you will need an npm package manager (`bun`, `npm`, etc) to install the `package.json` dependencies. ## Install Zig Zig can be installed either with our npm package [`@oven/zig`](https://www.npmjs.com/package/@oven/zig), or by using [zigup](https://github.com/marler8997/zigup). ```bash $ bun install -g @oven/zig $ zigup 0.11.0-dev.3737+9eb008717 ``` ## Building After cloning the repository, run the following command. The runs ```bash $ make setup ``` Then to build Bun: ```bash $ make dev ``` The binary will be located at `packages/debug-bun-{platform}-{arch}/bun-debug`. It is recommended to add this to your `$PATH`. To verify the build worked, lets print the version number on the development build of Bun. ```bash $ packages/debug-bun-*/bun-debug --version bun 0.x.y__dev ``` ## VSCode VSCode is the recommended IDE for working on Bun, as it has been configured. Once opening, you can run `Extensions: Show Recommended Extensions` to install the recommended extensions for Zig and C++. ZLS is automatically configured. ## JavaScript builtins When you change anything in `src/js/builtins/*` or switch branches, run this: ```bash $ make regenerate-bindings ``` That inlines the TypeScript code into C++ headers. {% callout %} Make sure you have `ccache` installed, otherwise regeneration will take much longer than it should. {% /callout %} ## Code generation scripts Bun leverages a lot of code generation scripts. The [./src/bun.js/bindings/headers.h](https://github.com/oven-sh/bun/blob/main/src/bun.js/bindings/headers.h) file has bindings to & from Zig <> C++ code. This file is generated by running the following: ```bash $ make headers ``` This ensures that the types for Zig and the types for C++ match up correctly, by using comptime reflection over functions exported/imported. TypeScript files that end with `*.classes.ts` are another code generation script. They generate C++ boilerplate for classes implemented in Zig. The generated code lives in: - [src/bun.js/bindings/ZigGeneratedClasses.cpp](https://github.com/oven-sh/bun/tree/main/src/bun.js/bindings/ZigGeneratedClasses.cpp) - [src/bun.js/bindings/ZigGeneratedClasses.h](https://github.com/oven-sh/bun/tree/main/src/bun.js/bindings/ZigGeneratedClasses.h) - [src/bun.js/bindings/generated_classes.zig](https://github.com/oven-sh/bun/tree/main/src/bun.js/bindings/generated_classes.zig) To generate the code, run: ```bash $ make codegen ``` Lastly, we also have a [code generation script](src/bun.js/scripts/generate-jssink.js) for our native stream implementations. To run that, run: ```bash $ make generate-sink ``` You probably won't need to run that one much. ## Modifying ESM modules Certain modules like `node:fs`, `node:stream`, `bun:sqlite`, and `ws` are implemented in JavaScript. These live in `src/js/{node,bun,thirdparty}` files and are pre-bundled using Bun. The bundled code is committed so CI builds can run without needing a copy of Bun. When these are changed, run: ``` $ make esm ``` In debug builds, Bun automatically loads these from the filesystem, wherever it was compiled, so no need to re-run `make dev`. In release builds, this same behavior can be done via the environment variable `BUN_OVERRIDE_MODULE_PATH`. When set to the repository root, Bun will read from the bundled modules in the repository instead of the ones baked into the binary. ## Release build To build a release build of Bun, run: ```bash $ make release-bindings -j12 $ make release ``` The binary will be located at `packages/bun-{platform}-{arch}/bun`. ## Valgrind On Linux, valgrind can help find memory issues. Keep in mind: - JavaScriptCore doesn't support valgrind. It will report spurious errors. - Valgrind is slow - Mimalloc will sometimes cause spurious errors when debug build is enabled You'll need a very recent version of Valgrind due to DWARF 5 debug symbols. You may need to manually compile Valgrind instead of using it from your Linux package manager. `--fair-sched=try` is necessary if running multithreaded code in Bun (such as the bundler). Otherwise it will hang. ```bash $ valgrind --fair-sched=try --track-origins=yes bun-debug ``` ## Updating `WebKit` The Bun team will occasionally bump the version of WebKit used in Bun. When this happens, you may see something like this with you run `git status`. ```bash $ git status On branch my-branch Changes not staged for commit: (use "git add ..." to update what will be committed) (use "git restore ..." to discard changes in working directory) modified: src/bun.js/WebKit (new commits) ``` For performance reasons, `make submodule` does not automatically update the WebKit submodule. To update, run the following commands from the root of the Bun repo: ```bash $ bun install $ make regenerate-bindings ``` ## Troubleshooting ### libarchive If you see an error when compiling `libarchive`, run this: ```bash $ brew install pkg-config ``` ### missing files on `zig build obj` If you see an error about missing files on `zig build obj`, make sure you built the headers. ```bash $ make headers ``` ### cmakeconfig.h not found If you see an error about `cmakeconfig.h` not being found, this is because the precompiled WebKit did not install properly. ```bash $ bun install ``` Check to see the command installed webkit, and you can manully look for `node_modules/bun-webkit-{platform}-{arch}`: ```bash # this should reveal two directories. if not, something went wrong $ echo node_modules/bun-webkit* ``` ### macOS `library not found for -lSystem` If you see this error when compiling, run: ```bash $ xcode-select --install ``` ## Arch Linux / Cannot find `libatomic.a` Bun requires `libatomic` to be statically linked. On Arch Linux, it is only given as a shared library, but as a workaround you can symlink it to get the build working locally. ```bash $ sudo ln -s /lib/libatomic.so /lib/libatomic.a ``` The built version of bun may not work on other systems if compiled this way.