How do i run python 3.7 on ubuntu?

As most know, Python 3.7 is the oldest currently security-supported Python version with many porting or upgrading to 3.8, 3.9, and most recently 3.10 with 3.11 in RC status. For some applications or frameworks on Ubuntu, you may need to install Python 3.7 on your system. Python 3.7 is currently being worked on for security releases until its end of life on the 6th month of 2023. However, it is advisable to upgrade to newer versions if you develop applications as soon as possible.

In the following tutorial, you will learn how to download the latest Python 3.7 and install the archive format or use a PPA with this version of Python on Ubuntu 20.04 LTS Focal Fossa using the command terminal.

Table of Contents

1

  • Update Ubuntu
  • Install Python 3.7 – PPA Method
    • Alternative – Nightly Builds
  • Install Python 3.7 – Manual Method
    • Download Python 3.7
  • Create a Virtual Environment
  • Install Python PIP 3.7
  • Comments and Conclusion

Update Ubuntu

Before you begin, run a quick update to ensure your system is up-to-date to avoid conflicts during the tutorial and good system maintenance.

sudo apt update && sudo apt upgrade

Install Python 3.7 – PPA Method

The first and easiest solution for Ubuntu users would be to import the “deadsnakes” team Launchpad PPA. Given that 3.7 will not see many updates besides urgent security upgrades, you should install the standard stable, but the nightly version will be discussed for those who prefer it; updates will most likely hit both simultaneously.

First, install the prerequisite for adding custom PPAs.

sudo apt install software-properties-common -y

Second, add the deadsnakes/ppa to your APT package source list with the following command.

sudo add-apt-repository ppa:deadsnakes/ppa -y

Once the repository has been imported, run an APT update to fresh your package manager to reflect the new imported PPA.

sudo apt update

You can now install Python 3.7 by executing the following code:

sudo apt install python3.7 -y

To verify the installation and Python 3.7 build version, perform the following.

python3.7 --version

Example output:

How do i run python 3.7 on ubuntu?

Optionally, you can install the following extras.

Install development headers for building C extensions:

sudo apt install python3.7-dev

Install the standard library (venv) module:

sudo apt install python3.7-venv

Install the standard library (distutils) module:

sudo apt install python3.7-distutils

Install the (2to3.3.7) utility as well as the standard library (lib2to3) module:

sudo apt install python3.7-lib2to3

Install the standard library (dbm.gnu) module:

sudo apt install python3.7-gdbm

Install the standard library (tkinter) module:

sudo apt install python3.7-tk

Alternative – Nightly Builds

For developers that require the latest nightly builds, the PPA has an additional branch for these builds. However, they should only be used by professionals and developers that require the use of such builds.

sudo add-apt-repository ppa:deadsnakes/nightly -y

If you have the default 3.7 stable by (deadsnakes/ppa), you can run the apt update command to upgrade the existing packages.

sudo apt update

Then upgrade the packages:

sudo apt upgrade

If you do not have Python installed, use the installation command.

sudo apt install python3.7 -y

If you want to roll back to the stable PPA. First, remove python 3.7.

sudo apt autoremove python3.7

Next, remove the Nightly build PPA.

sudo add-apt-repository --remove ppa:deadsnakes/nightly -y

Once done, update the APT repository list to reflect the removal.

sudo apt update

Now re-install Python 3.7; you will re-add the stable PPA if you remove it.

Install Python 3.7 – Manual Method

Download Python 3.7

Once you have the download link, use the wget command to download the Python 3.7 archive.

wget https://www.python.org/ftp/python/3.7.13/Python-3.7.13.tar.xz

Extract the Python archive. Remember to change the version number if you downloaded a newer one.

tar -xf Python-3.7.{version}.tar.xz

Optionally, move Python 3.7 to a proper destination such as the /opt/ directory.

sudo mv Python3.7.{version} /opt/

Now install the dependencies required to install Python 3.7.

sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev curl libbz2-dev -y

Navigate to the directory

cd /opt/Python3.7.{version}/

Now, run the ./configure –enable-optimizations command.

./configure --enable-optimizations --enable-shared

