{"id":44,"date":"2024-03-31T15:16:32","date_gmt":"2024-03-31T14:16:32","guid":{"rendered":"http:\/\/lerking.servehttp.com\/?p=44"},"modified":"2025-02-08T19:37:36","modified_gmt":"2025-02-08T19:37:36","slug":"setting-up-a-python-virtual-environment-in-linux-bash-terminal","status":"publish","type":"post","link":"https:\/\/blog.lerk.ing\/index.php\/2024\/03\/31\/setting-up-a-python-virtual-environment-in-linux-bash-terminal\/","title":{"rendered":"Setting up a python virtual environment in Linux bash terminal."},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">My development platform<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>MSI GL65 95D Laptop<\/li>\n\n\n\n<li>32 GB ram<\/li>\n\n\n\n<li>Linux Mint 22.1 Xia<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Python venv setup in my platform<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">February 8th, 2025 - Updated to python 3.13<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Open a terminal window.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To get the latest python builds I use the 'deadsnakes' ppa:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>someone@somewhere:~$ sudo add-apt-repository ppa:deadsnakes\/ppa<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now you can istall the python version you want a virtual environment for.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>someone@somewhere:~$ sudo apt install python3.13 -y<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After installation, cd into the location where you want to install your venv. I'm using my home folder.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>someone@somewhere:~$ cd ~<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>someone@somewhere:~$ mkdir venv<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>someone@somewhere:~$ cd venv<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>someone@somewhere:~\/venv$<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In some cases it will be neccesary to install the venv for a particular python version i.e.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>someone@somewhere:~\/venv$ sudo apt install python3.13-venv<\/strong> <strong>-y<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now we create the venv environment running Python 3.13, using the following command.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>someone@somewhere:~\/venv$ python3.13 -m venv p3.13<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This will create a folder named <em>p3.1<\/em>3 in the venv folder.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now you can activate this virtual environment with command.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>someone@somewhere:~\/venv$ source p3.13\/bin\/activate<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Likewise it can be deactivated with the command.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>(p3.13) someone@somewhere:~\/venv$ deactivate<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>someone@somewhere:~\/venv$<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, being a Linux geek, I like to make things easy for myself. I'm using <em>bash<\/em> as my default terminal enterpreter. So I'll show you how I can make activating a virtual environment easier.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Start by editing the .<em>bashrc or .bash_aliases<\/em> file in your home folder.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">At the end of the file add this.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>alias p313='source ~\/venv\/p3.13\/bin\/activate'<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now save the bashrc file. On the next launch of the terminal, this new alias command will be set.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now open a new terminal. Type in the alias command we added:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>someone@somewhere:~$ p313<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The terminal prompt will now show the virtual environment you're now working from.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>(p3.13) someone@somewhere:~$<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now whenever you use python in this terminal session, it will be the version installed here.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I can then switch between the different venv projects, using the alias commands.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I hope you have found this useful.<br>Please leave comments and or questions below.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Until next time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My development platform Python venv setup in my platform February 8th, 2025 &#8211; 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&#8217;s jump right into it. Open a terminal window. To get the latest python builds I use the &#8216;deadsnakes&#8217; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[17,16,18],"class_list":["post-44","post","type-post","status-publish","format-standard","hentry","category-programming","tag-bash","tag-python","tag-virtual-environment"],"_links":{"self":[{"href":"https:\/\/blog.lerk.ing\/index.php\/wp-json\/wp\/v2\/posts\/44","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.lerk.ing\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.lerk.ing\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.lerk.ing\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.lerk.ing\/index.php\/wp-json\/wp\/v2\/comments?post=44"}],"version-history":[{"count":3,"href":"https:\/\/blog.lerk.ing\/index.php\/wp-json\/wp\/v2\/posts\/44\/revisions"}],"predecessor-version":[{"id":223,"href":"https:\/\/blog.lerk.ing\/index.php\/wp-json\/wp\/v2\/posts\/44\/revisions\/223"}],"wp:attachment":[{"href":"https:\/\/blog.lerk.ing\/index.php\/wp-json\/wp\/v2\/media?parent=44"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.lerk.ing\/index.php\/wp-json\/wp\/v2\/categories?post=44"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.lerk.ing\/index.php\/wp-json\/wp\/v2\/tags?post=44"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}