LoginSignup
10
9

Linuxにanacondaインストールしてパスを通して仮想環境作成

Last updated at Posted at 2023-05-08

何度も調べ直してるので自分用メモ。

Anacondaのインストール

ターミナルでbashを起動し、wgetでインストーラパッケージをダウンロードする。
anacondaのサイトにあるURLからLinux向け最新版のものを選ぶ。

$ wget https://repo.anaconda.com/archive/Anaconda3-2023.03-1-Linux-x86_64.sh

インストーラを起動してインストールする。
Anaconda3-2023.03-1-Linux-x86_64.sh という名前のファイルがDownloads/またはホームディレクトリに作成されているので、以下コマンドでインストールの開始。

$ bash ~/Downloads/Anaconda3-2022.05-Linux-x86_64.sh

licence termsが長々と表示されるので、最後までスクロールしてyesと入力する。qキーを押すと一気にスクロールできる。

The following packages listed on https://www.anaconda.com/cryptography are included in the repository accessible through An
aconda Distribution that relate to cryptography.

Last updated February 25, 2022


Do you accept the license terms? [yes|no]
[no] >>>
Please answer 'yes' or 'no':'
>>> yes

インストールが開始され、インストール先のディレクトリが表示される。この場合は、/root/anaconda3にインストールされる。特に変更しない場合はそのままEnterを押す。

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] >>>
PREFIX=/root/anaconda3
Unpacking payload ...

完了したら、yesと入力して初期化を行う。

done
installation finished.
Do you wish the installer to initialize Anaconda3
by running conda init? [yes|no]
[no] >>> yes

パスを通す
このままcondaを実行しようとしてもbash: conda: command not foundと言われてしまうので、パスを通す必要がある。
先ほどインストール時に表示されたインストール先のディレクトリ/root/anaconda3を元に、以下のコマンドでパスを通す。

$ export PATH=/root/anaconda3/bin:$PATH

これで、condaコマンドが認識されるようになった。

$ conda --version
conda 23.3.1

Anacondaで仮想環境の作成

※この段階でうまくいかない場合は、シェルを再起動する

仮想環境の作成
$ conda create -n 環境名 python=バージョン

$ conda create -n py310 python=3.10

仮想環境の起動
$ conda activate 環境名

$ conda activate py310

仮想環境の終了
$ conda deactivate 環境名

$ conda deactivate py310

作成済み仮想環境の一覧を表示

$ conda env list

作成済み仮想環境の削除

$ conda remove -n 環境名 --all

Jupyterのkernelに、作成した仮想環境を追加する

仮想環境を作成しただけでは、JupyterNotebookからkernelとして環境を選択することはできない。
利用可能なカーネルの表示

$ jupyter kernelspec list
Available kernels:
  python3    /root/anaconda3/share/jupyter/kernels/python3

ここに、先ほど追加した仮想環境のカーネルを追加する。対象の仮想環境をアクティベートした状態で以下コマンドを実行。

$ pip install ipykernel
$ python -m ipykernel install --user --name py310 --display-name "Conda Python 3.10"

Jupyter Notebook上でカーネルの切り替えができるようになった。

ref: https://linuxhint.com/conda-command-not-found/

10
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9