【Python】Anaconda環境の構築・設定まとめ

Python

インストーラの入手

Anacondaのダウンロードサイトからインストーラをダウンロードする。

「Download Now」の「Skip Registration」をクリックする。

OSごとにAnaconda/Minicondaのインストーラへのリンクが表示される。

インストールするデバイスに合うものをクリックすると、ダウンロードが開始される。

今回はLinux版Minicondaのインストーラをダウンロードする。

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

インストール

インストーラのオプションは-hオプションで確認できる。

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

usage: Miniconda3-latest-Linux-x86_64.sh [options]

Installs Miniconda3 py313_26.1.1-1
-b           run install in batch mode (without manual intervention),
             it is expected the license terms (if any) are agreed upon
-f           no error if install prefix already exists
-h           print this help message and exit
-p PREFIX    install prefix, defaults to /home/tomita/miniconda3, must not contain spaces.
-s           skip running pre/post-link/install scripts
-m           disable the creation of menu items / shortcuts
-u           update an existing installation
-t           run package tests after installation (may install conda-build)
-c           run 'conda init' after installation (only applies to batch mode)

バッチモードでダウンロードする。

以下のコマンドを実行する。

$ bash Miniconda3-latest-Linux-x86_64.sh -bc -p ~/miniconda3

-pオプションでインストール先のファイルパスを指定する。

また-cオプションで、condaコマンドなどが使えるようシェルの設定ファイル(ex. .bashrc)を書き換えを行うよう設定する。

コマンドを実行すると、どの設定ファイルを書き換えたか表示される。

Initializing '/home/hoge/lib/miniconda3' with 'conda init'...
no change     /home/hoge/lib/miniconda3/condabin/conda
no change     /home/hoge/lib/miniconda3/bin/conda
no change     /home/hoge/lib/miniconda3/bin/conda-env
no change     /home/hoge/lib/miniconda3/bin/activate
no change     /home/hoge/lib/miniconda3/bin/deactivate
no change     /home/hoge/lib/miniconda3/etc/profile.d/conda.sh
no change     /home/hoge/lib/miniconda3/etc/fish/conf.d/conda.fish
no change     /home/hoge/lib/miniconda3/shell/condabin/Conda.psm1
no change     /home/hoge/lib/miniconda3/shell/condabin/conda-hook.ps1
no change     /home/hoge/lib/miniconda3/lib/python3.13/site-packages/xontrib/conda.xsh
no change     /home/hoge/lib/miniconda3/etc/profile.d/conda.csh
modified      /home/hoge/.bashrc

「modified」と表示されている.bashrcファイルが変更された。

現在のシェルに変更を反映させるために、設定ファイルを再読み込みする。

. ~/.bashrc

プロンプトが以下のように変わればOK。

(base) [hoge@local ~]$

デフォルトでは、シェルを立ち上げるとbase環境が自動的に有効化される。

base環境の自動起動を止めたい場合は、本記事の「base環境の自動起動を止める」を参照。

仮想環境の作成

「conda create」コマンドで仮想環境を作成する。今回はこの環境にPythonをインストールする。

$ conda create -n myenv python

「Python=3.14」のようにバージョンを指定できる。

他のライブラリも入れる際は、python(=〇.〇〇)のあとにスペースで開けて続ける。

コマンドを実行すると、新たにいれるライブラリ一覧が表示されるので、「y」を入力し続行する。

仮想環境の削除

仮想環境の削除にはconda env removeを実行する。

まず、消したい環境が起動している場合はdeactivateする。

以下のコマンドを実行する。

$ conda env remove -n myenv # myenvは環境名

Proceed ([y]/n)?と聞かれたら、「y」を入力する。

仮想環境の起動・終了

仮想環境を起動するためには、conda activateコマンドを使用する。

$ conda activate myenv # myenvは環境名

仮想環境を終了するためには、conda deactivateコマンドを使用する。

$ conda deactivate

その他の設定

base環境の自動起動を止める

デフォルトでは、シェルを立ち上げるとbase環境が自動的に有効化される。

これを止めるには、以下のコマンドを実行する。

$ conda config --set auto_activate false

シェルを立ち上げなおして、プロンプトの頭に(base)が付いていないことが確認できる。

[hoge@local]$

コメント

タイトルとURLをコピーしました