Install Python on Ubuntu 19.04/18.04 using command terminal

The latest stable version of the Python programming language for Ubuntu 19.04 Linux to install while writing this tutorial article was 3.7.

For those don’t know about the Python, it is a computer programming language. An object-oriented dynamically typed language originally designed to write automated scripts (shells) that are increasingly being used for stand-alone, large-scale project development as versions are continually updated and new language features are added.

For Ubuntu 19.04 or 18.04 three Python versions are available by default that are:

  • python3 # version 3.7.3-1,
  • python # version 2.7.16-1
  • python-minimal # version 2.7.16-1

Note: Ubuntu 19 or 18 by default has Python 3.7.3, in case you want to change the version or don’t have pre-install Python then follow the below steps. This tutorial is also applicable for Linux Mint, Debian, Elementary OS and other same streams.

Step 1: Open Command terminal

To install Python on Ubuntu, go to Applications and search to open the command terminal. Alternatively, you can simply use the Keyboard shotcut i.e CTRL+Alt+T.

Step 2: Update the system

Before installing or removing any packages, once run the update command:

sudo apt-get update

Step 3: Install Python on Ubuntu

If you don’t have the latest version on your Ubuntu 19.04 i.e Python3 then use the below command to install:

sudo apt install python3

For those want to install some previous version of Python on their Ubuntu Linux OS they can use the below commands:

For Python 2.7.x version

sudo apt install python

For Python minimal version of 2.7

sudo apt install python-minimal

Use Apt repository to install Python

To install any previous Python version such as Python 2.3 – Python 2.6, Python 3.1 – Python3.4, Python 3.6 – Python3.7… We can simply use Deadsnake repository, here is the command for that:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update

After adding the repo use Python installation command along with the version you want to install, for example:

sudo apt-get install python3.4

Switch between multiple Python versions

In case, your Ubuntu or Debian system has installed with multiple Python versions and you want to set some particular version as default. Then you can switch among them.

We have four Python versions on our system, so first, we set priority for them:

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python2 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.4 2
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 3
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 4

Now, use the below command every time you want to set some particular version as default:

sudo update-alternatives --config python3

Switch between multiple Python versions

swtich versions of install python on Ubuntu

 Install Python using Source file on Ubuntu (optional)

If you are looking for some old version or some particular version of Python to be installed on Ubuntu or Linux Mint then you can simply compile it from the source. You can get archives each and every version of Python from its official website.

Here is the Python Source file download page.

For showing you how to compile any version of Python from its source on Ubuntu 19.04/18.04/1604 or Linux Minit or Debian, here we are downloading Python 3.4 version compressed source Tarball file.

To download Python Source file we use WGET command:

Right click on the Python Tarball file given on the website and copy the link and after that use it with wget in your terminal:

For example, we have copied the link of Python 3.4 tarball file…

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

Extract Tarball File

After downloading type:

ls

and then see the name of the downloaded file, it will be like this Python-x.x.x.tar.zx

x.x.x will be the version as in our case it was 3.4.0, thus our filename was Python-3.4.0.tar.xz

Use the below command to extract tarball file:

tar -xf  {file name}

tar -xf Python-3.4.0.tar.xz

Navigate to the folder and run configure script

After extracting your downloaded Python folder, first, switch to that directory for that

cd {directory name}

In our case:

cd Python-3.4.0

As you inside the directory, run the Configure script to check all the required dependencies before finally compiling the source:

./configure --enable-optimizations

Note: –enable-optimizations option will optimize the Python for your system.

Compile Python on Ubuntu

To compile python you can use the command make, however, to do it as per your computer CPU core resources, we use the flag -j with it. For that first, you should know about your PC cores.

To find PC cores type command

nproc

Now use the core number with -j flag. Thus the final command will be:

make -j 2

2 is the number of core we have on our system.

Finally, Install Python from compiled source

Note: In the below command we are not using standard installation command to install program from source i.e make install, becuase it will override your default pre-installed python version. Thus we use it with alt.

sudo make altinstall