Building Turso Golang Client on FreeBSD
At Conrad Research, we love Turso, Go, and FreeBSD. This guide walks you through building and installing the Turso Go client on FreeBSD, so that you can start writing Go applications that connect to Turso from your FreeBSD server.
Prerequisites
This guide is for FreeBSD running on x86_64 architecture. If you're running on a different architecture, such as ARM, you may need to adjust the steps accordingly.
Steps
1. Make sure prerequisites are installed
Install Git, Go, and LLVM
pkg install git golang llvm
git --version
go version
llvm-config --version
If you encounter missing shared library errors when running 'git', check which command is missing:
ldd $(which git)Adjust 'LD_LIBRARY_PATH' in '~/.profile' if needed:
echo 'export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH' >> ~/.profile . ~/.profile
Install and initialize 'Rustup' from FreeBSD ports.
cd /usr/ports/devel/rustup-init
make install clean
rustup-init
Make sure that ~/.cargo/bin is in your PATH.
echo $PATH
If it's not, then edit your
~/.profilefile and add the following line:echo 'export PATH=$PATH:~/.cargo/bin' >> ~/.profile . ~/.profile
2. Build libsql library binding for FreeBSD.
Build libsql library:
Note: if you encounter an error about 'cross' not being found, make sure that ~/.cargo/bin is in your PATH
git clone https://github.com/tursodatabase/libsql.git
cd libsql/bindings/c
cargo build --release --target x86_64-unknown-freebsd
In the next step we'll copy the compiled library to the Turso Go client's libs directory.
3. Fork and Patch the Turso Go client binary.
Create a fork of the Turso Go client repository on Github. If you're not sure how to do this, follow the instructions on the Github website
Clone your above fork to your local development environment.
Note: I'm using SSH here, but you can also use HTTPS if you prefer. I find it more convenient to use SSH keys for authentication when pushing changes to your fork on Github.
git clone [email protected]:yourusername/go-libsql.git
cd go-libsql
Set the original repository as the upstream remote and verify that it is set correctly:
git remote add upstream https://github.com/tursodatabase/go-libsql.git
git remote -v
Create a new branch for FreeBSD support:
git checkout -b freebsd-support
Create a new directory for FreeBSD support and then copy the compiled library to the Turso Go client's libs directory:
mkdir -p lib/freebsd_amd64
cp [Path from previous step/target/x86_64-unknown-freebsd/release/libsql_experimental.a lib/freebsd_amd64/libsql_experimental.a
Update the CGO flags in 'libsql.go' to include the FreeBSD library path. You'll see the following towards the top of the file:
#cgo darwin,amd64 LDFLAGS: -L${SRCDIR}/lib/darwin_amd64
#cgo darwin,arm64 LDFLAGS: -L${SRCDIR}/lib/darwin_arm64
#cgo linux,amd64 LDFLAGS: -L${SRCDIR}/lib/linux_amd64
#cgo linux,arm64 LDFLAGS: -L${SRCDIR}/lib/linux_arm64
We'll just need to add the FreeBSD library path to the CGO flags:
#cgo freebsd,amd64 LDFLAGS: -L${SRCDIR}/lib/freebsd_amd64
Update the go.mod file's module path so that you can import the forked repository's branch into your project:
go mod edit -module github.com/yourusername/go-libsql
Push the changes to your forked repository:
git add .
git commit -m "Add FreeBSD support"
git push origin freebsd-support
Find the latest commit hash of your branch and use it to import your forked repository's branch in your project:
git ls-remote https://github.com/yourusername/go-libsql.git | head
go get github.com/yourusername/go-libsql@<CommitHash>
Now you can finally build your project using the Turso Go client on your FreeBSD system. Just make sure to enable CGO when building your project.
CGO_ENABLED=1 go build main.go
4. Keeping Your Fork Up-to-Date with the Original Repository.
To keep your fork up-to-date with the original repository, follow these steps:
- Fetch the latest changes from the upstream repository:
git fetch upstream
- Merge the changes into your branch:
git merge upstream/main
-
Resolve any conflicts that may arise during the merge process.
-
Push the updated branch to your forked repository:
git push origin freebsd-support
5. Conclusion
It may require some effort, but the payoff of running Turso on FreeBSD is well worth it.
Cheers 🥂