ImportError: libtensorflow_cc.so.2: cannot open shared object file: No such file or directory
@ProgrammerPeter 
Hi, Thanks for opening the issue,

It seems like the libtensorflow_cc.so.2 file is not being included in the generated TensorFlow wheel file. Here are a few suggestions to fix the issue:

Try building the TensorFlow package again with the following command:
```sh
bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
```
This command will build the TensorFlow package with support for CUDA-enabled GPUs. If you don't have a GPU, you can skip the --config=cuda flag.

Check the contents of the generated wheel file to see if the libtensorflow_cc.so.2 file is present. You can do this using the following command:
```sh
pip show tensorflow
```
This command will display information about the installed TensorFlow package, including its location on your system. Once you have the location of the package, you can navigate to the package directory and inspect its contents.

If the libtensorflow_cc.so.2 file is missing from the package, you can manually copy it into the appropriate directory. The directory structure of the TensorFlow package should be similar to the following:
```sh
tensorflow/
  __init__.py
  _api/
    ...
  _api_v1/
    ...
  _api.v2/
    ...
  ...
```
The libtensorflow_cc.so.2 file should be placed in the tensorflow directory alongside the other Python modules.

If none of the above solutions work, try building TensorFlow from source again, but this time with the following command:
```sh
bazel build --config=monolithic //tensorflow/tools/pip_package:build_pip_package
```
This command will build a monolithic TensorFlow package, which includes all of the necessary shared libraries. It seems like the issue is related to the missing shared library libtensorflow_cc.so.2. This can happen if the library is not found in the expected location or if it is not included in the TensorFlow package.

Here are some things you could try to resolve the issue:

Check if the library is installed:
Use the find command to search for the library in your system. If it is found, check if the path to the library is included in the LD_LIBRARY_PATH environment variable.

Try rebuilding TensorFlow:
The issue could be related to a problem during the build process. Try rebuilding TensorFlow using the --config=monolithic flag to ensure that all libraries are included.

Check if there are any conflicts:
If you have multiple versions of TensorFlow installed, there may be conflicts between the different versions. Make sure that you are importing the correct version of TensorFlow.

Try installing TensorFlow using pip:
Instead of building TensorFlow from source, try installing it using pip. This should ensure that all the required libraries are included in the package.

Hopefully, one of these suggestions will help you resolve the issue. > @ProgrammerPeter Hi, Thanks for opening the issue,
> 
> It seems like the libtensorflow_cc.so.2 file is not being included in the generated TensorFlow wheel file. Here are a few suggestions to fix the issue:
> 
> Try building the TensorFlow package again with the following command:
> 
> ```shell
> bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
> ```
> 
> This command will build the TensorFlow package with support for CUDA-enabled GPUs. If you don't have a GPU, you can skip the --config=cuda flag.
> 
> Check the contents of the generated wheel file to see if the libtensorflow_cc.so.2 file is present. You can do this using the following command:
> 
> ```shell
> pip show tensorflow
> ```
> 
> This command will display information about the installed TensorFlow package, including its location on your system. Once you have the location of the package, you can navigate to the package directory and inspect its contents.
> 
> If the libtensorflow_cc.so.2 file is missing from the package, you can manually copy it into the appropriate directory. The directory structure of the TensorFlow package should be similar to the following:
> 
> ```shell
> tensorflow/
>   __init__.py
>   _api/
>     ...
>   _api_v1/
>     ...
>   _api.v2/
>     ...
>   ...
> ```
> 
> The libtensorflow_cc.so.2 file should be placed in the tensorflow directory alongside the other Python modules.
> 
> If none of the above solutions work, try building TensorFlow from source again, but this time with the following command:
> 
> ```shell
> bazel build --config=monolithic //tensorflow/tools/pip_package:build_pip_package
> ```
> 
> This command will build a monolithic TensorFlow package, which includes all of the necessary shared libraries.

Copying the libtensorflow_cc.so.2 file into installed TensorFlow package fixes my issue. I will try build a monolithic TensorFlow package later. Thanks for your quick reply! > It seems like the issue is related to the missing shared library libtensorflow_cc.so.2. This can happen if the library is not found in the expected location or if it is not included in the TensorFlow package.
> 
> Here are some things you could try to resolve the issue:
> 
> Check if the library is installed: Use the find command to search for the library in your system. If it is found, check if the path to the library is included in the LD_LIBRARY_PATH environment variable.
> 
> Try rebuilding TensorFlow: The issue could be related to a problem during the build process. Try rebuilding TensorFlow using the --config=monolithic flag to ensure that all libraries are included.
> 
> Check if there are any conflicts: If you have multiple versions of TensorFlow installed, there may be conflicts between the different versions. Make sure that you are importing the correct version of TensorFlow.
> 
> Try installing TensorFlow using pip: Instead of building TensorFlow from source, try installing it using pip. This should ensure that all the required libraries are included in the package.
> 
> Hopefully, one of these suggestions will help you resolve the issue.

The libtensorflow_cc.so.2 file can be found in path `tensorflow/bazel-bin/tensorflow`. So I think adding this path to LD_LIBRARY_PATH can work. I just don't understand why it wasn't packed into the whl file. I will try to build using the --config=monolithic flag later. Thanks for your kind help! Hi @ProgrammerPeter ,

