14

Manjaro计算机视觉开发环境配置(Python/C++) - JohnHany的博客

 4 years ago
source link: http://johnhany.net/2020/03/manjaro-cv-environment/?
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

在之前的一篇文章中我分享了在Ubuntu 18.04系统上配置计算机视觉开发环境的过程:《Ubuntu计算机视觉开发环境配置(Python/C++)》。在这篇文章中,我会介绍在Manjaro 19.0系统上配置Python, C/C++开发环境,并安装PyTorch, TensorFlow和OpenCV的过程。

本文涉及的所有脚本都在我的GitHub上:https://github.com/johnhany/manjaro_setup

我的个人硬件型号及软件版本:

  • i7-9700k @ 3.6GHz
  • NVIDIA RTX 2080Ti
  • Manjaro 19.0
  • PyTorch 1.4.0
  • TensorFlow 2.1.0
  • OpenCV 4.2.0

Manjaro安装常见问题

本文暂不详述Manjaro安装过程,只根据个人经验给出安装过程中设置分区大小的建议。Ubuntu属于Debian系,而Manjaro属于ArchLinux系,这两个系统的主要区别在于包管理工具不同(Ubuntu用apt,Manjaro用pacman),Manjaro的内核的版本要高一些,而且图形界面也不同。我这里安装的是KDE桌面:https://manjaro.org/download/#kde-plasma

下面以总共约130GB大小为例:

  • /swap – 32GB。和内存大小一致就可以
  • / – 40GB。以使用Manjaro一周的情况来看,40GB的空间算是勉强够用。如下图所示,usr文件夹占用了16GB,opt文件夹占用了10GB,var文件夹占用了8GB,这样40GB的空间就所剩不多了。其中opt文件夹主要包含了Anaconda和CUDA的文件,var大部分是pacman的缓存。
  • /home – 60GB。目前我的home目录已经用了30GB,其中还有8GB是Android SDK。Anaconda的环境大约需要4GB空间。
root_dir.png

如果电脑里已经安装了Windows,在安装Manjaro的过程中可以把引导文件安装在Windows的引导分区(一个100MB大小的分区),它会自动识别并为Windows分配引导。这样以后开机时会进入Manjaro的Grub引导,而且可以选择不同的系统。

启动Manjaro黑屏的问题

虽然Manjaro 19.0已经包含了比较新的Linux内核(目前是5.4.23),但对于RTX系列显卡还是会出现在第一次启动系统时黑屏的问题。解决方法和Ubuntu下的解决方法类似,可以参考《Ubuntu计算机视觉开发环境配置(Python/C++)》。需要注意的是,从装机U盘启动之后,需要把“driver”那个选项从“free”改为“nonfree”或“proprietary”(官方文档建议Nvidia显卡用proprietary模式),然后按F10会切换到启动选项,再按E键编辑,把“splash”改为“nomodeset”。

更新pacman国内源

在国内可以改用国内的镜像源,以提高pacman的下载速度。

1. 在终端输入以下命令

sudo pacman-mirrors -c China -m rank
sudo pacman-mirrors -c China -m rank

在弹出的窗口中选择想采用的源。可以选一个延迟最低或更新时间最近的,也可以全选。

2. 编辑/etc/pacman.conf文件,可以用vim或系统自带的nano,在文件结尾添加以下内容:

[archlinuxcn]
SigLevel = Optional TrustedOnly
Server = https://mirrors.ustc.edu.cn/archlinuxcn/$arch
[archlinuxcn]
SigLevel = Optional TrustedOnly
Server = https://mirrors.ustc.edu.cn/archlinuxcn/$arch

vim可以用以下命令安装:

sudo pacman -S vim
sudo pacman -S vim

3. 在终端执行以下命令

sudo pacman -Syy && sudo pacman -S archlinuxcn-keyring
sudo pacman -Syy && sudo pacman -S archlinuxcn-keyring

安装AUR包管理器

pacman本身已经包含了比Ubuntu下的apt还要多的软件源,但有些软件还是没有被包含在pacman的源当中。所以,我们可以再安装一个AUR(Arch User Repository)包管理器,便于为更多的软件提供一键安装的支持。网上一般会推荐用yaourt,但这个工具已经不再维护了,这里我们推荐yay:

sudo pacman -S yay
sudo pacman -S yay

安装显卡驱动和CUDA

我们先介绍安装Nvidia显卡驱动,CUDA和cuDNN的过程。

Nvidia显卡驱动

Manjaro在安装过程中应该会自动安装适合的Nvidia驱动。可以在系统设置里面看到当前正在使用的驱动,如下图所示:

setting_graphics.png

如果驱动没有正确安装,可以用以下命令来安装:

sudo mhwd -a pci nonfree 0300
sudo mhwd -a pci nonfree 0300

CUDA和cuDNN

一键安装:

sudo pacman -S cuda cudnn
sudo pacman -S cuda cudnn

测试CUDA是否安装成功。把CUDA官方样例代码从/opt/cuda/samples/拷贝到个人主文件夹下(比如~/cuda_samples/),然后执行以下命令:

cd 1_Utilities/deviceQuery
./deviceQuery
cd 1_Utilities/deviceQuery
make
./deviceQuery

如果终端输出的最后一行是“Pass”,说明CUDA可以正常使用,已经安装成功。


C/C++环境

这里分别介绍gcc+gdb和clang+lldb两种环境的安装。

gcc/gdb环境

一键安装:

sudo pacman -S gcc gdb
sudo pacman -S gcc gdb

clang/lldb环境

一键安装:

sudo pacman -S clang lldb
sudo pacman -S clang lldb

Python环境

这里我们使用Anaconda建立虚拟的Python 3.7环境。

安装Anaconda

1. 一键安装:

sudo pacman -S anaconda
sudo pacman -S anaconda

2. 修改~/.bashrc文件,添加以下内容:

export PATH=/opt/anaconda/bin:$PATH
export PATH=/opt/anaconda/bin:$PATH

3. 激活conda环境,在终端执行:

source /opt/anaconda/bin/activate root
source /opt/anaconda/bin/activate root

然后执行以下命令,初始化conda:

conda init
conda init

修改conda源

修改为国内的conda源,以提高下载速度。打开~/.condarc文件(如果不存在的话建立该文件),修改文件内容如下:

channels:
- defaults
show_channel_urls: true
channel_alias: https://mirrors.tuna.tsinghua.edu.cn/anaconda
default_channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
channels:
  - defaults
show_channel_urls: true
channel_alias: https://mirrors.tuna.tsinghua.edu.cn/anaconda
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/pro
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

如果清华的conda源不能使用,可以改用上交的源:

channels:
- https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main/
- https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/free/
- defaults
show_channel_urls: true
channels:
- https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main/
- https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/free/
- defaults
show_channel_urls: true

修改pip源

改用国内的pypi源。建议执行该步骤前关闭conda的虚拟环境,可在终端中执行两次conda deactivate以退回到系统自带的Python环境。执行以下命令:

sudo pip install --upgrade pip
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
sudo pip install --upgrade pip
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

创建虚拟环境

重新打开终端,正常情况下,在用户名前会显示“(base)”字样,说明已经进入conda的默认环境。如果没有该字样的话,可以执行conda init,以激活conda环境。然后用以下命令建立一个名为”torch”的Python 3.7环境:

conda create -n torch python=3.7
conda create -n torch python=3.7

然后执行以下命令开启该环境:

conda activate torch
conda activate torch

可以顺便把常用的库安装好:

pip install numpy scipy matplotlib pylint
pip install numpy scipy matplotlib pylint

PyTorch

这里介绍在Anaconda虚拟环境下安装PyTorch 1.4.0的过程。

pip install torch torchvision
pip install torch torchvision

测试PyTorch

执行以下命令,打开Python交互模式

conda activate torch
python
conda activate torch
python

然后测试PyTorch是否安装好

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

看输出结果是否为“True”。

也可以试一下简单的张量计算

a = torch.randn(1, 10).to(device)
b = torch.randn(10, 1).to(device)
c = a @ b
print(c.item())
a = torch.randn(1, 10).to(device)
b = torch.randn(10, 1).to(device)
c = a @ b
print(c.item())

TensorFlow

这里介绍两种TensorFlow安装方法,一是在虚拟环境下直接安装官方包,二是安装Docker版本,便于模型的部署。

pip install tensorflow-gpu
pip install tensorflow-gpu

安装Docker镜像

1. 安装Docker。注意需要把<yourname>替换成你的用户名:

sudo pacman -S docker
sudo gpasswd -a <yourname> docker
sudo systemctl start docker
sudo systemctl enable docker
sudo pacman -S docker
sudo gpasswd -a <yourname> docker
sudo systemctl start docker
sudo systemctl enable docker

2. 安装nvidia-docker。

yay -S nvidia-docker
yay -S nvidia-docker

3. 更新Docker源。编辑/etc/docker/daemon.json文件:

