ubuntu安装python3.5+pycharm+anaconda+opencv+docker+nvidia-docker+tensorflow+pytorch+Cmake3.8

本文详细介绍了如何在Ubuntu系统上进行一系列的环境搭建与配置,包括但不限于Python版本切换至3.5、PyCharm和Anaconda安装、Docker及nvidia-docker安装配置、TensorFlow与PyTorch安装等,并提供了CMake3.8的安装步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一,切换python版本为3.5

装好ubuntu,python版本是2.7的

我自己安装并更改打开为python3.5

sudo apt-get install python3.5

设置优先级和默认环境:

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150

切换版本:

update-alternatives --config python

可看见python已经为3.5了。

二,安装pycharm:

pycharm官网

 

三,安装anaconda

下载python3.5对应的版本

https://repo.anaconda.com/archive/

然后:

~$ cd 下载
~/下载$ bash Anaconda2-4.2.0-Linux-x86_64.sh

这里需要一直按Enter,直到出现需要输入yes or no 。输入yes后,会显示Anaconda将要安装到哪个目录下,可以自行修改,这里我选择默认的路径,直接回车。最后会提示添加环境变量,输入yes后,安装完成。

pycharm中更改解释器路径。

ok,,结束。

在~/.bashrc添加

export PATH="/home/fzh/anaconda2/bin:$PATH"(注意py3是anaconda3)

在source一下 

创建虚拟环境

conda create -n your_env_name python=x.x

激活进入

source activate your_env_name 

查看已有的虚拟环境

conda-env list

删除环境

conda remove -n conda_name --all

卸载anaconda

首先直接删除整个anaconda文件夹,在vim ~/.bashrc

注释掉#  export PATH="/home/fzh/anaconda2/bin:$PATH"

最后在source  ~/.bashrc

重启终端

更换conda源

将以上配置文件写在~/.condarc中
vim ~/.condarc

channels:
  - https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
  - https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  - defaults
show_channel_urls: true

安装opencv

pip install opencv-contrib-python

pip install opencv-python

四,安装docker

修改docker硬盘路径

docker/nvidia-docker 操作指南

https://www.dongliwu.com/archives/61/#directory0824639160743786413

若先前有安装docker需要先卸载(若没有安装过则无需执行),执行命令: 
sudo apt-get remove docker docker-engine docker.io 
Docker的安装有多个方式,这里以最常见的方式为例。首先依次执行以下命令,把docker仓库加进到apt里.

sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88
sudo add-apt-repository  "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
正式安装docker: 
sudo apt-get update 
sudo apt-get install docker-ce 
apt-cache madison docker-ce 
sudo docker run hello-world 
最后一个命令是验证docker是否安装成功,它会下载并执行hello-world镜像。如果安装正确,应该可以正确执行。如果提示找不到,多重复几次即可.

五,安装nvidia-docker

装好记得重启,命令如下.

sudo systemctl daemon-reload
sudo systemctl restart docker

这里有一个小bug,如果上述操作还会报 Error response from daemon: Unknown runtime specified nvidia.

通过 vim /usr/bin/nvidia-docker

修改为如下,重启docker ,在改回去,在重启即可解决问题。

我利用有驱动的官方镜像安装其他环境,首先把docker跑起来

sudo nvidia-docker run -it  nvidia/cuda:9.0-base

apt-get update

apt-get upgrade

接下来,完善此镜像。安装完整的cuda-toolkit:
官方的镜像不完整,这一步很关键

apt install cuda-toolkit-9-0

apt-get install python3

apt-get install python3-pip

然后按照这个链接去装cuda和cudnn即可.

https://blog.youkuaiyun.com/fanzonghao/article/details/89707519

想要更新原来的镜像,比如上述都安转好了,用commit命令,adaf25712ec8是原有镜像的id,chaos_gpu是新镜像的名字,用docker images即可查看新的镜像

commit后面是ps出来的容器ID,后面跟自己想要保存的名字,我这里是chaos_gpu

sudo nvidia-docker commit adaf25712ec8 chaos_gpu

保持镜像到本地:

https://www.runoob.com/docker/docker-save-command.html

将镜像 runoob/ubuntu:v3 生成 my_ubuntu_v3.tar 文档

docker save -o my_ubuntu_v3.tar runoob/ubuntu:v3

载入本地镜像:

docker load -i tf_torch_gpu.tar

进入镜像 docker run --runtime nvidia -it ufoym/deepo:tensorflow-py36 bash

用Dockerfile创建镜像

1.1示例

#进入代码文件夹目录
1.vim Dockerfile (配置Dockerfile)
FROM fzh_image:torch_0.4_tf_1.12
COPY ./ /pcb_API (相对路径更好,绝对路径会出错)
LABEL maintainer="fzh@youedata.com"

ENV LC_ALL C.UTF-8
ENV LANG C.UTF-8

