Microsoft MVP성태의 닷넷 이야기
개발 환경 구성: 745. Linux / WSL 환경에 Miniconda 설치하기 [링크 복사], [링크+제목 복사],
조회: 103
글쓴 사람
정성태 (seongtaejeong at gmail.com)
홈페이지
첨부 파일
 

Linux / WSL 환경에 Miniconda 설치하기

파이썬 환경에서 "virtualenv + 패키지 관리자"와 같은 개념으로 conda가 있습니다. 그 conda가 패키징된 대표 제품으로 Anaconda가 있고, Miniconda는 Anaconda의 경량화 버전이라고 보시면 됩니다. 따라서 사실상 "Miniconda + 각종 부가 패키지 = Anaconda"라고 볼 수 있습니다.

암튼, WSL 환경이라면 기본 패키지의 용량이 40GB가 넘는 Anaconda보다는 역시나 Miniconda가 좋겠죠? ^^

Installing Miniconda
; https://www.anaconda.com/docs/getting-started/miniconda/install#linux-terminal-installer

설치 방법도 간단하고 여느 리눅스랑 다를 바 없습니다.

$ wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh

$ bash ./Miniconda3-latest-Linux-x86_64.sh

...[생략]...

Preparing transaction: done
Executing transaction: done
entry_point.py:256: DeprecationWarning: Python 3.14 will, by default, filter extracted tar archives and reject files or modify their metadata. Use the filter argument to control this behavior.
installation finished.
Do you wish to update your shell profile to automatically initialize conda?
This will activate conda on startup and change the command prompt when activated.
If you'd prefer that conda's base environment not be activated on startup,
   run the following command when conda is activated:

conda config --set auto_activate_base false

You can undo this by running `conda init --reverse $SHELL`? [yes|no]
[no] >>>

You have chosen to not have conda modify your shell scripts at all.
To activate conda's base environment in your current shell session:

eval "$(/home/testusr/miniconda3/bin/conda shell.YOUR_SHELL_NAME hook)"

To install conda's shell functions for easier access, first activate, then:

conda init

Thank you for installing Miniconda3!
$

마지막 질문에 "yes"라고 답하지 않으면 WSL Shell 시작 시 conda가 자동으로 활성화되지 않습니다. 하지만 'no'를 선택했어도 괜찮습니다, ^^ 이런 경우에는 수작업으로 conda를 활성화시킬 수 있는데요, 기본 설치 경로가 "/home/사용자이름/miniconda3"이므로 이런 식으로 명령어를 내리면 됩니다.

// conda 활성화
testusr@testpc:~$ source ~/miniconda3/bin/activate
(base) testusr@testpc:~$

// conda 비활성화
(base) testusr@testpc:~$ conda deactivate
testusr@testpc:~$

게다가, 다음의 명령어를 내리면,

$ conda init --all

설치 시 "yes"라고 답한 것과 동일한 효과를 볼 수 있으므로 언제든 선택 가능합니다. 참고로, 위와 같은 명령어는 결국 사용자의 "~/.bashrc" 파일에 다음과 같은 내용을 추가하는 것에 불과합니다.

$ cat ~/.bashrc

# ...[생략]...

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/testusr/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/home/testusr/miniconda3/etc/profile.d/conda.sh" ]; then
        . "/home/testusr/miniconda3/etc/profile.d/conda.sh"
    else
        export PATH="/home/testusr/miniconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

추가된 코드에 따라, WSL Shell 화면을 열면 자동으로 conda가 활성화된 상태로 시작합니다. (프롬프트를 통해 알 수 있습니다.)

(base) testusr@testpc:~$

원한다면, 저렇게 추가된 Shell 코드를 .bashrc 파일을 편집해 삭제하는 것도 가능하고, 직접 삭제하는 번거로운 과정을 대신해 주는 conda 명령어를 이용해도 됩니다.

(base) testusr@testpc:~$ conda init --reverse

또는, .bashrc 파일의 코드는 그대로 두면서 WSL Shell 시작 시 conda 활성화를 on/off 하는 것도 가능하므로 굳이 삭제까지 할 필요는 없을 것입니다.

// WSL Shell 시작 시 conda 비활성화
(base) testusr@testpc:~$ conda config --set auto_activate_base false

// WSL Shell 시작 시 conda 활성화
(base) testusr@testpc:~$ conda config --set auto_activate_base true




이후 문서에 따라,

# Managing environments
# https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html

가상 환경에 대한 CRUD도 해보고,

// 가상 환경 생성

$ conda create --name test_env
Channels:
 - defaults
