Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install TensorFlow in Ubuntu20.04

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about how to install TensorFlow in Ubuntu20.04. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

1. Pip install TensorFlow

The latest version of Ubuntu20.04 on the current date is used as the operating system platform. 20.04 Python3.8 version is installed by default, but pip is not installed, so you can use apt to install pip first:

$sudo apt install python3-pip-y

Check the version of pip and upgrade pip to the latest version if it is not the latest version:

$pip3-V$ pip3 install-- upgrade pip

There are many ways to install pip, for example, you can download pip locally and install it using the Python command. A version of pip higher than 19.0 is required to install TensorFlow2

You need to update pip to the latest version before installation:

$pip3 install-upgrade pip

Install virtualenv to help us create a virtual environment. The domestic Aliyun image source is used here, and the download speed will be much faster:

$pip3 install-U virtualenv-I https://mirrors.aliyun.com/pypi/simple

Create a new Python virtual environment and create a. / venv directory to hold it:

$virtualenv. / venv/

If we want to add the existing libraries in the system to the virtual environment, we can add-- system-site-packages. Specifies that the Python version applies to the-p parameter, such as-p python3

Activate the virtual environment:

$source venv/bin/activate

Use pip to install TensorFlow packages that support GPU, and you can choose a stable version or a preview version. Install the stable version of TensorFlow:

(venv) thanlon@thanlon:~$ pip install tensorflow-I https://mirrors.aliyun.com/pypi/simple/

If the installation only supports the CPU version: the pip install tensorflow-cpu; installation only supports the GPU version: pip install tensorflow-cpu. Specified version: pip install tensorflow-gpu==2.2.0

Install the preview version using the following command:

(venv) thanlon@thanlon:~$ pip install tf-nightly-I https://mirrors.aliyun.com/pypi/simple/

Be sure to make sure that / tmp is free enough before installation, which probably requires 3G~4G. Of course, you can also change the capacity of / tmp temporarily by command:

(venv) thanlon@thanlon:~$ sudo mount-t tmpfs-o size=4G / tmp (venv) thanlon@thanlon:~$ df-h/dev/sda10 4.0G 0 4.0G 0 0 / tmp

View all installed packages:

(venv) thanlon@thanlon:~$ pip list

Test whether TensorFlow that does not specify CPU and GPU versions does not distinguish between CPU and GPU and will support GPU. The test found that GPU is indeed supported:

Direct installation of TensorFlow, which does not specify a GPU version, is supported by GPU. In fact, the new version of TensorFlow does not specify that the GPU version also supports GPU. For version 1.15 and earlier, the CPU and GPU packages are separate. Pip install tensorflow==1.15 is the CPU version and pip install tensorflow-gpu==1.15 is the GPU version.

2. Anaconda install TensorFlow

Anaconda is an open source Python distribution that contains many popular Python packages for scientific computing and data analysis. It can be downloaded from the official website at https://www.anaconda.com/products/individual

You can also download it from Tsinghua Mirror Station. The official website is https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/.

We have copied the link to the latest version of Anaconda directly from Tsinghua Mirror Station and downloaded it locally using wget or axel:

$axel https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-5.3.1-Linux-x86_64.sh

Execute the script to install Anaconda, and do some actions such as selecting the installation path and whether to install Visual Stadio Code according to the prompts during the installation process:

. / Anaconda3-5.3.1-Linux-x86_64.sh

After the Anaconda installation is successful, you can install TensorFlow. By default, Anaconda uses a foreign image source. Before we install TensorFlow, change it to a domestic image source. For example, if you change it to the image source of China University of Science and Technology, the first is the most important and major image source address. You can add the second one or not:

Thanlon@thanlon:~$ conda config-add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/mainthanlon@thanlon:~$ conda config-add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free

Update conda:

Thanlon@thanlon:~$ conda update-n base-c defaults conda

Conda is more powerful than pip and can install non-Python libraries. Therefore, it is highly recommended to install TensorFlow using Anaconda!

Create and enter the virtual environment, leave the virtual environment and use the conda deactivate command:

Thanlon@thanlon:~$ conda create-n venv python=3.8thanlon@thanlon:~$ conda activate venv (venv) thanlon@thanlon:~$ conda deactivate

Install the latest version of TensorFlow GPU. The latest version is tensorflow-gpu-2.2.0. If you know the version, you can specify the version:

(venv) thanlon@thanlon:~$ conda install tensorflow-gpu

To check whether the installed TensorFlow is the GPU version, you need to install the jupyter notebook writer test:

(venv) thanlon@thanlon:~$ conda install jupyter notebook (venv) thanlon@thanlon:~$ jupyter notebook

The test procedure is as follows, which indicates that we have successfully installed TensorFlow that supports GPU:

At this point, the Anaconda installation TensorFlow is complete!

3. Construction of TensorFlow Docker container

First install Docker,Dockeer official installation document: https://docs.docker.com/engine/install/ubuntu/

There are three ways to install, I choose repository installation here. Uninstall the older version of Docker, if previously installed:

$sudo apt-get remove docker docker-engine docker.io containerd runc

Set up the Docker repository:

$sudo apt-get install\ apt-transport-https\ ca-certificates\ curl\ gnupg-agent\ software-properties-common

Add the official GPG key for Docker:

$curl-fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add-

Set up stable storage:

$sudo add-apt-repository\ "deb [arch=amd64] https://download.docker.com/linux/ubuntu\ $(lsb_release-cs)\ stable"

Update the apt package index and install the latest version of Docker Engine and containers:

$sudo apt-get update & & sudo apt-get install docker-ce docker-ce-cli containerd.io

Run the hello-world image to verify that Docker Engine is installed correctly:

$sudo docker run-it hello-world

Check to see if Docker is started:

$systemctl status docker.service

At this point, the Docker installation is successful, so let's start installing TensorFlow. First download the TensorFlow Docker image. TensorFlow provides a lot of ensorFlow Docker images. The official TensorFlow Docker image is located in the tensorflow/tensorflow Docker Hub code base:

References: https://tensorflow.google.cn/install/docker#gpu_support

Change the download source to the domestic mirror source Docker official China region: https://registry.docker-cn.com; Aliyun: https://pee6w651.mirror.aliyuncs.com:

$sudo vim / etc/docker/daemon.json$ systemctl restart docker.service $cat / etc/docker/daemon.json {"registry-mirrors": ["https://pee6w651.mirror.aliyuncs.com"]}

Here is a version that supports GPU and Jupyter:

$docker pull tensorflow/tensorflow:latest-gpu-jupyter # latest release w / GPU support and Jupyter

Successful installation:

Thanlon@thanlon:~$ sudo docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEtensorflow/tensorflow latest-gpu-jupyter 8d78dd1e1b64 8 weeks ago 3.99GB

View existing containers locally:

Thanlon@thanlon:~$ sudo docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEtensorflow/tensorflow latest-gpu-jupyter 8d78dd1e1b64 8 weeks ago 3.99GBtensorflow/tensorflow latest-gpu f5ba7a196d56 8 weeks ago 3.84GB

Next, run TAG as a container for latest-gpu. After entering the container, you can view the built-in Python version and the installed TensorFlow:

Thanlon@thanlon:~$ sudo docker run-it tensorflow/tensorflow:latest-gpuroot@c4fceafad48c:/# python-VPython 3.6.9root@c4fceafad48c:/# pip list...tensorflow-gpu 2.2.0...

You can open another Terminal to view the running image:

Thanlon@thanlon:~$ sudo docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES49b31ca53c49 tensorflow/tensorflow:latest-gpu "/ bin/bash" 17 seconds ago Up 16 seconds gallant_shirley

Running TAG is the container for latest-gpu-jupyter:

Thanlon@thanlon:~$ sudo docker run-p 8888 8888 tensorflow/tensorflow:latest-gpu-jupyter

Browsers can use jupyter notebook by visiting the following link:

The above is how to install TensorFlow in the Ubuntu20.04 shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report