Apologies for the delayed response. By default Tensorflow builds are not monolithic. Please refer to attached source file for same.

https://github.com/tensorflow/tensorflow/blob/f0b8b9a1457bcc8d83716b2034d3d6501693c7d0/.bazelrc#L98C1

To get a single shared file we need to use `--config=monolithic` flag. Could you please confirm whether you have tested the build with this flag ?

IMO, even with non monolithic builds the SO file also should be added to the wheel. Starting from TF2.13, tensorflow builds for Linux uses clang compiler inplace of GCC.Please refer attached [documentation](https://www.tensorflow.org/install/source#install_clang_recommended_linux_only). Can you try the build with clang 16.0 and let us know if that makes difference.

Thanks!
 This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. This issue was closed because it has been inactive for 7 days since being marked as stale. Please reopen if you'd like to work on this further. Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/60326">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/60326">No</a>
 I need to build a .whl that actually works, and "--config monolithic" fails due to issue #60052.

Why is the pip package missing libtensorflow_cc.so.2? r2.12 does not have this problem. Is it possible to reopen this issue? @plopresti ,

Reopened the issue as requested. COuld you confirm this is not an issue in TF2.12v ? Hi @SuryanarayanaY. Thank you for reopening.

As far as I can tell, this is not an issue in r2.12 nor on master. I am unsure what change on master fixes it however. I have reproduced this issue with v2.13.0 and found it is related to Bazel:
Bazel 5.1 through 5.4 don't have this issue (the library exists in the correct place), with Bazel 6.0 and 6.3 the library is missing.
I.e. after the `bazel build` command `bazel-bin/tensorflow/tools/pip_package/build_pip_package.runfiles/org_tensorflow/tensorflow/libtensorflow_cc.so.2` doesn't exist  @plopresti Is that the same for you or does it work for you in r2.12 and master with Bazel 6+? I traced this to this commit: https://github.com/bazelbuild/bazel/commit/26d882f92943587d39460320694b17865ea74f91

Description:
> [...] we would be adding the library itself to runfiles as well as the symlink created in the solib directory. Although harmless, it is unnecessary to add the original library [...]

The commit removes adding `precompiled_dynamic_library.resolved_symlink_dynamic_library`. Reverting the commit or replacing it by the [likely intended code](https://github.com/bazelbuild/bazel/pull/19378/commits/05687ed6e28e95ee576962c1e40b81c83610c8c5) fixes this.

I'm not sure why this causes the issue though as `libtensorflow_cc.so.2` is a symlink but only the resolved file ` libtensorflow_cc.so.2.13.0` is present. The following code part seems to be meant to include that file in the runfiles although I don't exactly understand how: https://github.com/tensorflow/tensorflow/blob/be88ba0aefb1b778c2c0d0d896a342a70579740c/tensorflow/tensorflow.bzl#L2415-L2420

It might also be a Bazel bug which confuses `resolved_symlink_dynamic_library` with `dynamic_library` in the properties but checking the sources I wasn't able to find how/where they are set. Possibly due to the symlink creation likely happening here: https://github.com/tensorflow/tensorflow/blob/41bd95012de90138d1cfad86e89347f7472df376/tensorflow/tensorflow.bzl#L913-L914
<details><summary>Click to expand!</summary> 
 
 ### Issue Type

Bug

### Have you reproduced the bug with TF nightly?

No

### Source

source

### Tensorflow Version

tf 2.13.0

### Custom Code

Yes

### OS Platform and Distribution

Linux

### Mobile device

_No response_

### Python version

3.9

### Bazel version

6.1.1

### GCC/Compiler version

9.5.0

### CUDA/cuDNN version

_No response_

### GPU model and memory

_No response_

### Current Behaviour?

I build the lastest TensorFlow code from source successfully with
`bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package`

Then I generate a TensorFlow whl successfully with
`./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg`

But when I pip install this whl, import tensorflow got "ImportError: libtensorflow_cc.so.2: cannot open shared object file: No such file or directory". 

I found the whl file generated is only 80.64M. But I think it should be about 200M.

But there has libtensorflow_cc.so.2 file under path tensorflow/bazel-bin/tensorflow. I don't know why it wasn't packed into the whl file.

### Standalone code to reproduce the issue

```shell
import tensorflow as tf
```


### Relevant log output

```shell
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/data1/envs/xtc3.9/lib/python3.9/site-packages/tensorflow/__init__.py", line 38, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "/data1/envs/xtc3.9/lib/python3.9/site-packages/tensorflow/python/__init__.py", line 36, in <module>
    from tensorflow.python import pywrap_tensorflow as _pywrap_tensorflow
  File "/data1/envs/xtc3.9/lib/python3.9/site-packages/tensorflow/python/pywrap_tensorflow.py", line 26, in <module>
    self_check.preload_check()
  File "/data1/envs/xtc3.9/lib/python3.9/site-packages/tensorflow/python/platform/self_check.py", line 63, in preload_check
    from tensorflow.python.platform import _pywrap_cpu_feature_guard
ImportError: libtensorflow_cc.so.2: cannot open shared object file: No such file or directory
```
</details>