DEV Community

Raman Butta
Raman Butta

Posted on

Installing & Working with Python - in Ubuntu 24.04

This approach is based on my personal experience, and is one of many approaches you'll find online.

Step 1 : Downloading

Go to the Anaconda website and download the shell script (.sh file) of Miniconda.

Step 2 : Create a folder

Create a folder say myfolder at your suitable location.

mkdir myfolder
cd myfolder
Enter fullscreen mode Exit fullscreen mode

Paste the miniconda.sh file inside this folder (optional, but I did for convenience)

Step 3 : Install conda through the miniconda script

bash ~/miniconda.sh
# follow interactive prompts:
#  - accept license (yes)
#  - choose install location (default: ~/miniconda3) OK
#  - at the end it asks to initialize shell; say "yes" (or you can init manually later)
Enter fullscreen mode Exit fullscreen mode

After install, either restart terminal or source the shell rc:

# restart terminal or
source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Stop auto-activation of base (recommended) :

conda config --set auto_activate_base false
Enter fullscreen mode Exit fullscreen mode

Now base will not activate automatically on opening a new terminal.

To confirm proper installation of conda, you can run the following commands :

conda --version
which conda
Enter fullscreen mode Exit fullscreen mode

Step 4 : Create and set up a python environment with conda

I do not prefer to store my custom env inside the installation folder ~/miniconda3/envs/myenv. I want it inside my project myfolder folder, isolated from the system. So instead of conda create -n and conda install -n commands, I will use --prefix :

conda create --prefix ./myenv python=3.10
Enter fullscreen mode Exit fullscreen mode

Then activate the env :

conda activate ./myenv
Enter fullscreen mode Exit fullscreen mode

You will notice that the entire path to myenv gets prefixed before the terminal prompt in parenthesis, which looks bulky. To just keep the name myenv, run the following commands :

conda config --set env_prompt '({name}) '
# Then deactivate and activate myenv again
conda deactivate
conda activate ./myenv
Enter fullscreen mode Exit fullscreen mode

Then check the python version inside that env :

python --version          # should show Python 3.10
which python              # should show ~/YOUR-LOCATION/myenv/bin/python
Enter fullscreen mode Exit fullscreen mode

So you see that the system/OS has an inbuilt python installed, which I did not disturb. I wanted Python3.10 since that is the most supported version for ML applications. But I did not separately install it from the Python website. Instead it got auto-installed while creating the myenv with this python version.

Next, I installed some basic python packages within this activated env as per my needs. You can do what suits your needs.

conda install numpy pandas scipy scikit-learn matplotlib jupyterlab biopython -y
Enter fullscreen mode Exit fullscreen mode

Step 5 : Install and register an IPython kernel from myenv

I planned to use an IDE like VSCode or Jupyter Notebook. Since these IDEs detect kernels and not environments, this step is needed.

# Install ipykernel
conda install ipykernel -y
# Register the kernel with a readable name
python -m ipykernel install --user --name myenv --display-name "Python 3.10 (myenv)"
Enter fullscreen mode Exit fullscreen mode

Now myenv can act as a kernel for these IDEs.

Step 6 : Use python in your favourite IDE

Select the kernel in your IDE and start coding !!

N.B. Jupyter IDEs need to be launched from within myenv

jupyter notebook # if you want to use notebook
jupyter lab # if you want to use lab
Enter fullscreen mode Exit fullscreen mode

N.B. Regardless, I prefer Google Colab for most of my applications :)

Marimo : an alternative to Jupyter notebook

Marimo is a Jupyter notebook with each cell being somewhat logically connected to each other. That's way if you update the value of a variable in a cell and re-run it, related values in other cells will be auto-updated and auto-run. This is called reactive execution. Thus the notebook can act as a single python script or app and has an extension of .py instead of .ipynb.

After you've created your myenv environment, you can easily install and use marimo from within it.

conda activate ./myenv
conda install -c conda-forge marimo -y
Enter fullscreen mode Exit fullscreen mode

That's it. Then the installation comes with many inbuilt sample notebooks openable using the marimo tutorial command. Open this one and go through it :

marimo tutorial for-jupyter-users
Enter fullscreen mode Exit fullscreen mode

Creating and editing marimo notebooks

# create notebook
marimo new analysis.py

# open it
marimo edit analysis.py
Enter fullscreen mode Exit fullscreen mode

You can open multiple notebooks within different browser tabs as well.

Top comments (1)

Collapse
 
hammglad profile image
Hamm Gladius

Nice walkthrough, especially the env location and kernel setup steps. I've usually relied on pyenv + venv on Ubuntu instead of conda. Has anyone compared performance, disk use, or dependency headaches between this Miniconda approach and pyenv/venv on 24.04?