How to Install Miniconda on Ubuntu 24.04

Updated on December 20, 2024
How to Install Miniconda on Ubuntu 24.04 header image

Introduction

Miniconda is a lightweight distribution of Anaconda for Python machine learning and data science tasks. Miniconda only includes Conda and additional utility packages to support projects with specific requirements or storage limitations on a server.

This article explains how to install Miniconda on Ubuntu 24.04 and set up Conda virtual environments to manage Python packages on the server.

Prerequisites

Before you begin:

Install Miniconda

Miniconda is not available in the default package repositories on Ubuntu. Follow the steps below to download the latest installation script and install Miniconda on your server.

  1. Switch to your user's home directory.

    console
    $ cd
    
  2. Create a new miniconda3 project directory.

    console
    $ mkdir -p miniconda3
    
  3. Visit the Miniconda releases page and download the latest Miniconda3 installation script.

    console
    $ wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda3/miniconda.sh
    
  4. Run the miniconda.sh script using Bash to install Miniconda.

    console
    $ bash miniconda3/miniconda.sh -b -u -p miniconda3
    

    The above command installs Miniconda and creates a new base Conda environment for Python packages, dependencies, and libraries on your server.

  5. Activate Conda.

    console
    $ source miniconda3/bin/activate
    
  6. Verify that your shell prompt changes to the base Conda environment.

    console
    (base) linuxuser@Miniconda:~$
    
  7. Initialize Conda to add new configurations in your shell environment.

    console
    $ conda init
    
  8. Reload your shell configuration to activate Conda.

    console
    $ source ~/.bashrc
    
  9. View the installed Conda version.

    console
    $ conda --version
    

    Your output should be similar to the one below.

    conda 24.9.2
  10. View information about the active Conda environment, Python version, and channels.

    console
    $ conda info
    

    Output:

    active environment : base
    active env location : /home/linuxuser/miniconda3
            shell level : 1
       user config file : /home/linuxuser/.condarc
    populated config files : /home/linuxuser/miniconda3/.condarc
          conda version : 24.9.2
    conda-build version : not installed
         python version : 3.12.7.final.0
                 solver : libmamba (default)
       virtual packages : __archspec=1=x86_64_v3
                          __conda=24.9.2=0
                          __glibc=2.39=0
                          __linux=6.8.0=0
                          __unix=0=0
       base environment : /home/linuxuser/miniconda3  (writable)
      conda av data dir : /home/linuxuser/miniconda3/etc/conda

Manage Conda Environments

Conda environments allow you to isolate and manage specific packages, dependencies, and versions required in your projects. On your server, Conda supports one active environment at a time. Activating a new environment automatically deactivates the active environment. Follow the steps below to create and activate Conda environments to run specific Python packages on your server.

  1. Create a new myenv Conda environment.

    console
    $ conda create -n myenv
    

    Verify the myenv path, enter y, and press Enter when prompted to create the Conda environment.

    ## Package Plan ##
    
    environment location: /home/linuxuser/miniconda3/envs/myenv
    
    Proceed ([y]/n)? y
    
    Preparing transaction: done
    Verifying transaction: done
    Executing transaction: done
  2. Activate the myenv Conda environment.

    console
    (base) $ conda activate myenv
    
  3. Verify that your Conda environment changes from (base) to (myenv).

    console
    (myenv) $
    
  4. Install specific packages in the myenv environment. For example, install Python 3.12, numpy and pandas to use in your project.

    console
    (myenv) $ conda install python=3.12 numpy pandas
    

    Enter y and press Enter when prompted to install the packages.

    Proceed ([y]/n)? y
    
    Downloading and Extracting Packages:
    
    Preparing transaction: done                               
    Verifying transaction: done                               
    Executing transaction: done  
  5. Create another myenv-2 Conda environment.

    console
    (myenv) $ conda create -n myenv-2
    
  6. List all available Conda environments on your server.

    console
    (myenv) $ conda env list
    

    Output:

    # conda environments:
    #
    base                     /home/linuxuser/miniconda3
    myenv                 *  /home/linuxuser/miniconda3/envs/myenv
    myenv-2                  /home/linuxuser/miniconda3/envs/myenv-2
  7. Deactivate the active myenv Conda environment.

    console
    (myenv) $ conda deactivate
    

    The above command deactivates the active myenv environment and reactivates the base environment as the active environment.

