Setting up a python virtual environment in Linux bash terminal.

My development platform

  • MSI GL65 95D Laptop
  • 32 GB ram
  • Linux Mint 22.1 Xia

Python venv setup in my platform

February 8th, 2025 – Updated to python 3.13

I will be explaining the venv setup as off python 3.4. Tested and verified with the newest python 3.13. Let’s jump right into it.

Open a terminal window.

To get the latest python builds I use the ‘deadsnakes’ ppa:

someone@somewhere:~$ sudo add-apt-repository ppa:deadsnakes/ppa

Now you can istall the python version you want a virtual environment for.

someone@somewhere:~$ sudo apt install python3.13 -y

After installation, cd into the location where you want to install your venv. I’m using my home folder.

someone@somewhere:~$ cd ~

someone@somewhere:~$ mkdir venv

someone@somewhere:~$ cd venv

someone@somewhere:~/venv$

In this example I cd into my home directory designated by the tilde (~). Here I create a new folder (venv) for my venv. I can install as many virtual environments as I like.

In some cases it will be neccesary to install the venv for a particular python version i.e.

someone@somewhere:~/venv$ sudo apt install python3.13-venv -y

Now we create the venv environment running Python 3.13, using the following command.

someone@somewhere:~/venv$ python3.13 -m venv p3.13

This will create a folder named p3.13 in the venv folder.

Now you can activate this virtual environment with command.

someone@somewhere:~/venv$ source p3.13/bin/activate

Likewise it can be deactivated with the command.

(p3.13) someone@somewhere:~/venv$ deactivate

someone@somewhere:~/venv$

However, being a Linux geek, I like to make things easy for myself. I’m using bash as my default terminal enterpreter. So I’ll show you how I can make activating a virtual environment easier.

Start by editing the .bashrc or .bash_aliases file in your home folder.

At the end of the file add this.

alias p313=’source ~/venv/p3.13/bin/activate’

Now save the bashrc file. On the next launch of the terminal, this new alias command will be set.

Now open a new terminal. Type in the alias command we added:

someone@somewhere:~$ p313

The terminal prompt will now show the virtual environment you’re now working from.

(p3.13) someone@somewhere:~$

Now whenever you use python in this terminal session, it will be the version installed here.

Whichever modules you install from this terminal session, will only be installed in this virtual environment. So it functions more or less as a sandbox.

As said before, you can have as many virtual environments as you like. I like to test out new python versions in virtual environments. So what I do is download a new version i.e. python3.14. I then cd into my base venv folder and create a new venv project using python3.14. Then I add a new alias to the bottom of my .bash_aliases file.

I can then switch between the different venv projects, using the alias commands.

I hope you have found this useful.
Please leave comments and or questions below.

Until next time.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *