Today we’re diving into the world of Conda, or more accurately, Miniconda. We’ll explore why it’s a superior alternative to pip for managing packages and environments in your Python development workflow.
What is Conda?
Conda is a robust package and environment manager designed to handle dependencies, virtual environments, and more. It allows you to install packages, binaries, and set environmental variables within isolated environments. These isolated environments ensure that different projects can use different package versions, settings, and more, preventing conflicts and streamlining development.
Today, we’ll specifically be focusing on Miniconda. Miniconda is a lightweight installer for Conda, Python, and core dependencies, providing access to the Anaconda repository packages without the overhead of the full Anaconda distribution.
Benefits of Using Conda
So why switch to Conda? Here are some compelling reasons:
- Compatibility: Conda enables you to run your software within isolated environments. This ensures your application always has the exact packages, variables, and settings it needs for consistent performance.
- Python Version Management: You can easily install and manage different Python versions within different environments, ensuring compatibility with various projects.
- Cleaner System: Unlike system-wide package installations using pip, Conda keeps all your packages and dependencies isolated within virtual environments. This prevents clutter and avoids conflicts between project dependencies.
- Streamlined Development: With clearly separated projects in their own virtual environments, it becomes easier to manage, differentiate, and develop your applications. This makes the overall development process more organized and efficient.
Getting Started
Before we begin, make sure your Ubuntu system and Python installations are up to date. While we won’t delve into Python updates through apt
in this post (as Conda will be managing that), knowing the system’s baseline can help troubleshooting.
Currently, the latest version provided by Ubuntu’s apt
is 3.10, however Conda is released with Python 3.10.8 which is a good starting point. While Python has progressed beyond this, we can rely on Conda for our Python versions.
Let’s head over to the Miniconda website to grab the latest installer for Linux. Copy the link for the shell script.
Back in our terminal, we’ll use wget
to download the installer:
wget <miniconda_installer_link>
Once the download completes, execute the installer. We’ll use sudo
so we can install Miniconda to /opt/miniconda3
sudo bash miniconda3-latest-Linux-x86_64.sh
Follow the prompts, accepting the terms and selecting /opt/miniconda3
as the install location.
The script will install Miniconda and offer to run conda init
. While we will accept, it won’t initialize for our user.
Setting up Conda for Your User
Because conda init
ran as root, it didn’t modify our user’s bashrc
. To remedy this we need to create a symlink in the /usr/bin
folder that will point to the conda
binary we just installed.
sudo ln -s /opt/miniconda3/bin/conda /usr/bin/conda
Now we can run conda init bash
to initialize our bashrc
file.
conda init bash
Next we will source ~/.bashrc
. You should see (base)
added to your terminal prompt which indicates you’re in the base environment. This base environment is not meant for specific projects, so we’ll want to create a dedicated environment instead.
source ~/.bashrc
Creating a Virtual Environment
Let’s create a new virtual environment using this command. We’ll also specify that we want python 3.9 installed in the environment.
conda create --name flask_app python=3.9
After confirming the install, we can activate the environment:
conda activate flask_app
We can verify we’re in the environment by running python --version
which should show 3.9 or by running which python
which should point to the environment in our user folder.
Installing Packages
Let’s say you want to install the popular web framework flask
in the virtual environment. We can easily install it using Conda.
conda install flask
If you want to search for available packages, use the following:
conda search flask
And If you want to see all the installed packages and grep for a specific one, use the following:
conda list | grep flask
With flask installed we can now start creating our application.
Creating a Sample Flask Application
Create a simple Flask app called hello.py
.
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "<p>Hello, World!</p>"
We can run this by using the following command:
python -m flask --app hello run --host=0.0.0.0
Now lets deactivate the environment with conda deactivate
and try and run the same command. It should say that no module flask can be found since it isn’t installed in the environment we’re currently using. This clearly shows how environments can contain different packages and are isolated from one another.
Customizing Environment Directory
By default, Conda stores environments in a hidden directory .conda
. If we wanted to change this default path, we need to create a directory and configure Conda to use it.
mkdir conda_envs
conda config --add envs_dirs ~/conda_envs
After this we can create a new environment with conda create --name test123
and Conda should now store it in the directory we just defined.
Using Requirements Files
When developing applications you typically work with specific package versions and you want to make sure that is consistent throughout your development process. The way we can ensure that we are using specific versions of packages is by using requirements files. Here is a basic example.
name: flask_app
dependencies:
- flask=2.1.3
The file defines a name and a list of dependencies which we want to be installed. Let’s update our current flask_app
using this file:
conda activate flask_app
conda env update --file requirements.yaml
This should update the environment so that flask 2.1.3 is installed and we can verify using conda list | grep flask
Creating Environment with Requirements
Alternatively, you can define packages from the get-go when creating an environment. For example:
conda create --name hello flask=2.1.3 python=3.9
This will create an environment hello
and also install flask
2.1.3 and python
3.9.
Conclusion
This was a brief introduction to how to use Conda. Conda allows you to split your environments into multiple virtual environments with their own dependencies without them interfering with each other. This can be incredibly valuable when you are deploying your applications. You can define different virtual environments that your applications rely on and be certain that you won’t have any dependency issues when deploying your application.
While Flask is just a basic example, this knowledge can be applied to larger applications as well. You just have different virtual environments and different packages inside of them, making your development life easier and more consistent.