Run a Jupyter Notebook in a Conda Environment

Jupyter Notebook is an open-source interactive computing application that uses computational notebooks to execute code and run various applications. Jupyter Notebook is typically accessed in a web browser, but can also run in a terminal session using nbconvert and jupyter console commands. Follow the steps below to install the Jupyter Notebook package and run a notebook in your active Conda environment.

  1. Install the latest jupyter package version in your environment.

    console
    $ conda install jupyter
    

    Enter y and press Enter when prompted to install the package.

  2. View all installed packages in the Conda environment and verify that the new jupyter package is available.

    console
    $ conda list -n myenv
    

    Your output should be similar to the one below.

    .........
    jupyter                   1.0.0           py312h06a4308_9
    jupyter-lsp               2.2.0           py312h06a4308_0
    jupyter_client            8.6.0           py312h06a4308_0
    jupyter_console           6.6.3           py312h06a4308_1
    jupyter_core              5.7.2           py312h06a4308_0
    jupyter_events            0.10.0          py312h06a4308_0
    jupyter_server            2.14.1          py312h06a4308_0
    .......
  3. Create a new hello.ipynb example notebook using a text editor such as nano.

    console
    $ nano hello.ipynb
    
  4. Add the following contents to the hello.ipynb file.

    json
    {
     "cells":[
      {
         "cell_type":"code",
         "execution_count":2,
         "id":"5acd64e3-b2bc-41b0-8fc1-1666cb479f30",
         "metadata":{
    
         },
         "outputs": [
             {
              "name": "stdout",
         "output_type": "stream",
              "text": [
    
              ]
             }
            ],         
         "source":[
            "msg=\"Greetings from Vultr!\"\n",
            "print(msg)"
         ]
      },
      {
         "cell_type":"raw",
         "id":"7ed9e62d-6bc3-4116-b0e9-1b06814cc5f2",
         "metadata":{
    
         },
         "source":[
    
         ]
      }
     ],
     "metadata":{
      "kernelspec":{
         "display_name":"Python 3 (system-wide)",
         "language":"python",
         "name":"python3"
      },
      "language_info":{
         "codemirror_mode":{
            "name":"ipython",
            "version":3
         },
         "file_extension":".py",
         "mimetype":"text/x-python",
         "name":"python",
         "nbconvert_exporter":"python",
         "pygments_lexer":"ipython3",
         "version":"3.10.12"
      }
     },
     "nbformat":4,
     "nbformat_minor":5
    }
    

    Save and close the file.

    The above file creates a new notebook with two cells, a code cell and a raw cell using the Python version 3.10.12 as the kernel. The code cell creates a msg variable that prints a Greetings from Vultr! message to the standard output (stdout).

  5. Run the hello.ipynb notebook to execute all code cells and save the output to a hello.nbconvert.ipynb notebook in your environment.

    console
    $ jupyter nbconvert --to notebook --execute hello.ipynb
    

    Output:

    [NbConvertApp] Converting notebook hello.ipynb to notebook
    [NbConvertApp] Writing 1175 bytes to hello.nbconvert.ipynb
  6. View the hello.nbconvert.ipynb file contents and verify that it includes a Greetings from Vultr! message in the outputs section.

    console
    $ cat hello.nbconvert.ipynb
    

    Output:

    {
     "cells":[
      {
         ........
         "outputs":[
            {
               "name":"stdout",
               "output_type":"stream",
               "text":[
                  "Greetings from Vultr!\n"
               ]
            }
         ],
         "source":[
            "msg=\"Greetings from Vultr!\"\n",
            "print(msg)"
         ]
      }
      .........
    }  

Conclusion

You have installed Miniconda on Ubuntu 24.04 and created Conda environments to manage specific packages on the server. You can create multiple Conda environments to isolate specific projects, install Python packages and libraries to run on your server. For more information and configuration options, visit the Miniconda documentation.