Platform: linux-64
Collecting package metadata (repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: /home/testusr/miniconda3/envs/test_env



Proceed ([y]/n)? y


Downloading and Extracting Packages:

Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate test_env
#
# To deactivate an active environment, use
#
#     $ conda deactivate

(base) $

// 더욱 편리한 점은, 보통 특정 버전의 python 개발 환경을 구성하고 싶을 때 인자로 지정해 가상 환경을 만드는 것이 가능합니다.

$ conda create --name py311build python=3.11

// 가상 환경 조회

$ conda env list

# conda environments:
#
base                 * /home/testusr/miniconda3
test_env               /home/testusr/miniconda3/envs/test_env

$ conda info --envs

# conda environments:
#
base                 * /home/testusr/miniconda3
test_env               /home/testusr/miniconda3/envs/test_env

// 가상 환경 활성/패키지 설치

$ conda activate test_env
(test_env) testusr@testpc:~$

// pip를 이용한 패키지 설치 (Pip installs Python packages)
(test_env) testusr@testpc:~$ conda install pip
...[생략]...

(test_env) testusr@testpc:~$ pip install pymssql
...[생략]...

(test_env) testusr@testpc:~$ python -c 'import pymssql; print(pymssql.__version__)'
2.3.4

// conda를 이용한 패키지 설치 (whereas conda installs packages which may contain software written in any language.) 
(test_env) testusr@testpc:~$ conda install numpy
...[생략]...

(test_env) testusr@testpc:~$ python -c 'import numpy; print(numpy.__version__)'
2.2.5


// 가상 환경 비활성
(test_env) testusr@testpc:~$ conda deactivate
(base) testusr@testpc:~$


// 가상 환경 삭제
(base) testusr@testpc:~$ conda remove --name test_env --all

구성한 가상 환경을 conda-pack 도구를 이용해 tar.gz로 패키징도 해보면,

$ conda install conda-pack
$ which conda-pack
/home/testusr/miniconda3/envs/test_env/bin/conda-pack

$ conda-pack -n test_env -o test_env.tar.gz

기본적인 활용법은 모두 끝났다고 볼 수 있습니다. ^^




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 5/23/2025]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 91  92  93  [94]  95  96  97  98  99  100  101  102  103  104  105  ...
NoWriterDateCnt.TitleFile(s)
11599정성태7/10/201818151Math: 42. GeoGebra 기하 (19) - 두 원의 안과 밖으로 접하는 직선파일 다운로드1
11598정성태7/10/201820174Windows: 147. 시스템 복구 디스크를 USB 디스크에 만드는 방법
11597정성태7/10/201822340사물인터넷: 17. Thinary Electronic - ATmega328PB 아두이노 호환 보드의 개발 환경 구성
11596정성태7/10/201819975기타: 72. 과거의 용어 설명 - OWIN
11595정성태7/10/201825566사물인터넷: 16. New NodeMCU v3 아두이노 호환 보드의 기본 개발 환경 구성
11594정성태7/8/201820286Math: 41. GeoGebra 기하 (18) - 원의 중심 및 접선파일 다운로드1
11593정성태7/8/201819254Math: 40. GeoGebra 기하 (17) - 각의 복사파일 다운로드1
11591정성태7/7/201818579Math: 39. GeoGebra 기하 (16) - 삼각형의 방심과 방접원파일 다운로드1
11590정성태7/7/201818252Math: 38. GeoGebra 기하 (15) - 삼각형의 수심파일 다운로드1
11589정성태7/7/201818131.NET Framework: 787. object로 형변환된 인스턴스를 원래의 타입 인자로 제네릭 메서드를 호출하는 방법 [2]파일 다운로드1
11588정성태7/7/201819674디버깅 기술: 116. windbg 분석 사례 - ASP.NET 웹 응용 프로그램의 CPU 100% 현상 (3)
11587정성태7/5/201819588.NET Framework: 786. ASP.NET - HttpCookieCollection을 다중 스레드에서 사용할 경우 무한 루프 현상
11586정성태7/5/201818465Math: 37. GeoGebra 기하 (14) - 삼각형의 무게 중심파일 다운로드1
11585정성태7/5/201818935Math: 36. GeoGebra 기하 (13) - 삼각형의 외심과 외접하는 원파일 다운로드1
11584정성태7/5/201818524Math: 35. GeoGebra 기하 (12) - 삼각형의 내심과 내접하는 원파일 다운로드1
11583정성태7/5/201818719.NET Framework: 785. public으로 노출되지 않은 다른 어셈블리의 delegate 인스턴스를 Reflection으로 생성하는 방법파일 다운로드1
11582정성태7/5/201825050.NET Framework: 784. C# - 제네릭 인자를 가진 타입을 생성하는 방법 [1]파일 다운로드1
11581정성태7/4/201821981Math: 34. GeoGebra 기하 (11) - 3대 작도 불능 문제의 하나인 임의 각의 3등분파일 다운로드1
11580정성태7/4/201818826Math: 33. GeoGebra 기하 (10) - 직각의 3등분파일 다운로드1
11579정성태7/4/201817691Math: 32. GeoGebra 기하 (9) - 임의의 선분을 한 변으로 갖는 정삼각형파일 다운로드1
11578정성태7/3/201817844Math: 31. GeoGebra 기하 (8) - 호(Arc)의 이등분파일 다운로드1
11577정성태7/3/201817678Math: 30. GeoGebra 기하 (7) - 각의 이등분파일 다운로드1
11576정성태7/3/201820269Math: 29. GeoGebra 기하 (6) - 대수의 4칙 연산파일 다운로드1
11575정성태7/2/201820714Math: 28. GeoGebra 기하 (5) - 선분을 n 등분하는 방법파일 다운로드1
11574정성태7/2/201819115Math: 27. GeoGebra 기하 (4) - 선분을 n 배 늘이는 방법파일 다운로드1
11573정성태7/2/201818269Math: 26. GeoGebra 기하 (3) - 평행선
... 91  92  93  [94]  95  96  97  98  99  100  101  102  103  104  105  ...