python环境搭建

1. 安装 Ubuntu 16.04

修改 BIOS:HP 电脑 F10 进BIOS,“禁用” 安全引导模式,“启用” 旧引导模式。

U盘启动:开机 按 F9,根据提示选择 Ubuntu 16.04 安装U盘启动。

系统分区:根据实际PC存储磁盘情况进行分区: \boot, , \srv, swap, for bios 等

语言选择: English

1.1 apt-get 安装

# 更新安装包$ sudo apt-get update# 安装 ssh-server$ sudo apt-get install openssh-server# 支持 exFat 移动硬盘$ sudo apt-get install exfat-utils# 安装pip3$ sudo apt-get install python3-pip# 安装 ffmpeg$ sudo apt-get install ffmpeg# 安装 cmake$ sudo apt-get install cmake# 安装 git $ sudo apt-get install git# 安装 JRE,sshpass (ssh 隧道用)$ sudo apt-get install openjdk-8-jre$ sudo apt-get install sshpass

1.2 安装 NVIDIA 显卡驱动

关闭图形界面再安装驱动,命令为:

$ sudo service lightdm stop#或者$ sudo/etc/init.d/lightdm stop

其他显卡驱动相关,可参考:https://www.jianshu.com/p/a2ed3764e2b8

显卡驱动冲突可能造成 循环登陆问题,解决办法参考:https://www.jianshu.com/p/a2ed3764e2b8

检测需要的显卡驱动版本(CentOS)

# 安装gcc$ yum-y install gcc-c++# 添加 EIRepo 源$ sudo rpm--importhttps://www.elrepo.org/RPM-GPG-KEY-elrepo.org$ sudo rpm-Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm# 安装显卡驱动检测$ sudo yum install nvidia-detect# 运行$ nvidia-detect-v  Probingforsupported NVIDIA devices...[10de:1b06]NVIDIA Corporation GP102[GeForce GTX1080Ti]This device requires the current410.78NVIDIA driver kmod-nvidia

这里提示你需要的显卡驱动版本410.78。

Ubuntu 系统,尝试过安装 yum,再装nvidia-detect,但是失败了。

参考:https://blog.csdn.net/fox_wayen/article/details/78544449

ubuntu 安装yum需要特别谨慎,容易造成系统冲突

驱动下载

[Geforce官网]

[NVIDIA 官网]

python环境搭建_第1张图片

安装对应内核版本的kernel-devel

安装驱动需要制定你当前linux内核的对应的kernel-devel。如果找不到,显卡驱动会报错。

# 1. 查看当前内核版本$ uname-r# 2. 查看已经安装的内核版本$ uname-a;rpm-qa kernel\*|sort# 3. 安装当前内核版本$ sudo yum install"kernel-devel-$(uname -r)"# 注意:如果报错,说找不到,例如: "kernel-devel-3.10.0-957.1.3.el7.x86_64"  #      你需要自己去搜索这个rpm包,下载到当前目录,然后再安装。#      完成后在/usr/src/kernels/下生成对应版本的源码$ ls-al/usr/src/kernels  drwxr-xr-x22root root4096Dec2618:553.10.0-957.1.3.el7.x86_64

安装显卡驱动

$ wget https://us.download.nvidia.com/XFree86/Linux-x86_64/410.78/NVIDIA-Linux-x86_64-410.78.run$ bash NVIDIA-Linux-x86_64-410.78.run--kernel-source-path=/usr/src/kernels/3.10.0-957.1.3.el7.x86_64

检查显卡驱动是否安装成功

$ nvidia-smiThuDec2718:42:112018+-----------------------------------------------------------------------------+|NVIDIA-SMI410.78DriverVersion:410.78CUDAVersion:10.0||-------------------------------+----------------------+----------------------+|GPUNamePersistence-M|Bus-IdDisp.A|VolatileUncorr.ECC||FanTempPerfPwr:Usage/Cap|Memory-Usage|GPU-UtilComputeM.||===============================+======================+======================||0GeForceGTX108...Off|00000000:2D:00.0Off|N/A||17%29CP058W/250W|0MiB/11178MiB|0%Default|+-------------------------------+----------------------+----------------------++-----------------------------------------------------------------------------+|Processes:GPUMemory||GPUPIDTypeProcessnameUsage||=============================================================================||Norunning processes found|+-----------------------------------------------------------------------------+

