Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

Partner – Orkes – NPI EA (tag=Kubernetes)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

1. Introduction

Conda is a versatile tool for managing environments and packages across multiple languages and platforms. Therefore, its flexibility makes it an essential tool for developers working in diverse programming environments.

In this tutorial, we’ll configure Conda to activate automatically for each terminal session. To do this, we’ll add the necessary commands to the .bashrc file. Once configured, Conda will activate whenever a new terminal window opens.

2. Understanding the .bashrc File

The .bashrc file is a script that runs automatically every time a new terminal session is initiated. It typically resides in the home directory and is responsible for configuring the environment by setting up aliases, functions, and variables.

Next, let’s locate the .bashrc file in the home directory. We can use the ls command to list all items in a folder or directory, including hidden files:

$ ls -a ~
.              .cache     .fontconfig  Music     snap
..             .config    .gitconfig   Pictures  .ssh
.bash_history  Desktop    .gnupg       .pki      .sudo_as_admin_successful
.bash_logout   Documents  .lesshst     .profile  Templates
.bashrc        Downloads  .local       Public    Videos

The ls -a command lists all the items in the home directory, designated by the ~ symbol. From the output, we see that .bashrc is one of the hidden files inside our home directory.

Next, we can view the content of this file using the cat command:

$ cat ~/.bashrc
# ~/.bashrc: executed by bash(1) for non-login shells.
...
# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac
...
if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

We can add commands to this file that will run automatically whenever we initiate a bash session.

3. Verifying Conda Installation

First, we can check if Conda is already installed on our Linux system. Then, we’ll show how to easily install Conda.

3.1. Checking Conda Version

Let’s verify if Conda is installed by checking its version using the conda –version command:

$ conda --version
bash: conda: command not found

The output shows that the conda command wasn’t found on the system, meaning that Conda isn’t installed. Now that we’ve verified this, let’s proceed to install it.

3.2. Installing Conda

The first step is to download the Bash script for installing Conda from the Anaconda website:

$ wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
...
HTTP request sent, awaiting response... 200 OK
...
Saving to: ‘Miniconda3-latest-Linux-x86_64.sh’
Miniconda3-latest-Linu 100%[==========================>] 140.03M  6.49MB/s    in 20s     
2024-08-18 15:27:22 (7.09 MB/s) - ‘Miniconda3-latest-Linux-x86_64.sh’ saved [146836934/146836934]

Once downloaded, we’ll make the script executable, and then run it to install Conda:

$ chmod +x Miniconda3-latest-Linux-x86_64.sh
$ bash Miniconda3-latest-Linux-x86_64.sh 

Welcome to Miniconda3 py312_24.5.0-0

In order to continue the installation process, please review the license
agreement.
Please, press ENTER to continue
>>> 
ANACONDA TERMS OF SERVICE
...
To install conda's shell functions for easier access, first activate, then:
conda init
Thank you for installing Miniconda3!

We confirm the downloaded Bash script using the ls command, then use the chmod +x command to make the script executable, and finally use bash to run the script.

We press ENTER to accept all the default suggestions during installation, except for agreeing to activate the Conda environment during system boot-up.

3.3. Initializing Conda and Verifying Installation

After installation, let’s initialize Conda in our shell. This step ensures that Conda commands are available in every terminal session on our system:

$ conda init
no change     /home/gbenga/miniconda3/condabin/conda
...  /home/gbenga/miniconda3/etc/profile.d/conda.csh
modified      /home/gbenga/.bashrc
==> For changes to take effect, close and re-open your current shell. <==

After running the code, we’ll close and reopen our terminal so we can have access to all Conda commands.

Finally, we can verify that Conda is now installed on our system:

$ conda --version
conda 24.5.0

As is apparent from the output, the version of Conda installed on our system is 24.5.0, and now we have access to it from the terminal. Notably, we have this access because we initialized Conda as shown previously.

4. Creating a New Conda Environment

We’ll create a new Conda environment that automatically activates whenever a new terminal session is opened. For illustration, let’s create a Conda environment with the latest Python package pre-installed:

