阿里云Ubuntu服务器通过Anaconda配置python纯净虚拟环境

由于阿里云的ubuntu服务器 默认有一个2.7一个3.5版本的python,如果又另外安装和使用更高版本的话,经常会遇到运行python程序出现已经安装依赖包但找不到对应依赖包的情况。

给远程服务器安装Anaconda,然后用conda创建新的python虚拟环境,并且可以根据生产需求切换不同的python版本。

1、Anaconda下载安装:

下载地址1:Anaconda官网
下载地址2:清华源地址
下载所需的Anaconda 版本至自己的服务器(随便选一个自己的文件放就行)。

qiang@09230:~$ cd /home/qiang
qiang@09230:~$ wget https://repo.anaconda.com/archive/Anaconda3-2021.05-Linux-x86_64.sh
--2022-05-09 20:02:26--  https://repo.anaconda.com/archive/Anaconda3-2021.05-Linux-x86_64.sh
Resolving repo.anaconda.com (repo.anaconda.com)... 104.16.131.3, 104.16.130.3, 2606:4700::6810:8303, ...
Connecting to repo.anaconda.com (repo.anaconda.com)|104.16.131.3|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 570853747 (544M) [application/x-sh]
Saving to: ‘Anaconda3-2021.05-Linux-x86_64.sh’

Anaconda3-2021.05-Linux-x86_64.sh                     100%[========================>] 544.41M  11.8MB/s    in 1m 51s

2022-05-09 20:04:18 (4.91 MB/s) - ‘Anaconda3-2021.05-Linux-x86_64.sh’ saved [570853747/570853747]

qiang@09230:~$ ls
Anaconda3-2021.05-Linux-x86_64.sh  
qiang@09230:~$

在Linux系统里面.sh文件是可执行的脚本文件,可以用命令bash来进行安装。
在安装包文件目录下输入安装命令:

bash Anaconda3-2019.10-Linux-x86_64.sh
qiang@09230:~$ ls
 AlarmReport.log                     BoxCommunicationRun.log   flask                     'Lif tControlTest.txt'   MQTT            mysql_bak.sh               protect.sh        wget-log.1
 anaconda3                           django.log                flask.log                  LiftControlTest.txt     mqtt.log        NaviCommunicationRun.log   qiang             yangrui
 Anaconda3-2021.05-Linux-x86_64.sh   django_projects           LiftCommunicationRun.log   log                     mysql_bak2.sh   nginx-auth-ldap            start_server.sh
 BackGarden                          element.log               LiftControlNode.log        mapbackup               mysql_bak3.sh   nohup.out                  test
 bak                                 FlashCommunication.log    LiftControlTest.log        mapsync                 mysql_bak4.sh   php                        wget-log
qiang@09230:~$ sudo bash Anaconda3-2021.05-Linux-x86_64.sh

Welcome to Anaconda3 2021.05

In order to continue the installation process, please review the license
agreement.
Please, press ENTER to continue
>>>
===================================
End User License Agreement - Anaconda Individual Edition
===================================

Copyright 2015-2021, Anaconda, Inc.

All rights reserved under the 3-clause BSD License:

This End User License Agreement (the "Agreement") is a legal agreement between you and Anaconda, Inc. ("Anaconda") and governs your use of Anaconda Individual Edition (which was formerly known as Anaconda Distributi
on).

Subject to the terms of this Agreement, Anaconda hereby grants you a non-exclusive, non-transferable license to:
...........................
Export; Cryptography Notice
===========================

You must comply with all domestic and international export laws and regulations that apply to the software, which include restrictions on destinations, end users, and end use. Anaconda Individual Edition includes cr
yptographic software. The country in which you currently reside may have restrictions on the import, possession, use, and/or re-export to another country, of encryption software. BEFORE using any encryption software
, please check your country's laws, regulations and policies concerning the import, possession, or use, and re-export of encryption software, to see if this is permitted. See the Wassenaar Arrangement http://www.was
senaar.org/ for more information.

Anaconda has self-classified this software as Export Commodity Control Number (ECCN) 5D992.c, which includes mass market information security software using or performing cryptographic functions with asymmetric algo
rithms. No license is required for export of this software to non-embargoed countries.

The Intel Math Kernel Library contained in Anaconda Individual Edition is classified by Intel as ECCN 5D992.c with no license required for export to non-embargoed countries.

The following packages listed on https://www.anaconda.com/cryptography are included in the repository accessible through Anaconda Individual Edition that relate to cryptography.

Last updated April 5, 2021

Do you accept the license terms? [yes|no]
[no] >>> yes

