Explore Miniconda3: a simple and flexible Python environment and software package management tool

Miniconda3 installation and configuration notes

Foreword

Miniconda3 is a lightweight Anaconda distribution that provides a flexible and simplified way to manage Python environments and packages. This article will introduce how to install, configure and manage Miniconda3, as well as use the conda command to manage the environment and software packages.

Article directory

  • Miniconda3 installation and configuration notes
    • Preface
    • outline
    • 1. Install Miniconda3
    • 2. Activate and manage the environment
      • 2.1 Activate the specified environment
      • 2.2 Return to the basic environment
      • 2.3 Create a new environment
      • 2.4 Delete environment
    • 3. Manage software packages
      • 3.1 Install software packages
      • 3.2 Uninstall software packages
      • 3.3 Update software package
    • 4. Manage software package environment
      • 4.1 View the list of installed packages
      • 4.2 Search for available package versions
      • 4.3 List all existing environments
    • 5. Add or delete channels (software sources)
      • 5.1 Add new software source
      • 5.2 Remove software source
    • 6. Environment export and import
      • 6.1 Export environment to YAML file
      • 6.2 Import environment from YAML file
    • 7. The difference between base environment and new environment
      • 7.1 Default environment and Python version
      • 7.2 Using Conda and pip
      • 7.3 Installed packages
      • 7.4 Isolation and Management Advantages
    • 8. `conda: command not found`
    • Summarize

Outline

  1. Install Miniconda3

    • Download Miniconda3 installation package
    • Run the installation script
    • Set installation path
  2. Activate and manage environments

    • Use conda activate to activate the specified environment
    • Use conda deactivate to return to the base environment
    • Use conda create --name [packages] to create a new environment
    • Use conda remove --name --all to delete the environment
  3. Manage packages

    • Use conda install to install the package
    • Use conda remove to uninstall the package
    • Update packages using conda update
  4. Manage package environment

    • Use conda list to view the list of installed packages in the current environment
    • Use conda search to search for available package versions
    • Use conda info --envs to list all existing environments
  5. Add or remove channels (software sources)

    • Use conda config --add channels to add new software sources
    • Use conda config --remove channels to remove software sources
  6. Environment export and import

    • Use conda env export > environment.yml to export the environment to a YAML file
    • Use conda env create -f environment.yml to import an environment from a YAML file
  7. The difference between base environment and new environment

    • Default environment and Python version
    • Conda and pip use
    • Installed packages
    • Isolation and management advantages

1. Install Miniconda3

Miniconda3 is a lightweight Anaconda distribution. You can download the installation package suitable for your operating system on the Miniconda official website. Select the corresponding version (for example, Windows, MacOS, or Linux) and download the installation package.

After the download is complete, run the installation script and follow the prompts to install. You can choose the default installation path or customize the installation path.

2. Activating and managing the environment

2.1 Activate the specified environment

Activating a created environment is very simple, just use the conda activate command. For example, to activate an environment named “myenv”, run the following command:

conda activate myenv

2.2 Return to the basic environment

To return to the base environment, that is, to deactivate the current environment, you can use the conda deactivate command:

conda deactivate

2.3 Create a new environment

Use the conda create --name command to create a new environment. For example, to create a new environment named “myenv”, run the following command:

conda create --name myenv

You can also specify which packages need to be installed when creating the environment:

conda create --name myenv numpy pandas

Alternatively, you can create an environment by specifying the Python version, and install additional packages:

conda create --name myenv python=3.10 numpy pandas

This will create a new environment called “myenv” and install packages such as Python 3.7, numpy, and pandas.

2.4 Delete environment

If you want to remove environments that are no longer needed, you can use the conda remove --name --all command. For example, to delete an environment named “myenv” and all its packages, run the following command:

conda remove --name myenv --all

3. Management software packages

3.1 Installing software packages

To install a specific package, use the conda install command. For example, to install the numpy package, run the following command:

conda install numpy

You can also specify the version number of the package to install:

conda install numpy=1.19.2

3.2 Uninstalling software packages

If you want to remove a package you no longer need, use the conda remove command. For example, to uninstall the numpy package, run the following command:

conda remove numpy

3.3 Update package

To update an installed package to the latest version, use the conda update command. For example, to update the numpy package, run the following command:

conda update numpy

4. Manage software package environment

4.1 View the list of installed software packages

To view the list of installed packages in the current environment, you can use the conda list command. Run the following command:

conda list

A list of all installed packages will be displayed, including their names and version numbers.

4.2 Search for available software package versions

If you want to find available software packages and their version numbers, you can use the conda search command. For example, to search for numpy packages, run the following command:

conda search numpy

Available version information related to numpy will be displayed.

4.3 List all existing environments

For multiple environments that have been created, you can use the conda info --envs command to list all existing environments. Run the following command:

conda info --envs

A list of all environments will be displayed, including their names and paths.

5. Add or delete channels (software source)

5.1 Add new software source

