Microsoft MVP성태의 닷넷 이야기
개발 환경 구성: 745. Linux / WSL 환경에 Miniconda 설치하기 [링크 복사], [링크+제목 복사],
조회: 112
글쓴 사람
정성태 (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)
11396정성태12/8/201743415개발 환경 구성: 343. Visual Studio - 리눅스 용 프로젝트의 인텔리센스를 위한 헤더 파일 처리 방법 [3]
11395정성태12/8/201719047오류 유형: 441. 이벤트 로그 - Time Provider NtpClient: No valid response has been received from domain controller
11394정성태12/8/201718691개발 환경 구성: 342. 비주얼 스튜디오에서 실행하던 ASP.NET Core (.NET Framework) 응용 프로그램을 명령행에서 실행하는 방법
11393정성태12/7/201723449Windows: 145. 윈도우 10 빌드 17046부터 WSL에서 백그라운드 작업 지원 [5]
11392정성태12/7/201718394개발 환경 구성: 341. openSUSE에 닷넷 코어 설치
11391정성태12/7/201721405개발 환경 구성: 340. WSL을 이용해 윈도우 PC 1대에서 openSUSE 응용 프로그램을 Visual Studio로 개발하는 방법 [1]
11390정성태12/7/201730089개발 환경 구성: 339. WSL을 이용해 윈도우 PC 1대에서 Linux 응용 프로그램을 Visual Studio로 개발하는 방법 [6]
11389정성태12/7/201718752오류 유형: 440. .NET Core 오류 - 0x80131620 Unable to load DLL 'libuv'
11388정성태12/6/201722504개발 환경 구성: 338. WSL 또는 Ubuntu에 닷넷 코어 설치 [3]
11387정성태12/6/201722621오류 유형: 439. 이벤트 로그 - Data Sharing Service 서비스의 %%3239247874 오류 메시지
11386정성태12/5/201718313오류 유형: 438. Hyper-V - '...' failed to add device 'Virtual CD/DVD Disk'
11385정성태12/5/201731407VC++: 121. DXGI를 이용한 윈도우 화면 캡처 소스 코드(Visual C++) [16]파일 다운로드1
11384정성태12/5/201720863오류 유형: 437. Visual C++ - Cannot open include file: 'SDKDDKVer.h'
11383정성태12/4/201723824디버깅 기술: 110. 비동기 코드 실행 중 예외로 인한 ASP.NET 프로세스 비정상 종료 현상 [1]
11382정성태12/4/201722364오류 유형: 436. System.Data.SqlClient.SqlException (0x80131904): Connection Timeout Expired 예외 발생 시 "[Pre-Login] initialization=48; handshake=1944;" 값의 의미
11381정성태11/30/201718920.NET Framework: 702. 한글이 포함된 바이트 배열을 나눈 경우 한글이 깨지지 않도록 다시 조합하는 방법(두 번째 이야기)파일 다운로드1
11380정성태11/30/201718928디버깅 기술: 109. windbg - (x64에서의 인자 값 추적을 이용한) Thread.Abort 시 대상이 되는 스레드를 식별하는 방법
11379정성태11/30/201719287오류 유형: 435. System.Web.HttpException - Session state has created a session id, but cannot save it because the response was already flushed by the application.
11378정성태11/29/201720949.NET Framework: 701. 한글이 포함된 바이트 배열을 나눈 경우 한글이 깨지지 않도록 다시 조합하는 방법 [1]파일 다운로드1
11377정성태11/29/201720331.NET Framework: 700. CommonOpenFileDialog 사용 시 사용자가 선택한 파일 목록을 구하는 방법 [3]파일 다운로드1
11376정성태11/28/201724792VS.NET IDE: 123. Visual Studio 편집기의 \r\n (crlf) 개행을 \n으로 폴더 단위로 설정하는 방법
11375정성태11/28/201719234오류 유형: 434. Visual Studio로 ASP.NET 디버깅 중 System.Web.HttpException - Could not load type 오류
11374정성태11/27/201724660사물인터넷: 14. 라즈베리 파이 - (윈도우의 NT 서비스처럼) 부팅 시 시작하는 프로그램 설정 [1]
11373정성태11/27/201723746오류 유형: 433. Raspberry Pi/Windows 다중 플랫폼 지원 컴파일 관련 오류 기록
11372정성태11/25/201726532사물인터넷: 13. 윈도우즈 사용자를 위한 라즈베리 파이 제로 W 모델을 설정하는 방법 [4]
11371정성태11/25/201720308오류 유형: 432. Hyper-V 가상 스위치 생성 시 Failed to connect Ethernet switch port 0x80070002 오류 발생
... 91  92  93  94  95  96  97  98  99  100  101  [102]  103  104  105  ...