Server - 服务器配置 Conda 和 Jupiter Lab 的环境

欢迎关注我的CSDN:https://spike.blog.csdn.net/
本文地址:https://blog.csdn.net/caroline_wendy/article/details/125471196

Conda和Jupiter是服务器研发算法必备,两者连用提升开发效率。

Conda和Jupiter

安装Conda

miniconda可以首次安装,也可以复用环境。

首次安装Conda

安装miniconda,其它版本miniconda或者anaconda均可,命令如下:

bash files/Miniconda3-py38_4.10.3-Linux-x86_64.sh

实际下载地址:https://repo.anaconda.com/miniconda/,约98.8M

修改路径,默认即可:

[/home/user/miniconda3] >>> 

注意:配置在共享区域,速度较慢,不推荐。

复用Conda环境

复制.condarc.bashrc至当前环境,TORCH_HOME是torch模型的存储目录,如下:

cp /nfs/user/files/.condarc /home/user/.conda/.

# 添加配置命令
# my configs
export TORCH_HOME=/nfs/user/workspace/torch_home/

其中,.condarc是conda的配置:

channels:
  - defaults
show_channel_urls: true
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
  - 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
channel_priority: disabled
allow_conda_downgrades: true

激活conda:

source /home/user/.bashrc

创建conda环境,--clone可选,建议使用tmux,建议备份环境:

# Tmux可选
tmux new -s conda-init

# 复制创建
conda create -n torch-def --clone /nfs/user/conda/envs/torch-def/  # 创建
# 直接创建
conda create -n torch-def python=3.8

conda activate # 激活
conda deactivate  # 不激活

# 删除环境
# conda remove -n torch-def --all

# 备份环境
cp -r /home/user/miniconda3 /nfs/user/conda_env/miniconda3-jupyter-20220626

安装 pip 环境:

# python 2.7
curl "https://bootstrap.pypa.io/pip/2.7/get-pip.py" > get-pip-2.7.py
python get-pip-2.7.py

# python 3
curl 'https://bootstrap.pypa.io/get-pip.py' > get-pip.py
python get-pip.py

配置pip源:

mkdir ~/.pip
vim ~/.pip/pip.conf

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

安装pytorch,使用pip安装较慢,建议使用conda:

  • 时间比较长,使用tmux持续窗口。
  • 必须安装pytorch cuda 11版本,否则无法正常运行,具体cuda版本,参考nvidia-smi。
conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch

测试GPU是否可用,数据是否可用,参考,两个都需要测试。

# 测试环境
ngpu= 1
# Decide which device we want to run on
device = torch.device("cuda:0" if (torch.cuda.is_available() and ngpu > 0) else "cpu")
print("驱动为:",device)
print("GPU型号: ",torch.cuda.get_device_name(0))


# 测试张量
import 	torch
import  time
print(torch.__version__)
print(torch.cuda.is_available())

a = torch.randn(100, 10)
b = torch.randn(10, 200)
device = torch.device('cuda')
a = a.to(device)
b = b.to(device)
c = torch.matmul(a, b)
print(a.device, c.device)

卸载Conda环境

删除conda文件夹,删除.bashrc的启动命令,和删除配置文件,即可:

vim .bashrc
rm -rf ~/.condarc ~/.conda ~/.continuum

配置Jupiter

配置Jupiter Notebook

Jupyter安装

pip install ipython
pip install jupyter

配置Jupyter环境,参考After installing with pip, “jupyter: command not found”

vim ~/.bashrc

# shift+g跳转到文件尾部,gg跳转到文件头部
# jupyter
export PATH=$PATH:~/.local/bin

设置密码,可以设置为123或其他密码。

ipython
# from notebook.auth import passwd # 旧版
from jupyter_server.auth import passwd
passwd()

# 每次都不同
'argon2:$argon2id$......'

exit

配置密码:

mkdir ~/.jupyter
vim ~/.jupyter/jupyter_notebook_config.py