1.3 安装CUDA

请在不启动图形界面的情况下,安装CUDA以及NVIDIA显卡驱动。

退出和启动图形界面,参考:https://www.jianshu.com/p/a2ed3764e2b8

下载CUDA

注:Tensorflow 1.7 需要用CUDA 9.0 版本。

https://developer.nvidia.com/compute/cuda/9.0/Prod/local_installers/cuda_9.0.176_384.81_linux-run

https://developer.nvidia.com/compute/cuda/9.0/Prod/patches/1/cuda_9.0.176.1_linux-run

https://developer.nvidia.com/compute/cuda/9.0/Prod/patches/2/cuda_9.0.176.2_linux-run

https://developer.nvidia.com/compute/cuda/9.0/Prod/patches/3/cuda_9.0.176.3_linux-run

https://developer.nvidia.com/compute/cuda/9.0/Prod/patches/4/cuda_9.0.176.4_linux-run

[选择其他需要的 CUDA 版本下载]

选择 runfile 安装CUDA:

我起初选择rpm下载安装,失败了。报的错误是显卡驱动冲突(机器之前有人装过NVIDIA显卡驱动,有版本冲突问题)。我后来用 runfile,成功了。特别注意:runfile可以干掉原先的NVIDIA显卡驱动。

need-to-insert-img

$ sudo bash cuda_9.0.176_384.81_linux.run

注意:安装过程中,选择不重新安装显卡驱动。

CUDA 环境变量配置

PATH=$PATH:$HOME/bin:/usr/local/cuda/binLD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64/CUDA_HOME=/usr/local/cudaexport PATHexport LD_LIBRARY_PATHexport CUDA_HOME

检查cuda是否可用

$ cd/usr/local/cuda-9.0/samples/1_Utilities/deviceQuery$ sudo make$./deviceQuery./deviceQuery Starting...CUDADeviceQuery(RuntimeAPI)version(CUDARTstaticlinking)Detected8CUDACapabledevice(s)Device0:"GeForce GTX 1080 Ti"CUDADriverVersion/RuntimeVersion10.0/9.0CUDACapabilityMajor/Minorversion number:6.1Totalamount ofglobalmemory:11178.........>PeeraccessfromGeForceGTX1080Ti(GPU7)->GeForceGTX1080Ti(GPU3):Yes>PeeraccessfromGeForceGTX1080Ti(GPU7)->GeForceGTX1080Ti(GPU4):Yes>PeeraccessfromGeForceGTX1080Ti(GPU7)->GeForceGTX1080Ti(GPU5):Yes>PeeraccessfromGeForceGTX1080Ti(GPU7)->GeForceGTX1080Ti(GPU6):YesdeviceQuery,CUDADriver=CUDART,CUDADriverVersion=10.0,CUDARuntimeVersion=9.0,NumDevs=8Result=PASS

查看 nvcc 版本

$ nvcc--versionnvcc:NVIDIA(R)Cudacompiler driverCopyright(c)2005-2017NVIDIACorporationBuilton Fri_Sep__1_21:08:03_CDT_2017Cudacompilation tools,release9.0,V9.0.176

1.4 安装CUDNN

Tensorflow 1.7 要求 CUDA9.0  要求 CUDNN 7.1

注:Nvidia 会要求注册登陆才能下载,我用我的qq邮箱注册登陆的。

我有过CUDNN官网登陆不上的经历,下载不易,建议保存。

need-to-insert-img

[CUDNN 官网]下载前需要先注册

下载得到:cudnn-9.0-linux-x64-v7.1.tgz

直接解压到 /usr/local/cuda

主要是两个目录,include 和 lib64

