Iron Attorney
Iron Attorney

Reputation: 1070

How to decide whether to use miniconda on ubuntu terminal startup

I was trying to install miniconda on ubuntu in a way that doesn't disrupt python3 unless I want it to. This doesn't seem easy from the installation shell script. The installation (if you let it) adds this to your ~/.bashrc file (which sets up your terminal environment when you open a new terminal session):

# # >>> conda initialize >>>

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

# <<< conda initialize <<<

This forces every terminal session to use minicondas python executable and environment. How do you make it optional?

I found a way and thought I'd share...

Upvotes: 0

Views: 340

Answers (2)

Iron Attorney
Iron Attorney

Reputation: 1070

ALTERNATIVE: Add new custom terminal launcher instead

As I promised, I have discovered how to make a new terminal launcher that will automatically initialise miniconda and leaves your normal terminal launcher unsullied.

FIRST create a new file directly in your home folder to be used to initialise newly launched miniconda terminals.

I've called mine ".bashrc-conda" which will neatly stick it next to your standard bash shell initialisation file ".bashrc" and make it hidden like the original. If you want to do this from a terminal, run this:

> ~/.bashrc-conda

Add the following line to the top of the new file:

source ~/.bashrc

This starts up the new shell with the usual initialisation procedure.

Then add the miniconda initialisation script to your new file ~/.bashrc-conda under the line we already added:

# # >>> conda initialize >>> 

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

# <<< conda initialize <<<

Now after the usual initialisation procedure, the conda intialisation will take place.

NOTE: If you allowed the minocnda installer to add the above script to ~/.bashrc, then make sure you also remove it from there so your regular terminal acts the way it did before miniconda arrived.

SECOND we need to add a new terminal profile...

Open any terminal window, click the menu button (the three horizontal lines) and click on preferences.

In the left column, click the "+" symbol next to Profiles and enter the name of your new miniconda profile, for this example we'll assume you used "Conda".

Again in the left column, click the newly created profile "Conda" and select the "Command" tab.

Tick the checkbox "Run a custom command instead of my shell" and enter this in the "Custom Command" text box below:

bash --rcfile ~/.bashrc-conda

Close the preferences.

You now have a terminal profile that launches a shell using our custom .bashrc-conda script.

Last create the new desktop widget that will launch your customised terminal.

Create a new file in "~/.local/share/applications". If you can't see it in your file explorer you need to tick the "Show hidden files" option. For our example we'll call this file "terminal-conda.desktop". If you wish to create this file in the terminal, run this:

> ~/.local/share/applications/terminal-conda.desktop

Copy and paste this into that file:

[Desktop Entry]
# VERSION=3.32.1
Name=Conda Terminal
Keywords=shell;prompt;command;commandline;cmd;
Exec=gnome-terminal --profile="Conda"
Icon=gnome-terminal
Type=Application
StartupNotify=true
Actions=new-window;preferences;

[Desktop Action new-window]
Name=New Window
Exec=gnome-terminal --window

[Desktop Action preferences]
Name=Preferences
Exec=gnome-terminal --preferences

Once you save this, you should be able to search and find the application "Conda Terminal" in the usual way. This new "application" just launches a gnome-terminal window using our new Conda profile, which as meantioned above launches bash with our custom .bashrc-conda file. If you can't see it, then there is a mistake in the file we just created: "~/.local/share/applications/terminal-conda.desktop". One thing to check is that there is no trailing whitespaces on any of the lines.

Assuming you can see it, clicking on it should launch your terminal with the extra miniconda initialisation script. You can "Add to favourites" to add it perminantly to the left hand launcher bar (what's that called again in ubuntu?).

CURRENT ISSUE

Currently the new terminals are added to the list of windows associated with the regular terminal widget in the left hand launcher bar. If anyone knows how I can force them to open under the new modified terminal launcher widget, that would be excellent.

Upvotes: 0

Iron Attorney
Iron Attorney

Reputation: 1070

To give me a choice between default system python and miniconda, I altered the ~/.bashrc conda section to look like this instead:

# # >>> MODIFIED conda initialize >>>

# !! MOD !! ask user if they wish to use miniconda
read -p "Use miniconda for python3? [y/any other input]" condaAnswer
echo "$condaAnswer"
if [ "$condaAnswer" = "y" ]; then  

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

# !! MOD !! ask user if they wish to use miniconda
fi
# <<< conda initialize <<<

The mod at the begining:

read -p "Use miniconda for python3? [y/any other input]" condaAnswer
echo "$condaAnswer"
if [ "$condaAnswer" = "y" ]; then

This means that whenever you open a new terminal session, the first thing that happens is you get asked if you want the python envirenment to be miniconda or not. User enters 'y' and presses enter to use miniconda python envirenment. Any other input skips the conda initialisation.

The end of the modification is just the closing of the if statement:

fi

Hope this is useful to anyone else not wanting total conda takeover like me. My next thought is to create a terminal shortcut in the favourates that always does the conda initialisation so I can choose which terminal environment I use instead from the quicklaunch bar instaed. I will add details of that when I get to it, or if someone else has info on that, please post :)

Pete

Upvotes: 1

Related Questions