# 说明
c.ServerApp.ip='*' #设置访问notebook的ip,*表示所有IP,这里设置ip为都可访问  
c.ServerApp.password=u'argon2:$argon2id$v=19...' #填写刚刚生成的密文  
c.ServerApp.open_browser=False # 禁止notebook启动时自动打开浏览器(在linux服务器一般都是ssh命令行访问,没有图形界面的。所以,启动也没啥用)  
c.ServerApp.port=8901 #指定访问的端口,默认是8888。  

# 实际,不能有空格或汉字
c.ServerApp.ip='*'
c.ServerApp.password=u'argon2:$argon2id$v=19...'
c.ServerApp.open_browser=False
c.ServerApp.port=8901
c.ServerApp.notebook_dir="/"

进入工作环境,先尝试启动jupyter,再切换nohup模式启动:

cd /nfs/user

jupyter notebook --allow-root
nohup jupyter notebook --allow-root > nohup.jupyter-53.out &

ps aux | grep notebook  # 查看是否启动

访问地址,需要替换为真实IP地址,使用ifconfig查询eth0,例如:http://172.xx.0.xx:8889/,密码123

配置Jupiter Lab

额外安装包:

python -m pip install --upgrade pip
pip install jupyterlab

启动Jupiter Lab

nohup jupyter lab --port=8901 --no-browser --allow-root > nohup.jupyter-lab.out &

当使用SSH登录服务器时,无法直接获取IP地址。需要配置SSH隧道转发:

ssh -N -f -L localhost:8899:localhost:8899 [yourname]@xxx.xxx.xxx.xxx

手动关闭隧道转发:

lsof -ti:8899 | xargs kill -9

配置Conda环境到Jupiter Notebook

配置conda环境到jupyter,参考jupyter notebook 如何配置conda环境:

python -m ipykernel install --user --name torch-new --display-name "torch-new"

其他jupyter的kernel环境命令:

jupyter kernelspec list  # 显示所用kernel
jupyter kernelspec remove torch-new  # 删除kernel

配置 Bash

参考,注意替换[user]为自己的目录,执行 source ~/.bashrc 配置成功。

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000

# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize

# If set, the pattern "**" used in a pathname expansion context will
# match all files and zero or more directories and subdirectories.
#shopt -s globstar

# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color|*-256color) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
#force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
	# We have color support; assume it's compliant with Ecma-48
	# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
	# a case would tend to support setf rather than setaf.)
	color_prompt=yes
    else
	color_prompt=
    fi
fi

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

# colored GCC warnings and errors
#export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'

# some more ls aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

# Add an "alert" alias for long running commands.  Use like so:
#   sleep 10; alert
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

# my configs
export PATH=$PATH:~/.local/bin
export TORCH_HOME=/nfs_baoding/[user]/workspace/torch_home/
export LD_LIBRARY_PATH=/home/[user]/miniconda3/lib:$LD_LIBRARY_PATH 

alias bos='bcecmd --conf-path /etc/bceconf/ bos'

# Added by cryoSPARC:
export PATH="/nfs_baoding/[user]/workspace/cryosparc/cryosparc_master/bin":$PATH
export PATH="/home/[user]/.aspera/connect/bin":$PATH
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在服务器配置conda环境,您可以按照以下步骤进行操作: 1. 首先,您需要在服务器上安装conda。您可以访问conda的官方网站并按照指示进行安装。如果您是在生物信息领域工作,可以使用bioconda来安装conda。您可以通过访问https://bioconda.github.io/user/install.html 来获取详细的安装指南。 2. 一旦conda安装完成,您可以通过运行以下命令来激活conda环境: ``` source ~/.bashrc ``` 这将激活conda并使其可用于您的终端会话。 3. 您可以使用以下命令检查conda的版本: ``` conda --version ``` 如果成功显示了conda的版本信息,则说明安装和激活过程都成功了。 4. 如果您想修改默认的conda环境和缓存路径,您可以使用以下命令查看当前的默认环境和缓存路径: ``` conda info ``` 这将显示有关conda环境和缓存路径的详细信息。您可以根据需要对其进行修改。 通过按照以上步骤,您可以在服务器上成功配置conda环境。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [服务器conda配置和安装](https://blog.csdn.net/qq_45058516/article/details/122476928)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [服务器安装conda环境](https://blog.csdn.net/FFive2021/article/details/130548825)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SpikeKing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值