您现在的位置是:首页 >技术交流 >docker ubuntu 搭建torch开发环境网站首页技术交流
docker ubuntu 搭建torch开发环境
简介docker ubuntu 搭建torch开发环境
一、配置百度源
将/etc/apt/sources.list的内容替换为如下:
以Ubuntu18.04为例
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb http://mirrors.baidu.com/file/ubuntu/ bionic main restricted universe multiverse
# deb-src https:/mirrors.baidu.com/file/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.baidu.com/file/ubuntu/ bionic-updates main restricted universe multiverse
# deb-src https://mirrors.baidu.com/file/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.baidu.com/file/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src https://mirrors.baidu.com/file/ubuntu/ bionic-backports main restricted universe multiverse
deb http://mirrors.baidu.com/file/ubuntu/ bionic-security main restricted universe multiverse
# deb-src https://mirrors.baidu.com/file/ubuntu/ bionic-security main restricted universe multiverse
# 预发布软件源,不建议启用
# deb http://mirrors.baidu.com/file/ubuntu/ bionic-proposed main restricted universe multiverse
# deb-src http://mirrors.baidu.com/file/ubuntu/ bionic-proposed main restricted universe multiverse
再执行 apt-get update
更新
二、启动docker
创建start_docker.sh脚本
#/bin/bash
# paddle1.7.1镜像地址
# image_name=registry.baidu.com/paddlecloud/paddlecloud-runenv-centos6u3-online:paddlecloud-v1.7.1-gcc482-cuda10.0_cudnn7
# image_name=registry.baidubce.com/paddlepaddle/paddle:2.0.0-gpu-cuda10.1-cudnn7
#image_name=registry.baidu.com/vis-general-ocr/paddle:2.0.0-gpu-cuda10.1-cudnn7-pdc-base
#image_nmame=iregistry.baidu-int.com/paddlecloud/pytorch1.4.0:ubuntu16.04-cuda10.1_cudnn7
image_name=iregistry.baidu-int.com/paddlecloud/pytorch1.4.0:ubuntu16.04-cuda10.1_cudnn7
#image_name=iregistry.baidu-int.com/paddlecloud/paddlecloud-runenv-ubuntu16.04-offline:paddlecloud-paddle-v2.2.2-gcc820-cuda10.1_cudnn7
# 容器名称
container_name=$1
guazai_path= xxx
# 挂载物理机GPU驱动库
export CUDA_SO="$(ls /usr/lib64/libcuda* | xargs -I{} echo '-v {}:{}') $(ls /usr/lib64/libnvidia* | xargs -I{} echo '-v {}:{}')"
# 挂载nvidia硬件
export DEVICES=$(find /dev/nvidia* -maxdepth 1 -not -type d | xargs -I{} echo '--device {}:{}')
# -d: 后台运行容器
# -v: 格式为 {宿主机目录}:{容器目录},将容器目录挂载到宿主机目录上
# --name: 为容器命名
# -it: 以交互式的方式启动容器
# --network=host: 使用宿主机网络
docker run ${CUDA_SO} ${DEVICES} -it -d --network=host --ipc=host
-v ${guazai_path}:${guazai_path}
-v /usr/bin/nvidia-smi:/usr/bin/nvidia-smi
--name ${container_name} ${image_name} bash
ps:docker镜像为百度内镜像,torch版本较低,后续手动安装
启动脚本
source start_docker.sh ${container_name}
三、docker基本操作
启动docker
image_name 为创建的容器名称
docker exec -it ${container_name} bash
删除docker
首先需要先关闭docker
docker stop ${image_id}
然后再删除
docker rm ${image_id}
四、安装环境
1、安装python3.6以上的版本
docker中自带python2.7 和python3.6,在这里我们将其升级为python3.6以上的版本,去官网下载对应的python版本,以python3.7为例。
a. 查看当前安装的python:
which python
which python3
Out:
/opt/conda/envs/py27/bin/python
/usr/bin/python3
b. 安装依赖,执行下列命令安装依赖过程中,如有提示,一律输入 y 。
sudo apt-get install python-dev libffi-dev libssl-dev
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev
c. 执行安装
./configure prefix=/usr/local/python3
make && make install
d. 修改软连接(配置全局变量)
#备份现有的软连接
mv /usr/bin/python3 /opt/conda/bin/python3.bak
#添加python3的软链接
ln -s /usr/local/python3/bin/python3.7 /opt/conda/bin/python3
#测试是否安装成功了
python3 -V
e. 安装/升级pip
apt-get install python3-pip
# 执行升级
pip3 install --upgrade pip
2、安装torch
去官网下载python对应的torch / vision版本,在用pip3进行安装,以python3.7,torch1.10的版本为例:
pip3 install torch-1.10.1+cu113-cp37-cp37m-linux_x86_64.whl
pip3 install torchvision-0.11.2+cu113-cp37-cp37m-linux_x86_64.whl
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。