$ cd/usr/local$ sudo tar-xvf~/Download/cudnn-9.0-linux-x64-v7.1.tgz

1.5 安装 ffmpeg

优先使用 $ sudo apt-get install ffmpeg

手动安装 真是一个比较痛苦的过程,我就不写了

请参考:https://blog.csdn.net/u010782875/article/details/80679510

安装包下载地址:https://download.csdn.net/download/u010782875/10476579

need-to-insert-img

ffmpeg-4.0.2.tar.bz2

lame-3.100.tar.gz

last_x264.tar.bz2

libdca-0.0.6.tar.bz2

libogg-1.3.3.tar.gz

libvorbis-1.3.3.tar.gz

nasm-2.13.03.tar.xz

xvidcore-1.3.5.tar.gz

装 ffmpeg 参数

$ ./configure --enable-shared --prefix=/usr/local/ffmpeg --enable-libmp3lame --enable-gpl --enable-version3 --enable-nonfree --enable-pthreads --enable-postproc --enable-ffserver --enable-ffplay --enable-libx264

$ make

$ make install

配置环境变量:

export PATH=/usr/local/ffmpeg/bin:$PATH

配置动态链接库

sudo vi /etc/ld.so.conf

...

# add

/usr/local/ffmpeg/lib

2. 安装开发环境

2.1. 搭建python 虚拟环境 virtualenv

参考文章:[python三大神器之virtualenv]

虚拟环境有很多好处,例如:

* 想用python3,可以单独建一个python3的环境。不用每次 python、pip 都非要跟着一个小尾巴 3。

* pip install时,省去很多sudo。

建议:root账号建立所有虚拟环境,统一放在/env

$ sudo su$ pip3 install virtualenv$ mkdir/env$ cd/env$ virtualenv RetailX# $ virtualenv -p /anaconda3/bin/python RetailX# 启动虚拟环境$ source/env/RetailX/bin/activate# 取消虚拟环境$ deactivate

添加 CUDA 环境变量配置

$ vi /env/RetailX/bin/activate...# 添加 CUDA 环境变量配置PATH=$PATH:$HOME/bin:/usr/local/cuda/binLD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64/:/usr/local/tensorrt/lib/CUDA_HOME=/usr/local/cudaexport PATHexport LD_LIBRARY_PATHexport CUDA_HOME

设置自动启动虚拟环境

$ vi~/.bashrc# Addsource/env/RetailX/bin/activate

2.2 pip 换源

新版ubuntu要求使用https源,要注意。

清华:https://pypi.tuna.tsinghua.edu.cn/simple

阿里云:http://mirrors.aliyun.com/pypi/simple/

中国科技大学https://pypi.mirrors.ustc.edu.cn/simple/

华中理工大学:http://pypi.hustunique.com/

山东理工大学:http://pypi.sdutlinux.org/

豆瓣:http://pypi.douban.com/simple/

参考:https://www.cnblogs.com/microman/p/6107879.html

永久修改,一劳永逸:

修改 ~/.pip/pip.conf

[global]index-url=https://pypi.tuna.tsinghua.edu.cn/simple[install]trusted-host=mirrors.aliyun.com

2.3 虚拟环境下 pip 安装python开发环境

重要:虚拟环境 pip 安装,必须先 sudo su 到root下,不能使用sudo pip。换源也是给root换源(/root/.bashrc)。

(RetailX) pi@cue-110077:~$ sudo su(RetailX) root@cue-110077:/home/pi# pip install XXXX

安装 TensorFlow 1.7

$ pip install tensorflow-gpu==1.7检测是否安装成功$ python>>>import tensorflow as tf>>>tf.__version__'1.7.0'>>>tf.__path__['/env/RetailX/lib/python3.6/site-packages/tensorflow']# 不报错

安装OpenCV

$ pip install opencv-python

安装其他

pip install minpypip install pillowpip install keras -U --prepip install filterpypip install progressbarpip install urllib3pip install pycudapip install onnxpip install cryptopip install libsm6pip install kafka-pythonpip install opencv-python