Sometimes, you may need to add additional software sources to get specific packages or updates. Use the conda config --add channels command to add new software sources. For example, to add a software repository named “mychannel”, run the following command:

conda config --add channels mychannel

5.2 Remove software source

To remove software sources that are no longer needed, use the conda config --remove channels command. For example, to remove a repository named “mychannel”, run the following command:

conda config --remove channels mychannel

6. Environment export and import

6.1 Export environment to YAML file

You can export an environment and its dependencies to a YAML file to recreate the environment elsewhere. Use the conda env export > environment.yml command to export the current environment to the environment.yml file. For example, run the following command:

conda env export > environment.yml

6.2 Import environment from YAML file

To import an environment from a YAML file, use the conda env create -f environment.yml command. For example, to import an environment from a file named environment.yml, run the following command

conda env create -f environment.yml

A new environment will be created based on the environment specification in the YAML file.

7. The difference between base environment and new environment

7.1 default environment and Python version

After Miniconda3 is installed, an environment named “base” will be created by default. This is the initial default environment, using the Python version that comes with Miniconda3.

When you create a new environment, you can choose different Python versions and other packages to suit different project needs.

7.2 Using Conda and pip

In the “base” environment, you can use the conda command and the pip command to install and manage software packages.

In a new environment, it is recommended to use the conda command to install and manage software packages to ensure the consistency of the environment and the correct handling of dependencies.

7.3 Installed software packages

The “base” environment may have some commonly used software packages pre-installed, while the new environment is blank and requires you to manually install the required software packages.

7.4 Isolation and management advantages

By creating different environments, you can achieve isolation between projects and ensure that their dependencies do not interfere with each other. This can avoid conflicts between different projects and improve the flexibility and reliability of software package management.

In addition, the process of creating and managing the new environment is clearer and simpler, allowing you to better control and maintain your project’s development environment.

# Add Miniconda’s bin directory to the PATH environment variable
export PATH="/path/to/miniconda3/bin:$PATH"

# Set up automatic activation of base environment
conda config --set auto_activate_base true

# Search for available Python versions
conda search "^python$"

#Install the specified version of Python
conda install python=desired_version

Please note that /path/to/miniconda3 needs to be replaced with the path where you actually installed Miniconda, and “desired_version” should be replaced with the specific Python version you want to install, such as 2.7, 3.6, etc.

In addition, after you type the above command, you need to execute the following command to make the above setting take effect:

source ~/.bashrc

In this way, Miniconda’s bin directory can be added to the system’s PATH environment variable, and the base environment can be automatically activated. Next, you can search for available Python versions through the conda search command and install the required Python version using the conda install command.

source ~/.bashrc is a command used to reload the current user’s .bashrc file. In Linux and Mac systems, .bashrc is one of the configuration files of the Bash shell. It contains commands and settings that need to be run every time the terminal is started.

After you modify the .bashrc file and save it, in order for the changes to take effect, you can use the source command to reload the file. After executing the source ~/.bashrc command, Bash will re-read the .bashrc file and apply the changes.

By running the source ~/.bashrc command, you can ensure that changes you previously made to the .bashrc file take effect immediately without restarting your terminal session.

8. conda: command not found

If the conda: command not found error occurs when running the conda config --set auto_activate_base true command, it may be because the Miniconda installation path has not been added to your system correctly. in environment variables.

Please try the following methods to resolve the issue:

  1. First, please confirm that you have successfully installed Miniconda and remember its installation path (for example: “/home/qqq/miniconda3”).

  2. Open a terminal or command prompt window and use a text editor to open your shell profile (for example, “.bashrc” or “.bash_profile” in bash).

    • On Linux and Mac, you can use the following command to open:

      nano ~/.bashrc
      
    • On Windows, you can use the following command to open:

      notepad.exe %USERPROFILE%\.bashrc
      
  3. Add the following lines at the end of the configuration file (assuming your Miniconda installation directory is “/home/qqq/miniconda3”):

    # Add Miniconda to system environment variables
    export PATH="/path/to/miniconda3/bin:$PATH"
    

    Please note that /path/to/miniconda3 needs to be replaced with the path where you actually installed Miniconda. If you have added a similar line to the configuration file before, be sure to add it only once.

  4. Save and close the configuration file.

  5. Now restart your terminal or command prompt window and run the conda config --set auto_activate_base true command again to see if you can find and execute the command.

In this way, you should be able to successfully set up the configuration to automatically activate the base environment.

If the above steps do not resolve the issue, try reinstalling Miniconda and make sure you choose to add it to your system environment variables during the installation process. If you have additional questions, please feel free to ask!

Summary

Miniconda3 is a powerful tool that can help us create, manage and switch different Python environments, and solve software package dependency issues. This article introduces the steps to install Miniconda3, as well as how to activate, create and delete environments, manage software packages, add or delete software sources, and export and import environments and other common operations. By mastering these basic knowledge and common commands, you will be able to use Miniconda3 more effectively and easily cope with the needs of different projects.