Chen Li


Conda 101

This article is a quick guidance to use Anaconda & Miniconda on Windows and Linux. I wrote it so that I can check the command myself.

§1 Anaconda

For Windows: Go the the official website Anaconda and download the default exe file. Open and install it.

Get familiar with Jupyter Notebook. Feel free to check out some other articles and mess around, knowing that you can always delete the old environment and start from scratch, you’ll be fine. And that, is the most important message from this article.

§2 Miniconda

For Windows: Go the the official website Miniconda and download the default exe file. Open and install it.

For Linux: Use the commands shown in the website to download and install it. A few commands that could useful in a Linux system when conda init <SHELL_NAME>:

  • To see shell type:

    echo $SHELL
    

    most Linux systems use bash, so in this case conda init <SHELL_NAME> would be:

    conda init bash
    
  • To close and restart the shell (for bash):

    source ~/.bashrc
    

§2.1 Commands

If you want to install new packages, such as TensorFlow, it’s necessary to create a new environment so that these environments don’t conflict, the official documents are always useful, and some commonly used commands after you opened the Anaconda Prompt (miniconda3) are:

  • See the environments created:

    conda info -e
    # or
    conda info --envs
    
  • Create a new environment, such as envName (the name can be something you prefer):

    conda create -n envName
    # remember to change the name!
    # or
    conda create --name envName
    # with python=3.7
    conda create -n envName python=3.7
    
  • Activate a certain environment, such as envName

    conda activate envName
    
  • Deactivate the current environment:

    conda deactivate
    
  • Delete a certain environment, such as envName:

    conda remove -n envName --all
    
  • Install packages, such as numpy, matplotlib, torch, torchinfo, jupyterlab:

    pip install numpy
    # or
    pip install numpy -i https://mirrors.aliyun.com/pypi/simple/
    # 临时从镜像阿里云下载
    
  • See the packages installed in the envName environment:

    conda activate envName
    conda list
    # or
    pip list
    
  • Export the list of the packages installed in current environment as a .txt file:

    conda list --export > requirements.txt
    
  • Export the list of the packages installed in the envName environment as a .yaml file with full environment information:

    conda env export --name envName --file requirements.yaml
    
  • Clear cache (Remove index cache, lock files, unused cache packages, and tarballs):

    conda clean --all
    

§2.2 Install Jupyter Notebook 7 in Miniconda

Jupyter Notebook 7 looks a lot like JupyterLab. After v7.0.0a, v7.0.0b and so on, v7.0.1 is released on 31 July 2023. What I like best about Jupyter Notebook 7 is Table of Content, few buttons on the upper-right of each notebook block, and the Visual Debugger that can run code line by line and show variables.

What’s worth mentioning is that installing Jupyter Notebook 7 will install JupyterLab too, so you might as well just install JupyterLab.

  1. Since the base environment of Miniconda is kind of empty, you can install Jupyter Notebook on a new environment.

    conda create -n jupyter
    activate jupyter
    pip install notebook
    
  2. Open Jupyter Notebook:

    activate jupyter
    jupyter notebook
    
  3. For Windows: After this, there should be a jupyter-notebook.exe file in .\miniconda3\envs\jupyter\Scripts folder. Now run

    jupyter notebook --generate-config
    

    You will see a jupyter_notebook_config.py file in C:\Users\yourUserName\.jupyter folder. Then find this line:

    # c.ServerApp.notebook_dir = ''
    

    Change this line into:

    c.ServerApp.notebook_dir = 'C:\Folder\You\Like'
    

    Next time you can double click jupyter-notebook.exe and it will open Jupyter Notebook in C:\Folder\You\Like folder. Now you can create a start menu shortcut for this .exe file or something. (And there should be a jupyter-lab.exe in the .\miniconda3\envs\jupyter\Scripts folder, you can open it as well.)

  4. If you want to change to other environments such as envName when using Jupyter Notebook:

    conda activate jupyter
    conda install nb_conda_kernels
    conda activate envName
    conda install ipykernel
    python -m ipykernel install --user --name=envName
    
  5. Delete the envName environment in Jupyter Notebook:

    conda activate jupyter
    jupyter kernelspec uninstall envName
    
  • Convert notebook.ipynb to .py:

    jupyter nbconvert --to python notebook.ipynb