"registry-mirrors" : [
"http://registry.docker-cn.com",
"http://docker.mirrors.ustc.edu.cn",
"http://hub-mirror.c.163.com"
"insecure-registries" : [
"registry.docker-cn.com",
"docker.mirrors.ustc.edu.cn"
"debug" : true,
"experimental" : true
{
  "registry-mirrors" : [
    "http://registry.docker-cn.com",
    "http://docker.mirrors.ustc.edu.cn",
    "http://hub-mirror.c.163.com"
  ],
  "insecure-registries" : [
    "registry.docker-cn.com",
    "docker.mirrors.ustc.edu.cn"
  ],
  "debug" : true,
  "experimental" : true
}

4. 下载docker镜像。

为了避免出现“Got permission denied while trying to connect to the Docker daemon”的错误,需要切换一下系统用户(注意需要把<yourname>替换成用户名):

sudo su
su <yourname>
sudo su
su <yourname>

然后下载并开启带有jupyter notebook的tensorflow镜像:

docker pull tensorflow/tensorflow:latest-gpu-py3-jupyter
nvidia-docker run -u $(id -u):$(id -g) --name=tf-gpu -it -p 8888:8888 tensorflow/tensorflow:latest-gpu-py3-jupyter
docker pull tensorflow/tensorflow:latest-gpu-py3-jupyter
nvidia-docker run -u $(id -u):$(id -g) --name=tf-gpu -it -p 8888:8888 tensorflow/tensorflow:latest-gpu-py3-jupyter

开启成功之后,可以在终端里所提示的网址访问notebook。


OpenCV

pip install opencv-python
pip install opencv-python

VS Code

这里只给出VS Code的安装,关于基于VS Code的Python和C++开发环境配置可以参考《Ubuntu计算机视觉开发环境配置(Python/C++)》

sudo pacman -S visual-studio-code-bin
sudo pacman -S visual-studio-code-bin

其他有用的工具

  • Chrome浏览器
sudo pacman -S google-chrome
sudo pacman -S google-chrome
  • 网易云音乐
sudo pacman -S netease-cloud-music
sudo pacman -S netease-cloud-music
  • 中文输入法
sudo pacman -S fcitx-im
sudo pacman -S fcitx-configtool
sudo pacman -S fcitx-im
sudo pacman -S fcitx-configtool

然后修改~/.xprofile文件,添加以下内容:

export GTK_IM_MODULE=fcitx
export QT_IM_MODULE=fcitx
export XMODIFIERS="@im=fcitx"
export GTK_IM_MODULE=fcitx
export QT_IM_MODULE=fcitx
export XMODIFIERS="@im=fcitx"
  • nvtop
sudo pacman -S cmake ncurses git
yay -S nvtop
sudo pacman -S cmake ncurses git
yay -S nvtop
  • TexStudio和中文LaTex环境
sudo pacman -S texlive-most texlive-langchinese texlive-latexextra texlive-fontsextra
sudo pacman -S texstudio
sudo pacman -S texlive-most texlive-langchinese texlive-latexextra texlive-fontsextra
sudo pacman -S texstudio
sudo pacman -S wqy-microhei wqy-bitmapfont wqy-zenhei wqy-microhei-lite
sudo pacman -S adobe-source-han-sans-cn-fonts adobe-source-han-serif-cn-fonts
sudo pacman -S ttf-dejavu
sudo pacman -S noto-fonts noto-fonts-extra noto-fonts-emoji noto-fonts-cjk
yay -S ttf-adobe-song
yay -S ttf-adobe-heiti
yay -S ttf-adobe-kaiti
sudo pacman -S ttf-lato
sudo pacman -S wqy-microhei wqy-bitmapfont wqy-zenhei wqy-microhei-lite
sudo pacman -S adobe-source-han-sans-cn-fonts adobe-source-han-serif-cn-fonts
sudo pacman -S ttf-dejavu
sudo pacman -S noto-fonts noto-fonts-extra noto-fonts-emoji noto-fonts-cjk

yay -S ttf-adobe-song
yay -S ttf-adobe-heiti
yay -S ttf-adobe-kaiti
sudo pacman -S ttf-lato

编辑/etc/profile文件,添加以下一行内容:

export FONTCONFIG_FILE=/etc/fonts/fonts.conf
export FONTCONFIG_FILE=/etc/fonts/fonts.conf

然后在终端执行以下命令,刷新字体缓存

fc-cache -vf
sudo texhash
fc-cache -vf
sudo texhash
  • Typora
sudo pacman -S typora
sudo pacman -S typora
  • 微信和Tim
yay -S deepin-wine-wechat
yay -S deepin-wine-tim
yay -S deepin-wine-wechat
yay -S deepin-wine-tim
  • Foxit PDF阅读器
sudo pacman -S foxitreader
sudo pacman -S foxitreader

其他工具和脚本

  • 把“文档”,“下载”等目录换为英文:
sudo pacman -S xdg-user-dirs-gtk
export LANG=en_US
xdg-user-dirs-gtk-update
sudo pacman -S xdg-user-dirs-gtk
export LANG=en_US
xdg-user-dirs-gtk-update

在弹出窗口中,选择替换。然后执行以下命令并重启:

export LANG=zh_CN.UTF-8
export LANG=zh_CN.UTF-8
  • GPU监控脚本
watch -n 0.5 nvidia-smi
watch -n 0.5 nvidia-smi

本文涉及的所有脚本都在我的GitHub上:https://github.com/johnhany/manjaro_setup


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK