千家信息网

Ubuntu20.04中怎么安装TensorFlow

发表于:2025-02-19 作者:千家信息网编辑
千家信息网最后更新 2025年02月19日,这期内容当中小编将会给大家带来有关Ubuntu20.04中怎么安装TensorFlow,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。1. pip安装TensorFl
千家信息网最后更新 2025年02月19日Ubuntu20.04中怎么安装TensorFlow

这期内容当中小编将会给大家带来有关Ubuntu20.04中怎么安装TensorFlow,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

1. pip安装TensorFlow

这里使用的是当前日期最新版的 Ubuntu20.04 作为操作系统平台。20.04默认安装了Python3.8版本,但是没有安装pip,所以可以使用apt先安装pip:

$ sudo apt install python3-pip -y

查看pip的版本,如果不是最新版本顺便将pip升级到最新版本:

$ pip3  -V$ pip3 install --upgrade pip

安装pip的方式很多,例如可以把pip下载到本地使用Python命令安装等。安装TensorFlow2需要使用高于19.0的pip版本

安装之前需要把pip更新到最新版:

$ pip3 install --upgrade pip

安装 virtualenv 来帮助我们创建虚拟环境,这里使用的是国内阿里云的镜像源,下载速度会快很多:

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

创建一个新的 Python 虚拟环境,创建一个 ./venv 目录来存放它:

$ virtualenv ./venv/

如果我们想把系统中已有的库也加入到虚拟环境中,可以加上--system-site-packages。指定Python版本适用-p参数,如:-p python3

激活该虚拟环境:

$ source venv/bin/activate

使用 pip 安装支持 GPU 的 TensorFlow 软件包,可以选择稳定版或预览版。安装稳定版的TensorFlow:

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

如果安装只支持CPU版本:pip install tensorflow-cpu;同理安装只支持GPU版本:pip install tensorflow-cpu。指定版本:pip install tensorflow-gpu==2.2.0

安装预览版使用下面的命令:

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

安装之前一定要 保证/tmp空闲足够充足,大概需要3G~4G,当然也可以通过命令临时更改 /tmp 容量:

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

查看已安装的所有包:

(venv) thanlon@thanlon:~$ pip list

测试不指定 CPU 和 GPU 版本的 TensorFlow 是否不区分 CPU 和 GPU,是否会支持GPU。测试发现确实支持GPU:

直接安装的TensorFlow,并没有指定GPU版本,却得到GPU的支持。实际上新版本的TensorFlow不指定GPU版本同样支持GPU。对于1.15及更早版本,CPU和GPU软件包是分开的。pip install tensorflow==1.15是CPU版本,pip install tensorflow-gpu==1.15是GPU版本。

2. Anaconda安装TensorFlow

Anaconda是一个开源的Python发行版本,其中包含了很多流行的用于科学计算、数据分析的Python包。可以从官网下载,官网地址:https://www.anaconda.com/products/individual

也可以到清华镜像站下载,官网链接:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/

我们这里直接从清华镜像站复制了Anaconda最新版本的链接,使用 wget 或者 axel 先下载到本地:

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

执行脚本安装Anaconda,安装过程中根据提示结合自己需要做例如选择安装路径、是否安装Visual Stadio Code的一些操作:

$ ./Anaconda3-5.3.1-Linux-x86_64.sh

Anaconda安装成功之后接下来就可以安装TensorFlow了,Anaconda默认使用国外的镜像源,在我们安装TensorFlow之前先更换为国内的镜像源,如更换为 中科大镜像源,第一个是最重要且主要的镜像源地址,第二个添不添加都可以:

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

更新conda:

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

conda比pip更加强大,可以安装非Python库。所以,非常推荐使用Anaconda安装TensorFlow!


创建并进入虚拟环境,离开虚拟环境使用 conda deactivate 命令:

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

安装 TensorFlow GPU 最新版本,当前最新版是 tensorflow-gpu-2.2.0,已知版本的情况下可以指定版本:

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

为了检查安装的 TensorFlow 是否是 GPU 版本,需要安装 jupyter notebook 写程序测试:

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

测试程序如下,说明我们已经成功安装支持 GPU 的TensorFlow:

至此,Anaconda安装TensorFlow已完成!

3. TensorFlow Docker容器的构建

首先安装Docker,Dockeer官方安装文档:https://docs.docker.com/engine/install/ubuntu/

安装方式有三种,我这里选择存储库安装。卸载旧版本的Docker,如果之前安装过:

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

设置Docker存储库:

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

添加Docker的官方GPG密钥:

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

设置稳定的存储:

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

更新apt程序包索引,并安装最新版本的Docker Engine和容器:

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

运行hello-world 映像来验证是否正确安装了Docker Engine:

$ sudo docker run -it hello-world

查看Docker是否启动:

$ systemctl status docker.service

至此Docker安装成功,下面开始安装TensorFlow。首先下载 TensorFlow Docker 镜像,TensorFlow 提供了很多ensorFlow Docker 镜像。官方 TensorFlow Docker 映像位于 tensorflow/tensorflow Docker Hub 代码库中:

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

更换下载源为国内的镜像源 Docker官方中国区:https://registry.docker-cn.com;阿里云: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"]}

这里就拉去一个支持 GPU 和 Jupyter 的版本:

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


成功安装:

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


查看本地已有的容器:

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

接下来运行 TAG 是 latest-gpu 的容器,进入容器后可以查看内置的 Python 版本以及安装好的 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...

可以另外开一个Terminal,查看正在运行的镜像:

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

运行 TAG 是 latest-gpu-jupyter 的容器:

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

浏览器访问下面的链接就可以使用 jupyter notebook:



上述就是小编为大家分享的Ubuntu20.04中怎么安装TensorFlow了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。

版本 镜像 支持 容器 环境 成功 命令 官方 测试 运行 最新版 程序 链接 分析 存储 更新 选择 接下来 内容 地址 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 在数据库中脚本里面添加实例 学生网络安全的作品 软件开发对电脑的要求高吗 上海探课网络技术有限公司 阿里旺旺网络技术有限公司 软件开发面临风险 沈阳视佳网络技术有限公司 科技的最高境界就是互联网吗 工业实时数据库方亚东 我的世界网易版服务器怎么加32k win10家庭版软件开发 沈阳大学毕业论文查重数据库 c 如何读任意行数据库 pg数据库执行计划怎么看 新闻周刊网络安全文字 棋牌服务器 js脚本 一般做软件开发能做到多少岁 邮政软件开发公司 安徽数据库日志审计参数 大庆app软件开发 服务器名字忘了怎么找回 建立用户联系方式数据库优点 数据库数据如何采集 美篇网络安全防范进社区 XX局网络安全要求 删除数据库次数据文件 英雄联盟手游国际服服务器炸了 手机 smb 服务器 服务器主机主板连接方法 重庆三维可视化人口管理软件开发
0