Anaconda3 will now be installed into this location:
/root/anaconda3

  - Press ENTER to confirm the location
  - Press CTRL-C to abort the installation
  - Or specify a different location below

[/root/anaconda3] >>> yes
......................................
 ujson              pkgs/main/linux-64::ujson-4.0.2-py38h2531618_0
  unicodecsv         pkgs/main/linux-64::unicodecsv-0.14.1-py38_0
  unixodbc           pkgs/main/linux-64::unixodbc-2.3.9-h7b6447c_0
  urllib3            pkgs/main/noarch::urllib3-1.26.4-pyhd3eb1b0_0
  watchdog           pkgs/main/linux-64::watchdog-1.0.2-py38h06a4308_1
  wcwidth            pkgs/main/noarch::wcwidth-0.2.5-py_0
  webencodings       pkgs/main/linux-64::webencodings-0.5.1-py38_1
  werkzeug           pkgs/main/noarch::werkzeug-1.0.1-pyhd3eb1b0_0
  wheel              pkgs/main/noarch::wheel-0.36.2-pyhd3eb1b0_0
  widgetsnbextension pkgs/main/linux-64::widgetsnbextension-3.5.1-py38_0
  wrapt              pkgs/main/linux-64::wrapt-1.12.1-py38h7b6447c_1
  wurlitzer          pkgs/main/linux-64::wurlitzer-2.1.0-py38h06a4308_0
  xlrd               pkgs/main/noarch::xlrd-2.0.1-pyhd3eb1b0_0
  xlsxwriter         pkgs/main/noarch::xlsxwriter-1.3.8-pyhd3eb1b0_0
  xlwt               pkgs/main/linux-64::xlwt-1.3.0-py38_0
  xmltodict          pkgs/main/noarch::xmltodict-0.12.0-py_0
  xz                 pkgs/main/linux-64::xz-5.2.5-h7b6447c_0
  yaml               pkgs/main/linux-64::yaml-0.2.5-h7b6447c_0
  yapf               pkgs/main/noarch::yapf-0.31.0-pyhd3eb1b0_0
  zeromq             pkgs/main/linux-64::zeromq-4.3.4-h2531618_0
  zict               pkgs/main/noarch::zict-2.0.0-pyhd3eb1b0_0
  zipp               pkgs/main/noarch::zipp-3.4.1-pyhd3eb1b0_0
  zlib               pkgs/main/linux-64::zlib-1.2.11-h7b6447c_3
  zope               pkgs/main/linux-64::zope-1.0-py38_1
  zope.event         pkgs/main/linux-64::zope.event-4.5.0-py38_0
  zope.interface     pkgs/main/linux-64::zope.interface-5.3.0-py38h27cfd23_0
  zstd               pkgs/main/linux-64::zstd-1.4.5-h9ceee32_0


Preparing transaction: done
Executing transaction: done
installation finished.
Do you wish the installer to initialize Anaconda3
by running conda init? [yes|no]
[no] >>>yes
......................................
no change     /home/qiang/yes/lib/python3.8/site-packages/xontrib/conda.xsh
no change     /home/qiang/yes/etc/profile.d/conda.csh
modified      /root/.bashrc

==> For changes to take effect, close and re-open your current shell. <==

If you'd prefer that conda's base environment not be activated on startup,
   set the auto_activate_base parameter to false:

conda config --set auto_activate_base false

Thank you for installing Anaconda3!

===========================================================================

Working with Python and Jupyter notebooks is a breeze with PyCharm Pro,
designed to be used with Anaconda. Download now and have the best data
tools at your fingertips.

PyCharm Pro for Anaconda is available at: https://www.anaconda.com/pycharm

qiang@09230:~$

一直按Enter回车键继续,接受许可条款。注意按enter别安太快,接下来会提示是否添加环境变量,输入yes,否则还得自己配环境变量。

输入 python 命令进入python环境,同时终端的头部会出现(base),说明Anaconda已经安装成功了,接下来可以创建纯净python编译环境了。

(base) qiang@09230:~$ python
Python 3.8.8 (default, Apr 13 2021, 19:58:26)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

另外如果需要每次打开服务器终端时自动激活conda的base环境,可以输入如下命令:

conda config --set auto_activate_base false

2、使用conda创建新的python环境

conda create --name qiang python=3.8.10
qiang@09230:~$ source ~/.bashrc
(base) qiang@09230:~$ conda config --set auto_activate_base false
(base) qiang@09230:~$ conda config --set auto_activate_base true
(base) qiang@09230:~$ conda create --name qiang python=3.8.10
Collecting package metadata (current_repodata.json): done
Solving environment: failed with repodata from current_repodata.json, will retry with next repodata source.
Collecting package metadata (repodata.json): done
Solving environment: done


