How to Create a Python Virtual Environment on Ubuntu 24.04


Creating a virtual environment on a system is a crucial task that helps you manage your Python projects and dependencies. Each Python project may require different packages, and in some cases, those packages don’t play well together. Having a virtual environment created on a system will solve version issues, dependency conflicts, or permission hustles related to these Python packages.

If you are using the Ubuntu 24.04 system and have encountered issues working with Python packages simultaneously, read this tutorial to learn:

How to Create a Virtual Environment on Ubuntu 24.04

To create a virtual environment on Ubuntu 24.04, follow the below-given steps:

Step 1: Ensure Python is Installed

For creating a virtual environment, first ensure Python is installed on your system. By default, Python is installed on the Ubuntu system, you can verify it by running the below-given command:

python3 --version

Step 2: Install pip on Ubuntu

For managing Python packages on your system, you should also install pip, it can be done from the below-given command:

sudo apt install python3-pip -y

You can verify the pip installation on Ubuntu from the following command:

pip3 --version

Step 3: Install ven Package on Ubuntu

To create the virtual environment on Ubuntu, you are required to install the venv package on your system. Through this package, you will be able to create a virtual environment on the system; you can install the venv package on Ubuntu via the below-given command:

sudo apt install python3-venv -y

Step 4: Create a Virtual Environment on Ubuntu

To create a virtual environment on Ubuntu, navigate to the directory where you want to create a virtual environment. Then use the python3 interpreter followed by the -m flag and name of the virtual environment you want to create.

Here, we are creating the mytest_env virtual environment at the default Ubuntu location using the following command:

python3 -m venv mytest_env

Alternatively, you can also create a virtual environment using the virtualenv command followed by the environment name:

virtualenv virtual_env_name

Here, as a reference, we have created a virtual environment with the name myenv on the system:

Step 5: Activate the Virtual Environment

After creating the virtual environment, it’s now time to activate it so that you can use it. Activating the virtual environment will temporarily adjust your PATH and set up the shell so that you will be able to use the environment packages or settings. To activate the virtual environment, use the below-given source command:

source test_env/bin/activate

When you execute the above-mentioned command, your terminal prompt will change to the name of the activated environment:

Note: Ensure replacing the virtual environment name mytest_env with the virtual environment you created in Step 4.

Step 6: Test the Virtual Environment

To test the working of the virtual environment on Ubuntu, let’s install a Python package, numpy within the environment. For instance, we are installing numpy package in our created virtual environment using the following command:

pip3 install numpy

In this way, you can install Python packages locally, and it won’t interfere with your system-wide Python packages. You can run any other Python or pip commands inside this virtual environment and install your packages accordingly.

Step 7: Deactivate the Virtual Environment

After working with the virtual environment, deactivate the environment and go back to your original command prompt using:

deactivate

Bonus Method: How to Create a Python Environment on Ubuntu 24.04 Using Conda

Besides using the venv package to create a Python environment, you can also perform a similar process on Ubuntu using Conda. Conda is a versatile package manager used for installing software packages, dependencies, and environments, including Python packages.

To create a Python environment on Ubuntu 24.04 using Conda, follow the below-given steps:

Step 1: Download the Conda Script File on Ubuntu

First, you must download the official Conda script file on your Ubuntu system by using the following wget command:

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh

Step 2: Run the Conda Script File on Ubuntu

After downloading the Conda script, you can run it on Ubuntu by using the bash command followed by the Conda script name:

bash Miniconda3-latest-Linux-x86_64.sh

When you run the script, you must press Enter to continue the installation process:

Press the Enter button multiple times until you see the license terms option. At this point, continue the installation by accepting the license terms using the yes option. Then press the Enter button again to continue with the default location for installing Conda on the Ubuntu system.

At the final prompt, go with yes option to activate Conda on the startup:

This will install Conda on Ubuntu.

Close the terminal and reopen it again to enter the Conda base environment:

You can create your environment with Conda on Ubuntu using:

conda create --name env_name

Or, you can directly install Python packages on the base Conda environment. As a reference, we have installed scipy package through the pip command:

To deactivate the Conda environment, you can use the following command:

conda deactivate

For more details, about how to work with the Python environment with Conda on a user-created environment, read here.

Conclusion

Creating a virtual environment helps you solve the dependency issues associated with the Python packages. In this blog, a virtual environment is created on Ubuntu 24.04 by first ensuring Python and pip are installed on the system. Then installed the venv package from the apt command and used the python3 command with -m flag and virtual environment name. Later on, we activated the virtual environment and installed Python packages in the isolated environment. Apart from that, we have also discussed the bonus method to create a Python virtual environment with Conda on the Ubuntu system.

Categories