Libtorch CMakeLists.txt Building Error

Newbie2008: <p>Whenever I try to build my CMakeLists.txt file, it says that "CMake Error at CMakeLists.txt:4 (find_package):<br>
By not providing “FindTorch.cmake” in CMAKE_MODULE_PATH this project has<br>
asked CMake to find a package configuration file provided by “Torch”, but<br>
CMake did not find one.</p>
<p>Could not find a package configuration file provided by “Torch” with any of<br>
the following names:</p>
<pre><code>TorchConfig.cmake
torch-config.cmake
</code></pre>
<p>Add the installation prefix of “Torch” to CMAKE_PREFIX_PATH or set<br>
“Torch_DIR” to a directory containing one of the above files.  If “Torch”<br>
provides a separate development package or SDK, be sure it has been<br>
installed."</p>
<p>I do not know how to fix this. It would be really helpful if someone could help me.</p>

ptrblck: <p>I would recommend to follow <a href="https://pytorch.org/tutorials/advanced/cpp_frontend.html">this tutorial</a>, which gives you a quick introduction to using <code>libtorch</code>.<br>
As the tutorial and the error message explain, you should use:</p>
<pre><code class="lang-auto">root@fa350df05ecf:/home# mkdir build
root@fa350df05ecf:/home# cd build
root@fa350df05ecf:/home/build# cmake -DCMAKE_PREFIX_PATH=/path/to/libtorch ..
</code></pre>
<p>to build the application.</p>

Newbie2008: <p>Basically I am using CLion and I want to run the program from there. When I use the run feature, it says that the TorchConfig.cmake file is missing</p>

ptrblck: <p>I don’t know, if CLion can build cmake applications, as I’ve never used it.<br>
If not, you would have to build it using the posted instruction.</p>

Wesley_Neill: <p>I get the same error as OP exactly when running cmake from the command line, as copied and pasted from the posted tutorial (Installing C++ Distributions of PyTorch).</p>
<blockquote>
<p>By not providing “FindTorch.cmake” in CMAKE_MODULE_PATH this project has<br>
asked CMake to find a package configuration file provided by “Torch”, but<br>
CMake did not find one.</p>
<p>Add the installation prefix of “Torch” to CMAKE_PREFIX_PATH or set<br>
“Torch_DIR” to a directory containing one of the above files.  If “Torch”<br>
provides a separate development package or SDK, be sure it has been<br>
installed.</p>
</blockquote>
<ul>
<li>
<p>Where is FindTorch.cmake located?</p>
</li>
<li>
<p>What is CMAKE_MODULE_PATH? An environment variable? A CMAKE variable we can set like <code>set(CMAKE_MODULE_PATH "some/path/")</code></p>
</li>
<li>
<p>What is the installation “prefix” (in this specific case, but also generally), and is CMAKE_PREFIX_PATH a variable we set in CMakeLists.txt, or is it an environment variable?</p>
</li>
</ul>

Wesley_Neill: <p>I was getting the same error, despite following official the tutorial for installing libtorch. I was able to resolve it by adding the following line to my CMakeLists.txt:</p>
<pre><code class="lang-auto">set(Torch_DIR path/to/libtorch/source/libtorch-win-shared-with-deps-2.0.1+cu118/libtorch/share/cmake/Torch)
</code></pre>
<p>This is where the file <code>TorchConfig.cmake</code> is located if you have downloaded libtorch as instructed by the tutorial.</p>

rlucas7: <p>Thanks <a class="mention" href="/u/wesley_neill">@Wesley_Neill</a> a similar <code>set()</code> edit worked for me.</p>
<p><a class="mention" href="/u/ptrblck">@ptrblck</a> (or any otherpytorch maintainer) it looks like this is still an open issue, and a couple other related ones w.r.t. the libtorch install doc → <a href="https://pytorch.org/cppdocs/installing.html" class="inline-onebox" rel="noopener nofollow ugc">Installing C++ Distributions of PyTorch — PyTorch main documentation</a> (e.g. selecting the correct distro (I run a mac-x86 at home and was able to build after selecting the latest x86 libtorch distro). Also I found it helpful to read through</p>
<aside class="onebox githubissue" data-onebox-src="https://github.com/pytorch/pytorch/issues/37766">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/issues/37766" target="_blank" rel="noopener nofollow ugc">github.com/pytorch/pytorch</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue" data-github-private-repo="false">
	  <svg width="60" height="60" class="github-icon" viewBox="0 0 14 16" aria-hidden="true"><path fill-rule="evenodd" d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/issues/37766" target="_blank" rel="noopener nofollow ugc">CMake Documentation Issue</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2020-05-04" data-time="15:19:11" data-timezone="UTC">03:19PM - 04 May 20 UTC</span>
      </div>


      <div class="user">
        <a href="https://github.com/oke-aditya" target="_blank" rel="noopener nofollow ugc">
          <img alt="" src="https://avatars.githubusercontent.com/u/47158509?v=4" class="onebox-avatar-inline" width="20" height="20">
          oke-aditya
        </a>
      </div>
    </div>

    <div class="labels">
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          module: docs
        </span>
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          triaged
        </span>
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">It isn't mentioned in PyTorch docs about the Set()
Also, the Cmake file isn't q<span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden">uite complete and needs to be updated. Maybe new users don't know about it.
```
set(CMAKE_PREFIX_PATH "libtorch/share/cmake/Torch")
```
[here](https://pytorch.org/tutorials/advanced/cpp_frontend.html) as well as [here](https://pytorch.org/tutorials/advanced/cpp_export.html)

The CMake file needs to be clear in the documentation. It is causing trouble for many people.
#12449 #21976 #806 #12449

It's not a bug. But a doc improvement issue.</span></p>
  </div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>
<p>
and <a href="https://github.com/pytorch/pytorch/issues/12449" class="inline-onebox" rel="noopener nofollow ugc">Could not find a package configuration file provided by "Torch" with any of the following names: · Issue #12449 · pytorch/pytorch · GitHub</a></p>
<p>the latter is a fairly old issue, if you think it would help I’d be willing to write up some clearer documentation here<br>
<a href="https://pytorch.org/cppdocs/installing.html" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/cppdocs/installing.html</a><br>
for libtorch setup.</p>
<p>Adding screenshot for proofs</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/d/8d4ac050e7616f82316280de9a753ea232bd02b5.png" data-download-href="https://discuss.pytorch.org/uploads/default/8d4ac050e7616f82316280de9a753ea232bd02b5" title="Screenshot 2025-02-20 at 12.19.23 PM"><img src="https://discuss.pytorch.org/uploads/default/original/3X/8/d/8d4ac050e7616f82316280de9a753ea232bd02b5.png" alt="Screenshot 2025-02-20 at 12.19.23 PM" data-base62-sha1="k9VtA1LDihIuhNPNS3vVTNEBMDb" width="482" height="136"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot 2025-02-20 at 12.19.23 PM</span><span class="informations">482×136 12 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>