==> WARNING: A newer version of conda exists. <==
  current version: 4.10.1
  latest version: 4.12.0

Please update conda by running

    $ conda update -n base -c defaults conda



## Package Plan ##

  environment location: /home/qiang/tools/anaconda3/envs/qiang

  added / updated specs:
    - python=3.8.10


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    python-3.8.10              |       h12debd9_8        57.7 MB
    ------------------------------------------------------------
                                           Total:        57.7 MB

The following NEW packages will be INSTALLED:

  _libgcc_mutex      pkgs/main/linux-64::_libgcc_mutex-0.1-main
  _openmp_mutex      pkgs/main/linux-64::_openmp_mutex-4.5-1_gnu
  ca-certificates    pkgs/main/linux-64::ca-certificates-2022.4.26-h06a4308_0
  certifi            pkgs/main/linux-64::certifi-2021.10.8-py38h06a4308_2
  ld_impl_linux-64   pkgs/main/linux-64::ld_impl_linux-64-2.35.1-h7274673_9
  libffi             pkgs/main/linux-64::libffi-3.3-he6710b0_2
  libgcc-ng          pkgs/main/linux-64::libgcc-ng-9.3.0-h5101ec6_17
  libgomp            pkgs/main/linux-64::libgomp-9.3.0-h5101ec6_17
  libstdcxx-ng       pkgs/main/linux-64::libstdcxx-ng-9.3.0-hd4cf53a_17
  ncurses            pkgs/main/linux-64::ncurses-6.3-h7f8727e_2
  openssl            pkgs/main/linux-64::openssl-1.1.1o-h7f8727e_0
  pip                pkgs/main/linux-64::pip-21.2.4-py38h06a4308_0
  python             pkgs/main/linux-64::python-3.8.10-h12debd9_8
  readline           pkgs/main/linux-64::readline-8.1.2-h7f8727e_1
  setuptools         pkgs/main/linux-64::setuptools-61.2.0-py38h06a4308_0
  sqlite             pkgs/main/linux-64::sqlite-3.38.3-hc218d9a_0
  tk                 pkgs/main/linux-64::tk-8.6.11-h1ccaba5_1
  wheel              pkgs/main/noarch::wheel-0.37.1-pyhd3eb1b0_0
  xz                 pkgs/main/linux-64::xz-5.2.5-h7f8727e_1
  zlib               pkgs/main/linux-64::zlib-1.2.12-h7f8727e_2


Proceed ([y]/n)? y


Downloading and Extracting Packages
python-3.8.10        | 57.7 MB   | ############################################################################################################################################################################ | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate qiang
#
# To deactivate an active environment, use
#
#     $ conda deactivate

(base) qiang@09230:~$ conda active qiang

CommandNotFoundError: No command 'conda active'.
Did you mean 'conda activate'?

(base) qiang@09230:~$ conda activate qiang
(qiang) qiang@09230:~$

这时在终端输入python,就可以看到我们刚刚建立好的环境:

(qiang) qiang@09230:~$ python
Python 3.8.10 (default, Jun  4 2021, 15:09:15)
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
(qiang) qiang@09230:~$

3、激活环境
激活之前先查看环境

(qiang) qiang@09230:~$ conda env list
# conda environments:
#
yourEnv                  /home/qiang/.conda/envs/yourEnv
base                     /home/qiang/tools/anaconda3
qiang                 *  /home/qiang/tools/anaconda3/envs/qiang

(qiang) qiang@09230:~$

激活环境:

(qiang) qiang@09230:~$ source activate base
(base) qiang@09230:~$ 

常用命令:

source activate yourEnv  # 激活环境
source deactivate yourEnv  # 退出环境
conda env remove -n yourEnv  # 删除环境  
conda remove --name xxxx  --all #彻底删除旧环境
conda remove -n yourEnv package_name  # 删除环境中的某个包

如果你手动修改过配置环境,需要执行下面命令,使用source命令执行脚本/.bashrc(/.bashrc会调用 /etc/bashrc文件)
source ~/.bashrc

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,864评论 6 494
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,175评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,401评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,170评论 1 286
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,276评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,364评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,401评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,179评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,604评论 1 306
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,902评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,070评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,751评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,380评论 3 319
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,077评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,312评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,924评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,957评论 2 351

推荐阅读更多精彩内容