Get up and running with Kimi, GLM, MiniMax, DeepSeek, gpt-oss, Qwen, Gemma and other models.
x/imagegen: install mlx.metallib and fix macOS rpath handling, add mlx library directories to LD_LIBRARY_PATH (#13695)
Jeffrey Morganaf7ea6e96e52
diff --git a/CMakeLists.txt b/CMakeLists.txtindex 6ea95f7d3..7724faa03 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -48,9 +48,10 @@ if((CMAKE_OSX_ARCHITECTURES AND NOT CMAKE_OSX_ARCHITECTURES MATCHES "arm64")set(GGML_CPU_ALL_VARIANTS ON)endif()-if (CMAKE_OSX_ARCHITECTURES MATCHES "x86_64")+if(APPLE)set(CMAKE_BUILD_RPATH "@loader_path")set(CMAKE_INSTALL_RPATH "@loader_path")+ set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)endif()set(OLLAMA_BUILD_DIR ${CMAKE_BINARY_DIR}/lib/ollama)@@ -196,6 +197,14 @@ if(MLX_ENGINE)FRAMEWORK DESTINATION ${OLLAMA_INSTALL_DIR} COMPONENT MLX)+ # Install the Metal library for macOS arm64 (must be colocated with the binary)+ # Metal backend is only built for arm64, not x86_64+ if(APPLE AND CMAKE_OSX_ARCHITECTURES MATCHES "arm64")+ install(FILES ${CMAKE_BINARY_DIR}/_deps/mlx-build/mlx/backend/metal/kernels/mlx.metallib+ DESTINATION ${OLLAMA_INSTALL_DIR}+ COMPONENT MLX)+ endif()+# Manually install cudart and cublas since they might not be picked up as direct dependenciesif(CUDAToolkit_FOUND)file(GLOB CUDART_LIBSdiff --git a/scripts/build_darwin.sh b/scripts/build_darwin.shindex 7b9937aa0..d5c5ee17e 100755--- a/scripts/build_darwin.sh+++ b/scripts/build_darwin.sh@@ -160,6 +160,8 @@ _build_macapp() {donecp dist/darwin-*/lib/ollama/*.so dist/darwin-*/lib/ollama/*.dylib dist/Ollama.app/Contents/Resources/cp dist/darwin/*.dylib dist/Ollama.app/Contents/Resources/+ # Copy MLX metallib (architecture-independent, just use arm64 version)+ cp dist/darwin-arm64/lib/ollama/*.metallib dist/Ollama.app/Contents/Resources/ 2>/dev/null || trueelsecp -a dist/darwin/ollama dist/Ollama.app/Contents/Resources/ollamacp dist/darwin/*.so dist/darwin/*.dylib dist/Ollama.app/Contents/Resources/@@ -170,7 +172,7 @@ _build_macapp() {# Signif [ -n "$APPLE_IDENTITY" ]; thencodesign -f --timestamp -s "$APPLE_IDENTITY" --identifier ai.ollama.ollama --options=runtime dist/Ollama.app/Contents/Resources/ollama- for lib in dist/Ollama.app/Contents/Resources/*.so dist/Ollama.app/Contents/Resources/*.dylib dist/Ollama.app/Contents/Resources/ollama-mlx ; do+ for lib in dist/Ollama.app/Contents/Resources/*.so dist/Ollama.app/Contents/Resources/*.dylib dist/Ollama.app/Contents/Resources/*.metallib dist/Ollama.app/Contents/Resources/ollama-mlx ; docodesign -f --timestamp -s "$APPLE_IDENTITY" --identifier ai.ollama.ollama --options=runtime ${lib}donecodesign -f --timestamp -s "$APPLE_IDENTITY" --identifier com.electron.ollama --deep --options=runtime dist/Ollama.app@@ -178,7 +180,7 @@ _build_macapp() {rm -f dist/Ollama-darwin.zipditto -c -k --keepParent dist/Ollama.app dist/Ollama-darwin.zip- (cd dist/Ollama.app/Contents/Resources/; tar -cf - ollama ollama-mlx *.so *.dylib) | gzip -9vc > dist/ollama-darwin.tgz+ (cd dist/Ollama.app/Contents/Resources/; tar -cf - ollama ollama-mlx *.so *.dylib *.metallib 2>/dev/null) | gzip -9vc > dist/ollama-darwin.tgz# Notarize and Stapleif [ -n "$APPLE_IDENTITY" ]; thendiff --git a/x/imagegen/server.go b/x/imagegen/server.goindex e980b4944..f9deb11d7 100644--- a/x/imagegen/server.go+++ b/x/imagegen/server.go@@ -14,7 +14,9 @@ import ("os""os/exec""path/filepath"+ "runtime""strconv"+ "strings""sync""time"@@ -84,6 +86,36 @@ func NewServer(modelName string) (*Server, error) {cmd := exec.Command(mlxExe, "runner", "--image-engine", "--model", modelName, "--port", strconv.Itoa(port))cmd.Env = os.Environ()+ // On Linux, set LD_LIBRARY_PATH to include MLX library directories+ if runtime.GOOS == "linux" {+ // Build library paths: start with LibOllamaPath, then add any mlx_* subdirectories+ libraryPaths := []string{ml.LibOllamaPath}+ if mlxDirs, err := filepath.Glob(filepath.Join(ml.LibOllamaPath, "mlx_*")); err == nil {+ libraryPaths = append(libraryPaths, mlxDirs...)+ }++ // Append existing LD_LIBRARY_PATH if set+ if existingPath, ok := os.LookupEnv("LD_LIBRARY_PATH"); ok {+ libraryPaths = append(libraryPaths, filepath.SplitList(existingPath)...)+ }++ pathEnvVal := strings.Join(libraryPaths, string(filepath.ListSeparator))++ // Update or add LD_LIBRARY_PATH in cmd.Env+ found := false+ for i := range cmd.Env {+ if strings.HasPrefix(cmd.Env[i], "LD_LIBRARY_PATH=") {+ cmd.Env[i] = "LD_LIBRARY_PATH=" + pathEnvVal+ found = true+ break+ }+ }+ if !found {+ cmd.Env = append(cmd.Env, "LD_LIBRARY_PATH="+pathEnvVal)+ }+ slog.Debug("mlx subprocess library path", "LD_LIBRARY_PATH", pathEnvVal)+ }+s := &Server{cmd: cmd,port: port,