$ conda create -n jimmyenv python=3.12
Channels:
...
  environment location: /home/gbenga/miniconda3/envs/jimmyenv
  added / updated specs:
    - python=3.12
The following packages will be downloaded:
    package                    |            build
    ---------------------------|-----------------
    ca-certificates-2024.7.2   |       h06a4308_0         127 KB
    pip-24.2                   |  py312h06a4308_0         2.8 MB
    setuptools-72.1.0          |  py312h06a4308_0         2.9 MB
    ------------------------------------------------------------
                                           Total:         5.8 MB
The following NEW packages will be INSTALLED:
...
  zlib               pkgs/main/linux-64::zlib-1.2.13-h5eee18b_1 
Proceed ([y]/n)? y
Downloading and Extracting Packages:
setuptools-72.1.0    | 2.9 MB    | ########                              |  22% Retrying ...
...                                                     
Executing transaction: done
# To activate this environment, use
#  $ conda activate jimmyenv
# To deactivate an active environment, use
#  $ conda deactivate

Creating the environment comes with packages pre-installed. Conversely, we can update any of the pre-installed packages by specifying them during creation, like we did for the Python version.

5. Manually Activating Conda Environment

Let’s activate the Conda environment we created earlier, named jimmyenv, to confirm it works:

gbenga@gbenga:~$ conda activate jimmyenv
(jimmyenv) gbenga@gbenga:~$

As shown above, the command activates the jimmyenv environment we created initially with the conda command. The last line displays jimmyenv in parentheses, indicating the current environment we’re using on our terminal.

6. Activating Conda Environment Using .bashrc

First, we’ll explore configuring .bashrc to activate our newly created Conda environment named jimmyenv.

Then, we’ll explore how to activate the base Conda environment, which is the default Conda environment.

6.1. Activating the Created Conda Environment From .bashrc

We’ll configure the system to automatically activate jimmyenv whenever a new terminal session starts. For illustration, we’ll use nano, a text editor, to edit the content of the .bashrc file.

Here’s the Bash code we’ll be adding to the .bashrc file:

# Activate Conda environment
if [ -f ~/miniconda3/etc/profile.d/conda.sh ]; then
    . ~/miniconda3/etc/profile.d/conda.sh
    conda activate jimmyenv
fi

After adding the code, we can save and close the file. Then, we use the source command to make the changes available to all the terminal sessions.

$ source ~/.bashrc
(jimmyenv) gbenga@gbenga:~$

The source command initializes the jimmyenv environment as shown on the last line, indicating the current environment we’re in.

Finally, we confirm the name of the current Conda environment we’re using:

(jimmyenv) gbenga@gbenga:~$ conda info --envs
# conda environments:
#
base                     /home/gbenga/miniconda3
jimmyenv              *  /home/gbenga/miniconda3/envs/jimmyenv

When we use the conda info –envs command to obtain information about all Conda environments, the jimmyenv environment is marked with an asterisk. The asterisk indicates the Conda environment that’s currently in use.

6.2. Activating the Default Conda Environment From .bashrc

Similarly, we can activate the base default Conda environment from the .bashrc file by following the same step. However, the only change is to modify the Conda environment name to base:

$ cat ~/.bashrc
...
# Activate Conda environment
if [ -f ~/miniconda3/etc/profile.d/conda.sh ]; then
    . ~/miniconda3/etc/profile.d/conda.sh
    conda activate base
fi

Likewise, we can follow the same procedure we used earlier:

$ source ~/.bashrc
(base) gbenga@gbenga:~$ conda info --envs
# conda environments:
#
base                  *  /home/gbenga/miniconda3
jimmyenv                 /home/gbenga/miniconda3/envs/jimmyenv

As shown above, the asterisk indicates that the base environment is currently in use. Now, once we close and reopen a new terminal session, the base Conda environment will be activated automatically for the new terminal session.

7. Conclusion

In this article, we explored the essential steps to set up and activate Conda environments automatically using the .bashrc file. We also detailed how to configure the .bashrc file to automatically activate either a custom Conda environment or the default base environment whenever a new terminal session begins.

By following these steps, we can streamline our workflow, ensuring that the right environment is always active without manual intervention.