虚拟机 Ubuntu 安装 Tensorflow
本文最后更新于:1 年前
1 软件环境
1.1 ubuntu-18.04-desktop-amd64.iso
1.2 python3.7.10
2 安装 ubuntu
2.1 由于存在简易安装,所以基本上都是傻瓜式操作
2.2 配置国内源
国内源
# 阿里云源 deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse ##測試版源 deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse # 源碼 deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse ##測試版源 deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse # 清华大学源 deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse ##測試版源 deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse # 源碼 deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse ##測試版源 deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
Ubuntu 的源存放在在 /etc/apt/ 目录下的 sources.list 文件中,修改前我们先备份,在终端中执行以下命令:
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bcakup
然后执行下面的命令打开 sources.list 文件,清空里面的内容,把上面阿里云与清华大学的 Ubuntu 源复制进去,保存后退出。
sudo gedit /etc/apt/sources.list
接着在终端上执行以下命令更新软件列表,检测出可以更新的软件:
sudo apt-get update
最后在终端上执行以下命令进行软件更新:
sudo apt-get upgrade
3 安装 python
ubuntu 本身带有python 2.7 和 python 3.6,如果没有可以执行以下命令
sudo apt-get update sudo apt-get install python2.7 sudo apt-get update sudo apt-get install python3.6
4 切换 python 版本
我们可以使用 update-alternatives 来为整个系统更改Python 版本。以 root 身份登录,首先罗列出所有可用的python 替代版本信息:
update-alternatives --list python update-alternatives: error: no alternatives for python
如果出现以上所示的错误信息,则表示 Python 的替代版本尚未被update-alternatives 命令识别。想解决这个问题,我们需要更新一下替代列表,将python2.7 和 python3.6放入其中。
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1 sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 2
–install 选项使用了多个参数用于创建符号链接。最后一个参数指定了此选项的优先级,如果我们没有手动来设置替代选项,那么具有最高优先 级的选项就会被选中。这个例子中,我们为/usr/bin/python3.6 设置的优先级为2,所以update-alternatives 命 令会自动将它设置为默认 Python 版本。
python --version Python 3.6.9
接下来,我们再次列出可用的 Python 替代版本。
update-alternatives --list python /usr/bin/python2.7 /usr/bin/python3.6
现在开始,我们就可以使用下方的命令随时在列出的 Python 替代版本中任意切换了。(这一步是最关键的)
update-alternatives --config python
以后我们就可以用 python 代替 python3
5 设置pip
5.1 安装pip3
由于 ubuntu pip有问题,在导mian包有问题,网上的教程也没用,所以使用pip3
sudo apt-get install python3-pip
查看pip3版本
pip3 --version pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)
升级 pip 为最新版本,不然下载包的会出问题
sudo pip3 install --upgrade pip
root@pncalbl-pc:/home/pncalbl# pip -V pip 21.0.1 from /usr/local/lib/python3.6/dist-packages/pip (python 3.6) root@pncalbl-pc:/home/pncalbl# pip3 -V pip 21.0.1 from /usr/local/lib/python3.6/dist-packages/pip (python 3.6)
如果系统只安装了一个 pip3, 可以通pip来直接使用 pip3,而不是 pip3
5.2 更换pip源
创建目录:
sudo mkdir ~/.pip
创建文件:
sudo gedit ~/.pip/pip.conf
将以下内容保存到文件中,建议使用清华的源
[global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple [install] trusted-host=pypi.tuna.tsinghua.edu.cn
5.3 pip install
可能出现的问题
hostname
可能是配置了系统代理,此时我们可以暂时关闭代理
6 安装 Tensorflow
这里使用 douban 的源,是因为他下载 tensorflow 比较快,不影响我们之前配pip源
由于之前已经配好了 pip 的源,所以只需要进行下载即可
pip install tensorflow-cpu==2.4.0 -i https://pypi.douban.com/simple/
安装中可能会有1~2个包,安装失败,我们可以手动安装
python -m pip install launchpadlib
继续安装 matplotlib 和 notebook(matplotlib是Python 的绘图库,jupyter notebook是基于网页的用于交互计算的应用程序)
pip install matplotlib notebook -i https://pypi.douban.com/simple/
在安装 notebook中,会出现一些问题
Running as root is not recommended. Use –allow-root to bypass.首先查看配置文件位置,
jupyter notebook --generate-config --allow-root
接下来打开配置文件:
gedit /root/.jupyter/jupyter_notebook_config.py
找到”#c.NotebookApp.allow_root = False “;去掉#,并修改成True即可解决root权限运行的问题
c.NotebookApp.allow_root =True jupyter notebook # 保存,重新运行程序:
设置访问密码, 打开 ipython 输入
from notebook.auth import passwd passwd() 然后根据提示输入2次密码 Enter password: ········ Verify password: ········
ipthon需要自己安装
apt-get install ipython3
复制密码,粘贴至配置文件(记得去掉 #)
c.NotebookApp.password = u'sha1:f5*****************************'
更多设置
c.NotebookApp.ip = ‘localhost’ c.NotebookApp.open_browser = True(True:启动时自动打开浏览器,False:需手动打开浏览器访问http://localhost:8888/tree) c.NotebookApp.port = 8888(端口设置)
7 测试 Tensorflow
安装成功后,执行jupyter notebook命令进入网页界面,界面如下:
新建1个界面,可以输入如下来验证,如下图:
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!