Get Started with a Conda Environment

How to Install Miniconda on Ubuntu 22.04

Updated on August 28, 2023
How to Install Miniconda on Ubuntu 22.04 header image

Introduction

Conda is an environment manager and a package manager that's bundled into a single package. Environments are a desired way of having different versions of the same software on the same computer. They are necessary when different applications depend on different versions of a common dependency. An environment manager, such as Conda, creates different paths for each environment. An environment installs packages into and runs packages from only its path, which is different from the paths of other environments. Hence environments are independent of each other.

Conda distributes with Anaconda and Miniconda packages. In principle, Conda is not Python-specific, but it's commonly used in Python-based applications. Anaconda includes Conda, a Python runtime environment, and many packages useful in data-science applications, such as PyTorch, Transformers, and Numpy. Anaconda saves a lot of time you would spend on installing packages. Hence, projects that require a variety of software tools prefer Anaconda because it's uniquely suited to large projects and exploratory projects.

Unlike Anaconda, Miniconda only includes the most important utility packages that run on top of Conda and Python. It's preferable on projects that require fewer tools and remote servers with storage limitations.

Choose Miniconda if:

  • The project is small or needs only a few packages
  • You already know the tools that the project needs
  • You prefer to manually install the needed software
  • Your computer has storage limitations

This guide explains how to install and use Miniconda on a Vultr Ubuntu server.

Prerequisites

Before you begin:

This guide uses the example values, user pythonuser and the home directory /home/pythonuser/, replace all occurrences with your actual user account

Install Miniconda

Download the Miniconda installer script

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

To get the latest installer script, visit the Miniconda downloads page

Two methods are available when installing Miniconda. The default normal installation option requires you to manually accept the license agreement and specify the installation directory. In the second silent installation option, the installer automatically accepts the license agreement and uses the default installation directory.

Depending on your preference, install Miniconda using any of the optional methods below.

Option 1: Normal Installation

  1. Run the installation script

     $ bash Miniconda3-latest-Linux-x86_64.sh

    When successful, the installer displays an on-screen message as below:

     In order to continue the installation process, please review the license agreement.
     Please, press ENTER to continue
  2. Press Enter to continue and view the license agreement. Use the Up and Down cursor keys to browse through the agreement. When prompted to accept the license terms, enter yes to proceed

     Do you accept the license terms? [yes|no]
  3. Verify the installation directory:

     Miniconda3 will now be installed in this location: 
     /home/pythonuser/miniconda3    
    
     - Press ENTER to confirm the location   
     - Press CTRL-C to abort the installation   
     - Or specify a different location below

    If you don't specify a location, Miniconda installs to the user's home directory by default. For the user pythonuser, the default installation directory is /home/pythonuser/miniconda3

  4. Press Enter to install Miniconda to your desired directory and verify that the user has write access to it, else the installation breaks

  5. When successful, the installer downloads the compressed files into the installation directory and extracts them. When complete, enter yes to initialize Miniconda

     installation finished.
     Do you wish the installer to initialize Miniconda3
     by running conda init? [yes|no]
  6. When the installation is complete, end your SSH session and start it again to activate Miniconda

Option 2: Silent Installation

By default, the standard interactive way to install Miniconda involves a process of accepting options. Hence, the interactive method is not suitable for automatic installations using a script, and the batch mode solves this problem. The -b option runs the installer in batch mode and automatically accepts the license agreement to install Miniconda on your server, use the method as described in the following steps

  1. Run the installation script

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

    The above command installs Miniconda to your user home directory. For the user pythonuser, it's installed to /home/user/miniconda3

    To install Miniconda to a specific location, such as /var/miniconda, apply the -p option. For example:

     $ bash Miniconda3-latest-Linux-x86_64.sh -b -p /var/miniconda

    When the installation is complete, your output should look like the one below:

     Preparing transaction: done 
     Executing transaction: done 
     installation finished.
  2. To initialize Miniconda, run the activation script

     $ source /home/pythonuser/miniconda3/bin/activate
  3. Use the init command to initialize Conda before using it

     $ conda init
  4. To start using Miniconda on your server, end your SSH session and start it again to activate the base environment

Verify the Installation

  1. When Miniconda is successfully installed on the server, the system prompt changes with the (base) variable.

     (base) pythonuser@servername:~$ 
  2. Verify that the system PATH includes the Miniconda directory

     $ echo $PATH

    Output:

     /home/pythonuser/miniconda3/bin:/home/pythonuser/miniconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
  3. To view the list of programs installed by Miniconda, use conda list as below

     $ conda list
  4. To exit the Miniconda environment, deactivate the Conda environment

     $ conda deactivate 

Uninstall Miniconda

  1. When Miniconda installs to your user home directory. Delete the directory to start the uninstallation process

     $ rm -rf /home/pythonuser/miniconda3
  2. Delete the hidden Conda directory

     $ rm -rf /home/pythonuser/.conda
  3. Edit the .bashrc file and delete the following configuration lines

     # >>> conda initialize >>>
     # !! Contents within this block are managed by 'conda init' !!
     __conda_setup="$('/home/pythonuser/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
     if [ $? -eq 0 ]; then
         eval "$__conda_setup"
     else
         if [ -f "/home/pythonuser/miniconda3/etc/profile.d/conda.sh" ]; then
             . "/home/pythonuser/miniconda3/etc/profile.d/conda.sh"
         else
             export PATH="/home/pythonuser/miniconda3/bin:$PATH"
         fi
     fi
     unset __conda_setup
      # <<< conda initialize <<<

    Delete the entire section (the entire block of code between # >>> conda initialize >>> and # <<< conda initialize <<<)

You have uninstalled Miniconda from the server. End your SSH session and log in again to apply changes

Conclusion

In this guide, you have installed Miniconda on a fresh Ubuntu server using two optional installation methods, the regular and batch modes. Then, you uninstalled Miniconda completely from your system after use. For more information about Miniconda, visit the official documentation.