For: Linux distro developers Subject: Building Vulkan ollama from source I've be...
OldCoder @OldCoder@dansu.org
For: Linux distro developers
Subject: Building Vulkan ollama from source
I've been working on my own Linux distro [Laclin] for 30 years.
This week, I managed to get a #Vulkan copy of #ollama to build in my distro. This post is a high-level overview of the process for other distro developers. Note: "Vulkan" means that it only works for some chipsets but it works out of the box in those cases.
Advisory: Builds from source are straight-forward in standard distros. If you're creating a non-standard distro from source, the process isn't for the faint-hearted. New packages can mean the need to juggle a dozen pieces.
"ollama" dependencies include the following packages. If your distro breaks packages into production and devel versions, the devel versions are needed.
bzip2, cmake, elfutils, eudev, expat, gcc [or clang], git, go, make, ncurses, pkgconf, python3, xorg, xzutils, zlib, zstd
To build "ollama" from source, you need to collect sources for the following pieces and get each of them to build separately.
xorg - You need to build "libvulkan_*.so" libraries [note the underscore] and install Vulkan ICD "*.json" files. These steps are part of the build of the "xorg" Mesa component, so you might find that they've already been done.
glslang - This needs to be installed as a separate package and also as a source tree that goes into the "shaderc" source tree. The build per se is simple "cmake".
git repo: https://github.com/KhronosGroup/glslang.git
spirv-headers - This needs to be installed as a separate package and also as a source tree that goes into the "spirv-tools" source tree. The build per se is simple "cmake".
git repo: https://github.com/KhronosGroup/SPIRV-Headers.git
spirv-tools - You need to add a copy of the "spirv-headers" source tree to this source tree. The build per se is simple "cmake".
git repo: https://github.com/KhronosGroup/SPIRV-Tools.git
shaderc - You need to add copies of the "glslang" and "spirv-tools" source trees to this source tree. The build per se is simple "cmake".
For this package, I recommend that you add the following "cmake" option setting:
-DSHADERC_SKIP_TESTS=ON
git repo: https://github.com/google/shaderc.git
vulkan-headers - The build for this package is simple "cmake".
git repo: https://github.com/KhronosGroup/Vulkan-Headers.git
vulkan-loader - This package provides a high-level Vulkan library named "libvulkan.so". That file is separate from the "libvulkan_*.so" files mentioned above. The build is simple "cmake".
git repo: https://github.com/KhronosGroup/Vulkan-Loader.git
vulkan-sdk - I didn't use upstream sources directly for this package. In my version:
There is a "bin" directory. It contains symlinks to the "glslang" and "glslangValidator" executables in the "glslang" package.
There are "include" and "share" directories. These are simply the corresponding "vulkan-headers" directories. "vulkan-headers" is installed to the "vulkan-sdk" prefix.
There is a "lib" directory. It is simply the "vulkan-loader" lib directory. "vulkan-loader" is installed to the "vulkan-sdk" prefix. Symlinks to the "xorg" "libvulkan_*.so" library files are added to the "lib" directory.
ollama - This is the tricky package in the list.
git repo: https://github.com/ollama/ollama.git
The build procedure for "ollama" looks roughly like this:
# Path to desired "ollama" prefix directory
PP=/somewhere/ollama
rm -fr $PP
BINDIR=$PP/bin
DATADIR=$PP/data
LIBDIR=$PP/lib
mkdir -p $BINDIR $DATADIR $LIBDIR
# Path to "spirv-headers" prefix directory
export CMAKE_PREFIX_PATH=/somewhere/spirv-headers
# Path to "vulkan-sdk" prefix directory
export VULKAN_SDK=/somewhere/vulkan-sdk
# Path to "xorg" prefix directory [export isn't needed]
XORG_PREFIX=/somewhere/xorg
rm -fr ollama
git clone --depth 1 https://github.com/ollama/ollama.git
cd ollama
go generate ./...
pushd llama/server/
cmake -DGGML_VULKAN=ON -DCMAKE_BUILD_TYPE=Release .
make -j$(nproc)
popd
go build
mv ollama $BINDIR/
mv llama/server/bin/*.so* $LIBDIR/
mv llama/server/bin/* $BINDIR/
cp -p $XORG_PREFIX/share/vulkan/icd.d/*.json $DATADIR/Installation of LLM models and how to run the "ollama" server and client is/are beyond the scope of these notes.
Here's one tip related to that: At runtime, you'll need to add the BINDIR and LIBDIR settings indicated above to PATH and LD_LIBRARY_PATH [respectively]. In "bash", the latter two environment variables should also exported using "export".