🚀

minicondaのインストール方法と初期設定

2023/08/08に公開

minicondaは仮想環境構築するツールである。
Linux環境で実環境とは異なるPython versionを実行をしたい時などに使用する。
Python versionは同じで目的ごとに環境を使い分けたい場合はvenvを使う。
venvは実環境と同じPython versionでpythonパッケージを書く環境ごとに別々に管理できるツールである。


インストール方法

以下のコマンドでインストール

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
chmod +x Miniconda3-latest-Linux-x86_64.sh
sh Miniconda3-latest-Linux-x86_64.sh
# 実行後はファイルは削除
# rm Miniconda3-latest-Linux-x86_64.sh

上記を実行後、以下が表示されるが基本的にyes, EnterでOK。

Please, press ENTER to continue                                                                                                                                                                                                                                              >>> Enter

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

Miniconda3 will now be installed into this location:
/home/kabec/miniconda3

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

[/home/kabec/miniconda3] >>> Enter

Preparing transaction: done
Executing transaction: done
installation finished.
Do you wish the installer to initialize Miniconda3
by running conda init? [yes|no]
[no] >>> yes
no change     /home/kabec/miniconda3/condabin/conda
no change     /home/kabec/miniconda3/bin/conda
no change     /home/kabec/miniconda3/bin/conda-env
no change     /home/kabec/miniconda3/bin/activate
no change     /home/kabec/miniconda3/bin/deactivate
no change     /home/kabec/miniconda3/etc/profile.d/conda.sh
no change     /home/kabec/miniconda3/etc/fish/conf.d/conda.fish
no change     /home/kabec/miniconda3/shell/condabin/Conda.psm1
no change     /home/kabec/miniconda3/shell/condabin/conda-hook.ps1
no change     /home/kabec/miniconda3/lib/python3.9/site-packages/xontrib/conda.xsh
no change     /home/kabec/miniconda3/etc/profile.d/conda.csh
modified      /home/kabec/.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 Miniconda3!

上記設定のままだとターミナル起動時にbase環境としてcondaが起動する。
base環境として起動してほしくない場合は以下を参考に自動起動をOFFに設定

source ~/.bashrc
conda config --set auto_activate_base False
conda config --show | grep auto_activate_base

以下実施する必要があるのか後日調査

conda config --add channels conda-forge
conda config --remove channels defaults
conda update conda
conda update --all

環境の作成

以下のコマンドで仮想環境を作成

conda create -n miniconda_venv python=3.7.9

miniconda_venvは仮想環境名で、適当な文字列を入力すればよい。
環境作成後は以下のコマンドで起動できる。

conda activate miniconda_venv

正しく環境が構築できているか確認

python -V
# Python 3.7.9
which python
# /home/kabec/miniconda3/envs/miniconda_venv/bin/python
which pip
# /home/kabec/miniconda3/envs/miniconda_venv/bin/pip
which pip3
# /home/kabec/miniconda3/envs/miniconda_venv/bin/pip3

※指定するPythonのversionによってはpip3ではなく、pipコマンドのみ準備されることがある。
pip3で実行してしまうと実環境側でパッケージのインストールが実行されてしまうので環境構築後は確認すること。

仮想環境を抜ける時は以下のコマンド

conda deactivate

References

Discussion