Build TensorFlow 2.3.1 from source on Ubuntu20.04 LTS and create a shared library in C ++ language

Trigger of things

One year has passed since the last article, and the information is out of date, so I updated it. Especially the number of PV does not increase: frowning2:

Change log

--2020/11/18: Reposted as Ubuntu20.04 LTS & TensorFlow 2.3.1. --Same day: Added because there were not enough commands in the library

PC used

cat /etc/lsb-release

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=20.04
DISTRIB_CODENAME=focal
DISTRIB_DESCRIPTION="Ubuntu 20.04.1 LTS"

gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Advance preparation

Subsequent commands are executed line by line from the top unless otherwise specified.

--Installation of prerequisite materials

sudo apt install -y build-essential 
sudo apt install -y libffi-dev
sudo apt install -y libssl-dev 
sudo apt install -y zlib1g-dev
sudo apt install -y liblzma-dev
sudo apt install -y libbz2-dev libreadline-dev libsqlite3-dev 
sudo apt install -y git
sudo apt install -y openjdk-11-jdk

--Installing pyenv

git clone https://github.com/pyenv/pyenv.git ~/.pyenv

#For bash people, ".zshrc "to".Change to "bashrc"
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
source ~/.zshrc

--Installing python 3.9.0

pyenv install 3.9.0
pyenv global 3.9.0
python -V # 3.9.OK if 0 appears

--Install virtualenv

pip install virtualenv
mkdir virtualEnv # (Any)
cd virtualEnv # (Any)
virtualenv py3.9.0 # 「py3.9."0" is an arbitrary character string
source py3.9.0/bin/activate # 「py3.9."0" is the above arbitrary string
#To get out of the environment, just type "deactivate"

Build preparation

--Install the package in python for build

#Load the virtualenv environment before executing

pip install -U pip numpy wheel

pip install -U keras_preprocessing --no-deps

--Installation of build tool bazel

sudo apt install curl gnupg
curl -fsSL https://bazel.build/bazel-release.pub.gpg | gpg --dearmor > bazel.gpg
sudo mv bazel.gpg /etc/apt/trusted.gpg.d/
echo "deb [arch=amd64] https://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
sudo apt update
sudo apt install -y bazel-3.1.0
sudo ln -s /usr/bin/bazel-3.1.0 /usr/bin/bazel
bazel --version  # 3.1.0

Build

git clone --recursive https://github.com/tensorflow/tensorflow.git
git checkout v2.3.1
./configure 
#Yes or No format is all No
# [Default...]Directory selection is the first python directory(This time virtualEnv)Only check carefully, and if not, specify the correct directory. Otherwise, press the enter key.

#Load the virtualenv environment before executing
bazel build --verbose_failures --config=v2 //tensorflow:libtensorflow_cc.so
bazel build --verbose_failures --config=v2 //tensorflow:libtensorflow_framework.so

Library preparation

--C ++ project directory hierarchy

[Project Folder]<-This time "tf"
  |-include/
  |   |- tensorflow/
  |-lib/
  |-CMakeLists.txt
  |-main.cpp

--Copy from tensorflow

cp bazel-bin/tensorflow/libtensorflow_cc.so ~/CLionProjects/tf/lib
cp bazel-bin/tensorflow/libtensorflow_cc.so.2 ~/CLionProjects/tf/lib
cp bazel-bin/tensorflow/libtensorflow_cc.so.2.3.1 cp -r third_party ~/CLionProjects/tf/include
cp bazel-bin/tensorflow/libtensorflow_framework.so ~/CLionProjects/tf/lib
cp bazel-bin/tensorflow/libtensorflow_framework.so.2 ~/CLionProjects/tf/lib
cp bazel-bin/tensorflow/libtensorflow_framework.so.2.3.1 ~/CLionProjects/tf/lib
cp -r tensorflow/c ~/CLionProjects/tf/include/tensorflow
cp -r tensorflow/cc ~/CLionProjects/tf/include/tensorflow
cp -r tensorflow/core ~/CLionProjects/tf/include/tensorflow
cp -r third_party ~/CLionProjects/tf/include                                   
cp -r bazel-tensorflow/external/eigen_archive/Eigen ~/CLionProjects/tf/include
cp -r bazel-tensorflow/external/eigen_archive/unsupported ~/CLionProjects/tf/include
cp -r third_party ~/CLionProjects/tf/include                                   

Operation check

CmakeLists.txt


cmake_minimum_required(VERSION 3.17)
project(tf)

set(CMAKE_CXX_STANDARD 20)

link_directories(lib) <-
include_directories(include) <-

add_executable(tf main.cpp)

target_link_libraries(tf tensorflow_cc tensorflow_framework) <-

main.cpp


#include <iostream>
#include "tensorflow/c/c_api.h"

int main() {
    printf("TensorFlow Ver. %s\n", TF_Version());
    return 0;
}

output


TensorFlow Ver. 2.3.1

Addendum Addendum Impressions The fact that it could be executed on Ubuntu means that there is no problem on Unix, so it should be possible on CentOS and macOS. (I don't know) Reference material "cmake cheat sheet" https://www.qoosky.io/techs/814fda555d)

"Compile and install tensorflow from source" https://memo.saitodev.com/home/tensorflow/build/

「tensorflow source build」 https://www.tensorflow.org/install/source

「bazel」 https://docs.bazel.build/versions/master/install-ubuntu.html

"The story of installing pyenv on ubuntu 20.04 [Updated on 2020/07/18]" https://qiita.com/neruoneru/items/1107bcdca7fa43de673d

Recommended Posts

Build TensorFlow 2.3.1 from source on Ubuntu20.04 LTS and create a shared library in C ++ language
Create a development environment for Ruby 3.0.0 and Rails 6.1.0 on Ubuntu 20.04.1 LTS
Build a DHCP and NAT router on Ubuntu 16.04
Install Ubuntu 20.04 in virtual box on windows10 and build a development environment using docker
How to install the language used in Ubuntu and how to build the environment
I tried to generate a C language program source from cURL
Build openssl, apache2.4, php7.4 from source on Ubuntu-server20.04.
Publish MySQL externally and log in on Ubuntu
Protobuf and gRPC C ++ environment construction on Ubuntu 18.04
I made a Ruby extension library in C
How to build a Pytorch environment on Ubuntu