RUN sed -i 's/^\(deb\|deb-src\) \([^ ]*\) \(.*\)/\1 http:\/\/mirrors.aliyun.com\/ubuntu \3/' /etc/apt/sources.list && \
    apt update && apt install -y --no-install-recommends libsm6 libxrender1 libxext6 libgtk2.0-dev && \
    apt clean && rm -rf /var/lib/apt/lists/*


RUN pip install h5py torch==0.4.0 requests bs4 flask opencv-python pillow numpy pandas -i https://pypi.tuna.tsinghua.edu.cn/simple

RUN mkdir /root/.torch /root/.torch/models
RUN cp -r /pcb_API/models/densenet/densenet169-b2777c0a.pth /root/.torch/models/

WORKDIR /pcb_API

ENTRYPOINT ["python","/pcb_API/manage_API.py"]

1.2带有字体的dockerfile

FROM daxin-docker:1.0
COPY ./ /

LABEL maintainer="fzh@youedata.com"

ENV LC_ALL C.UTF-8
ENV LANG C.UTF-8

RUN echo "ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true" | debconf-set-selections

RUN apt-get update && apt-get install -y --no-install-recommends poppler-data ttf-mscorefonts-installer fontconfig
RUN mkdir /usr/share/fonts/myfonts
RUN cp -r /FONT/* /usr/share/fonts/myfonts/
RUN fc-cache -fv
RUN chmod -R 777 /usr/share/fonts/myfonts

RUN pip install imutils opencv-python==3.4.2.16 sqlalchemy_utils -i https://pypi.tuna.tsinghua.edu.cn/simple
WORKDIR /src

ENTRYPOINT ["python","manage_pdf.py"]

1.3干掉nvidia源的dockerfile

FROM pytorch/pytorch:1.6.0-cuda10.1-cudnn7-devel
COPY ./ /

LABEL maintainer="fzh@youedata.com"

ENV LC_ALL C.UTF-8
ENV LANG C.UTF-8
ENV DEBIAN_FRONTEND noninteractive
# 移除nvidia的源, 解决nvidia cdn报错
RUN rm /etc/apt/sources.list.d/cuda.list /etc/apt/sources.list.d/nvidia-ml.list
RUN sed -i 's/^\(deb\|deb-src\) \([^ ]*\) \(.*\)/\1 http:\/\/mirrors.aliyun.com\/ubuntu \3/' /etc/apt/sources.list
RUN apt-get clean
RUN mv /var/lib/apt/lists /tmp
RUN mkdir -p /var/lib/apt/lists/partial
RUN apt-get clean
RUN apt update && apt-get install -y --no-install-recommends zbar-tools python-jpype tk-dev python-tk libffi-dev
RUN ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN dpkg-reconfigure -f noninteractive tzdata
RUN pip install requests Cython setuptools==41.0.0 -i https://pypi.douban.com/simple
RUN pip install -r /requirements.txt -i https://pypi.douban.com/simple
RUN pip uninstall enum34 -y
RUN pip install -U --force-reinstall --no-binary :all: gevent -i https://pypi.douban.com/simple
WORKDIR /src
ENTRYPOINT ["gunicorn", "-c", "gunicorn_config.py","-b", "0.0.0.0:6006", "manage:app"]

1.4 pull mysql镜像

拉mysql镜像
docker pull mysql:5.7.26
docker images|grep mysql
docker tag mysql:5.7.26 xunzhenhan/mysql:5.7.26
docker save xunzhenhan/mysql:5.7.26 > xunzhenhan-mysql-5.7.26.tar

1.5 用dockerfile创建mysql镜像,并设置账号密码

FROM hub.youedata.com/new_dataos_deploy/mysql:5.7

#设置账号密码登录
ENV MYSQL_ROOT_PASSWORD Admin_2018
#将所需文件放到容器中
#COPY setup.sh /mysql/setup.sh
#COPY schema.sql /mysql/schema.sql
#COPY privileges.sql /mysql/privileges.sql

#设置容器启动时执行的命令
CMD ["sh", "/mysql/setup.sh"]

 2.docker bulid

docker build -t pcb_docker . --rm (得到镜像)

3.建容器

run -d就后台挂起

nvidia-docker run -it -p 5112:6006 pcb_api_docker:latest

nvidia-docker run -it -p 5112:6006 --entrypoint bash pcb_api_docker:latest(不执行Dockerfile里的python /pcb_API/manage_API.py用做调试)

5.docker的一些命令

批量删除none的docker镜像  docker images|grep none|awk '{print $3}'|xargs docker rmi

删除异常停止的docker容器 docker rm `docker ps -a | grep Exited | awk '{print $1}'`

docker inspect id 查看docker的一些相关信息,对于组件可以查看config.py
docker logs id >xxx.txt 将docker日志输出为txt

docker cp

docker info 可以查看docker的磁盘目录

将主机/www/runoob目录拷贝到容器96f7f14e99ab的/www目录下。

docker cp /www/runoob 96f7f14e99ab:/www/

将容器96f7f14e99ab的/www目录拷贝到主机的/tmp目录中。

docker cp  96f7f14e99ab:/www /tmp/

6.将用户添加进docker用户组

1. 创建docker用户组
sudo groupadd docker
2. 将用户添加到docker用户组
sudo usermod -aG docker USER_NAME
3.查看是否添加成功
cat /etc/group | grep ^docker
4.更新docker组
newgrp docker
5. 重启docker服务
sudo systemctl restart docker

7.docker: Error response from daemon: Unknown runtime specified nvidia.

sudo systemctl daemon-reload
sudo systemctl restart docker

六.下载使用TensorFlow镜像
根据需要的版本下载tensorflow镜像并开启tensorflow容器: 

https://github.com/alibaba/x-deeplearning/wiki/%E7%BC%96%E8%AF%91%E5%AE%89%E8%A3%85?from=singlemessage

七.安装tensorflow

pip3 install tensorflow-gpu==1.12.0

import tensorflow as tf
a=tf.constant(1)
with tf.Session() as sess:
    print(sess.run(a))

八.安装pytorch

https://pytorch.org/get-started/locally/

安装torch官网根据cuda版本来安装

conda install pytorch torchvision cudatoolkit=9.0 -c pytorch

其中,-c pytorch参数指定了conda获取pytorch的channel,在此指定为conda自带的pytorch仓库。
因此,只需要将-c pytorch语句去掉,就可以使用清华镜像源快速安装pytorch了。

若要安装指定版本的pytorch

pip install torch==0.4.0

import torch
print(torch.version.cuda)
print(torch.cuda.is_available())
print(torch.__version__)

如果出现fasle,去检查cuda是否装好

最终挂载本地与docker同步命令

nvidia-docker run -it -v ~/AI/:/AI -w /AI/  --name=fzh_tf_torch_py3 -p 2111:22 -p 2112:6006 -p 2113:8888 tf_pytorch_gpu:1.12.0_1.1.0 bash

 

九,安装Cmake3.8

官网下载cmake https://cmake.org/files/v3.8/

vim ~/.bashrc  添加环境变量

source ~/.bashrc 

cmake --version

 

参考:

https://blog.youkuaiyun.com/WannaSeaU/article/details/88427010

 

(venv) E:\pycharm\study\dorm_face_recognition\model_training>pip install dlib Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting dlib Downloading https://pypi.tuna.tsinghua.edu.cn/packages/28/f4/f8949b18ec1df2ef05fc2ea1d1dd82ff2d050b8704b7d0d088017315c221/dlib-20.0.0.tar.gz (3.3 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.3/3.3 MB 6.6 MB/s eta 0:00:00 Installing build dependencies ... done Getting requirements to build wheel ... done Preparing metadata (pyproject.toml) ... done Building wheels for collected packages: dlib Building wheel for dlib (pyproject.toml) ... error error: subprocess-exited-with-error × Building wheel for dlib (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [48 lines of output] running bdist_wheel running build running build_ext Traceback (most recent call last): File "E:\python3.9.13\lib\runpy.py", line 197, in _run_module_as_main return _run_code(code, main_globals, None, File "E:\python3.9.13\lib\runpy.py", line 87, in _run_code exec(code, run_globals) File "E:\pycharm\study\venv\Scripts\cmake.exe\__main__.py", line 4, in <module> ModuleNotFoundError: No module named &#39;cmake&#39; ================================================================================ ================================================================================ ================================================================================ CMake is not installed on your system! Or it is possible some broken copy of cmake is installed on your system. It is unfortunately very common for python package managers to include broken copies of cmake. So if the error above this refers to some file path to a cmake file inside a python or anaconda or miniconda path then you should delete that broken copy of cmake from your computer. Instead, please get an official copy of cmake from one of these known good sources of an official cmake: - cmake.org (this is how windows users should get cmake) - apt install cmake (for Ubuntu or Debian based systems) - yum install cmake (for Redhat or CenOS based systems) On a linux machine you can run `which cmake` to see what cmake you are actually using. If it tells you it&#39;s some cmake from any kind of python packager delete it and install an official cmake. More generally, cmake is not installed if when you open a terminal window and type cmake --version you get an error. So you can use that as a very basic test to see if you have cmake installed. That is, if cmake --version doesn&#39;t run from the same terminal window from which you are reading this error message, then you have not installed cmake. Windows users should take note that they need to tell the cmake installer to add cmake to their PATH. Since you can&#39;t run commands that are not in your PATH. This is how the PATH works on Linux as well, but failing to add cmake to the PATH is a particularly common problem on windows and rarely a problem on Linux. ================================================================================ ================================================================================ ================================================================================ [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for dlib Failed to build dlib ERROR: Could not build wheels for dlib, which is required to install pyproject.toml-based projects
最新发布
06-26
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值