3 其他安装

3.1 Dlib (GPU版)

记得要在自己的 虚拟 python 环境下安装。

下载DLIB:http://dlib.net/files/dlib-19.16.tar.bz2

$ wget http://dlib.net/files/dlib-19.16.tar.bz2$ tar xvf dlib-19.16.tar.bz2$ cd dlib-19.16$ apt-get install cmake$ python setup.py install  --yes DLIB_USE_CUDA

3.2 SRS 搭建

安装 SRS 参考https://github.com/ossrs/srs

修改 RTMP配置:/usr/local/srs/conf/rtmp.conf

#very RTMP# @see https://github.com/ossrs/srs/wiki/v1_CN_SampleRTMP# @see full.conf for detail config.listen1935;max_connections1000;srs_log_tank        file;srs_log_file./objs/srs.log;http_api{enabled        on;listen1985;}http_server{enabled        on;listen80;dir./objs/nginx/html;}stats{network0;disk            sda sdb xvda xvdb;}vhost __defaultVhost__{min_latency    on;mr{enabled    off;#latency    350;}mw_latency100;#enabled        on;gop_cache      off;queue_length10;http_remux{enabled    on;mount[vhost]/[app]/[stream].flv;hstrs  on;}}

运行 SRS 服务

$ cd /usr/local/srs/$ ./objs/srs -c conf/rtmp.conf

PC 启动自动运行

$ vi/etc/rc.local# Addcd/usr/local/srs/./objs/srs-c conf/rtmp.conf

3.3 RetailX-Agent 搭建

ubuntu16.04修改网卡名称enp2s0为eth0我们会在这个网卡上去扫描摄像头

参考:https://blog.csdn.net/wenwenxiong/article/details/52937539

安装 JRE,sshpass (ssh 隧道用)

$ sudo apt-get install openjdk-8-jre$ sudo apt-get install sshpass

安装 RetailX-Agent

$ sudo su$ cp -r /home/pi/Downloads/camagent/ /srv/$ cp -r /home/pi/Downloads/agstream/ /srv/

RetailX-Agent 自动升级程序

# 启动自动升级$ sudo bash /srv/camagent/autoupdate.sh

启动 RetailX-Agent

$ sudo bash /srv/camagent/autoupdate.sh

定时任务

# 定时任务,启动监控程序。$ vi/var/spool/cron/crontabs/root# 添加 监控程序*/2****/usr/bin/python/srv/camagent/daemon.py>>/srv/camagent/logs/daemon.log*/2****/usr/bin/python/srv/camagent/autoupdate.py>>/srv/camagent/logs/autoupdate.log# 自动删除日志20*/1***find/srv/camagent/logs/-typef-mtime+15|xargs rm-rf# 查看定时任务$  ps-ef|grep javaroot1380104月16?00:00:00/usr/bin/sudo java-jar/srv/camagent/camagent-1.2.2.jarroot1381104月16?00:00:00/usr/bin/sudo java-jar/srv/camagent/autoupdatejar-1.0.0.jarroot1382138044月16?00:56:16java-jar/srv/camagent/camagent-1.2.2.jarroot1383138104月16?00:01:36java-jar/srv/camagent/autoupdatejar-1.0.0.jar

3.4 TensorRT 安装

安装TensorRT 到 /usr/local/tensorrt 目录下

$ cp -r ~/Download/tensorrt/ /usr/local/$ pip install /usr/local/tensorrt/python/tensorrt-5.0.2.6-py2.py3-none-any.whl

增加环境变量

$ vi/env/RetailX/bin/activate# addPATH=$PATH:$HOME/bin:/usr/local/cuda/binLD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64/:/usr/local/tensorrt/lib/CUDA_HOME=/usr/local/cudaexport PATHexport LD_LIBRARY_PATHexport CUDA_HOME


链接:https://www.jianshu.com/p/75805fe398aa

你可能感兴趣的:(python环境搭建)