Install Python 3.6 on Ubuntu 22.04 with Virtual Environment

Introduction

As a programmer, having multiple Python versions and creating an isolated virtual environment for your projects can be extremely useful. In this tutorial,

we will show you how to install Python 3.6 on Ubuntu 22.04 and set up a virtual environment using the built-in venv module.

Step 1: Update the Package Index

Before installing anything new, it’s always a good idea to update the package index. Run the following command in the terminal:

sudo apt update

Step 2: Prerequisites

sudo apt-get install -y make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev \
libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev \
libgdbm-dev libnss3-dev libedit-dev libc6-dev

Step 3: Download Python 3.6

wget https://www.python.org/ftp/python/3.6.15/Python-3.6.15.tgz
tar -xzf Python-3.6.15.tgz

Step 4: Compile Python Source

cd Python-3.6.15
./configure --enable-optimizations -with-lto --with-pydebug
make -j$(nproc) # nproc will give you the CPU cores
sudo make altinstall

Step 5: Check the Python Version

To check the installed Python version, run the following command in the terminal:

python3.6 --version

You should see the following output:

Python 3.6.x

Step 6: Create a Virtual Environment

Now that we have Python 3.6 installed, let’s create a virtual environment for our project. In the terminal, navigate to the directory where you want to create the virtual environment. Then, run the following command:

python3.6 -m venv myenv

This will create a virtual environment named “myenv” in the current directory.

Step 7: Activate the Virtual Environment

To activate the virtual environment, run the following command in the terminal:

source myenv/bin/activate

You should see the name of the virtual environment in the terminal prompt, indicating that the virtual environment is active:

(myenv) user@host:~$

Step 8: Deactivate the Virtual Environment

When you’re done working in the virtual environment, you can deactivate it by running the following command in the terminal:

deactivate

And that’s it! You have successfully installed Python 3.6 with a virtual environment on Ubuntu 22.04. By using virtual environments, you can keep your projects isolated and avoid version conflicts.

Reference links :

https://docs.python.org/3/library/venv.html

Leave a Reply