{"id":24483156,"url":"https://github.com/sukiboo/tf_build_guide","last_synced_at":"2025-09-01T17:39:14.993Z","repository":{"id":128452175,"uuid":"491614330","full_name":"sukiboo/tf_build_guide","owner":"sukiboo","description":"A step-by-step guide to build TensorFlow from source","archived":false,"fork":false,"pushed_at":"2022-05-14T17:49:07.000Z","size":8,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-21T12:35:52.786Z","etag":null,"topics":["guide","tensorflow"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sukiboo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-05-12T17:48:59.000Z","updated_at":"2023-12-10T19:22:21.000Z","dependencies_parsed_at":null,"dependency_job_id":"0420602e-af24-45ee-a1ca-e55b92619407","html_url":"https://github.com/sukiboo/tf_build_guide","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sukiboo%2Ftf_build_guide","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sukiboo%2Ftf_build_guide/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sukiboo%2Ftf_build_guide/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sukiboo%2Ftf_build_guide/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sukiboo","download_url":"https://codeload.github.com/sukiboo/tf_build_guide/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243639405,"owners_count":20323505,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["guide","tensorflow"],"created_at":"2025-01-21T12:31:05.847Z","updated_at":"2025-03-14T19:47:04.575Z","avatar_url":"https://github.com/sukiboo.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Building TensorFlow from source\n\nThis is a step-by-step guide for building a CPU version of TensorFlow on Linux and MacOS.\nOfficial instructions are available at https://www.tensorflow.org/install/source but they are not detailed enough and I'm tired of troubleshooting the same issues every time I need to set up a new machine.\nI wrote this guide mainly to avoid an unnecessary frustration in the future, though someone else might find it useful as well.\n\n\n## Why building from source when you can just `pip install`?\n\nHave you seen messages like these:\n```\nThis TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) \nto use the following CPU instructions in performance-critical operations:  AVX2 FMA\nTo enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.\n```\nThen you can speed up your code by 20+% if you just build TensorFlow from source.\nAll you need is the terminal window, this guide and several hours of free time!\n\n\n## Instructions\n\n### 1. Install prerequisites\nMake sure you have `python` and `pip` installed already.\nOn MacOS you will need Xcode 9.2 or later; check your version with `xcodebuild -version`.\n\n### 2. Choose the version of TensorFlow you want to install\nIt might depend on your particular needs or the version of Python you're using. \nIf you're not sure, a good way to find the latest compatible version is to do a pip-install and then check the version:\n```\npip install tensorflow\npip show tensorflow\n```\nOn my current machine it is `2.6.2`, which is what I'm going to use for the rest of this guide.\n\n### 3. Find the version of Bazel you will need\nFirst, find the required version of Bazel in the `.bazelversion` file of the repository of your version of TensorFlow: https://github.com/tensorflow/tensorflow/blob/v2.6.2/.bazelversion\nIn my case it is `3.7.2` so that's what I'm going to install.\n\n### 4. Install Bazel\nDownload Bazel installer from the relesases page: https://github.com/bazelbuild/bazel/releases/tag/3.7.2 and install it and with `--user` flag:\n```\nchmod +x bazel-3.7.2-installer-linux-x86_64.sh\n./bazel-3.7.2-installer-linux-x86_64.sh --user\n```\n\n### 5. Download TensorFlow source files\nDownload your version (from step 2.) of TensorFlow from https://github.com/tensorflow/tensorflow:\n```\ngit clone -b v2.6.2 https://github.com/tensorflow/tensorflow.git\n```\n\n### 6. Configure TensorFlow\nSpecify the configuration that you need.\nI typically use the default options but your needs may vary.\n```\ncd tensorflow\n./configure\n```\nIf Bazel is not found, run `export PATH=\"$PATH:$HOME/bin\"`\n\n### 7. Compile TensorFlow\nSpecify the optimization flags that you need or choose `arch=native` to use the flags supported by your CPU, which is what I use:\n```\nbazel build --config=opt --copt=-march=native -k //tensorflow/tools/pip_package:build_pip_package\n```\nThis is the most convoluted step that typically takes several hours to complete and many things can go wrong.\nFor instance, here are some of the problems I've experienced in the past:\n- if a package is missing, i.e. `keras-preprocessing`, install it with `pip install keras-preprocessing`\n- if python2 is not found create a symlink via `sudo ln -s /usr/bin/python3 /usr/bin/python`\n- if compilation breaks for no appearent reason, check the logs by running `dmesg`; if you see out of memory errors, e.g. `Out of memory: Kill process 23747 (cc1plus)`, try one of the following fixes:\n  - specify the memory size by adding `--local_ram_resources=8192` flag\n  - reduce the number of workers by adding `-j 1` flag\n- on MacOS, make sure that you're using the correct version of Xcode; if you're not, install and switch via `sudo xcode-select -switch /path_to_xcode/Xcode.app`\n- if nothing else helps, try running `bazel sync` to check the dependencies\n\n### 8. Build TensorFlow\nIf the previous step completed without errors, this should be a pretty straightforward process.\n```\n./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg\n```\n\n### 9. Install TensorFlow\nFinally, install the package.\nKeep in mind that the tags of your package will likely differ from mine!\nIf you have the same version installed already, either uninstall it or run pip with the `--force-reinstall` flag.\n```\npip install /tmp/tensorflow_pkg/tensorflow-2.6.2-cp36-cp36m-linux_x86_64.whl\n```\n\n### 10. Verify successful installation\nImport TensorFlow to check your installation -- make sure to leave the current directory first:\n```python\nimport tensorflow as tf\ntf.Variable(0)\n```\nIf no warnings appear then all is well and you can enjoy your well-earned speed up!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsukiboo%2Ftf_build_guide","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsukiboo%2Ftf_build_guide","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsukiboo%2Ftf_build_guide/lists"}