Note, The script performs several checks to make sure all of the dependencies on your system are present. The ./configure –enable-optimizations will optimize the Python binary by running multiple tests, which will make the build process slower.

Now that you have built and configured the environment, it is time to compile it with the command make.

make

A handy trick is to specify the -j <number of cpu> as this can significantly increase compiling speed if you have a powerful server. For example, the LinuxCapable machine has 6 CPUs, and I can use all 6 or at least use 4 to 5 to increase speed.

make -j 6

Once you have finished building, install Python binaries as follows:

sudo make altinstall

Note, it is advised to use the make altinstallcommand NOT to overwrite the default Python 3 binary system.

Next, after the installation, you need to configure the dynamic linker run-time bindings:

sudo ldconfig /opt/Python3.7.{version}

Note, do not skip this, or you will face issues. You will also need to replace the path with your directory name and version.

Confirm that Python 3.7 is installed and the build version by running the following command:

python3.7 --version

Example output:

How do i run python 3.7 on ubuntu?

Create a Virtual Environment

Python’s venv module is a virtual environment is a Python environment such that the Python interpreter, libraries, and scripts installed into it are isolated from those established in other virtual environments, and (by default) any libraries installed on your operating system, for example, those that are installed on your Ubuntu system to avoid clashing and disturbing your production environments.

To ensure Python 3.7 is installed correctly and functioning, create a quick Python project.

First, create the project directory and navigate to it:

mkdir ~/test_app && cd ~/test_app

Inside the project root directory, run the following command to create a virtual environment for the test name test_app.

python3.7 -m venv test_app_venv

Next, activate the virtual environment as follows:

source test_app_venv/bin/activate

After starting the virtual environment, you will now be in the shell prompt terminal. This will show the name of your environment that will be prefixed.

Example:

How do i run python 3.7 on ubuntu?

To exit the virtual environment, use the following command:

deactivate

For older versions of Python, it is advised to install PIP by downloading get-pip.py using the wget command.

wget https://bootstrap.pypa.io/get-pip.py

Next, install the file downloaded.

python3.7 get-pip.py

Once installed, it is a good idea to check for upgrades.

python3.7 -m pip install --upgrade pip

Example output:

How do i run python 3.7 on ubuntu?

Now verify the PIP 3.7 version installed with the following command.

pip3.7 --version

Example output:

How do i run python 3.7 on ubuntu?

Comments and Conclusion

In the tutorial, you have learned how to install Python 3.7 and create a virtual test environment on Ubuntu 20.04 LTS Focal Fossa desktop or server.

Overall, it is advised if you are in development to move to Python 3.10 in the future, but using Python 3.7 is still safe as the Python Software Foundation maintains 3.7 until the middle of 2023. Still, you must check for updates and re-install Python using the steps provided in the tutorial.

How do I open python 3.7 in Ubuntu?

Go to the download directory and decompress the source package: tar -zxvf Python-3.7.5.tgz..
Go to the new folder and run the following configuration, build, and installation commands: cd Python-3.7.5 ./configure --prefix=/usr/local/python3.7.5 --enable-loadable-sqlite-extensions --enable-shared make sudo make install..

How do I change python to 3.7 in Ubuntu?

Upgrade python 2.7 to 3.6 and 3.7 in Ubuntu.
Step 1:- Install ppa. This PPA contains more recent Python versions packaged for Ubuntu. ... .
Step 2:- Update packeges. Now, update your packages by running the following command. ... .
Step 3:- Upgrade python 2. x to python 3. ... .
PiP installation. Install pip by running the following command..

How do I install python 3.7 6 on Ubuntu?

Option 2: Install Python 3.7 From Source Code (Latest Version).
Step 1: Update Local Repositories. ... .
Step 2: Install Supporting Software. ... .
Step 3: Download the Latest Version of Python Source Code. ... .
Step 4: Extract Compressed Files. ... .
Step 5: Test System and Optimize Python. ... .
Step 6: Install a Second Instance of Python (recommended).

How do I run python 3 in Ubuntu?

How to run Python in Ubuntu (Linux).
Step1: Open your desktop like this..
Step2: Go for Files > Documents in the left hand side..
Step3: In documents, you can either go for a folder in which you want to save your program or directly make a program there itself..