How to Install Python on Ubuntu Linux

ubuntu 3344434 640 1

This post shows students and new users steps to install the latest version of Python on Ubuntu Linux from repository or manually build it from source code.

Python, versatile and popular general-purpose programming language can easily be installed on Ubuntu via multiple methods. Whether you’re building and advanced program or creating a simple task in Python, you’ll need Python installed in order to use it.

With Python, you can do almost anything like writing simple or advanced scripts, build and program robots and complicated machineries, develop websites and many more. Python also lets you work quickly and integrates systems more efficiently.

The latest versions of Python come with many new features including, assignment expression — which assigns values to variables as part of a larger expression, position-only parameter, f-strings support and many more.

There are many ways to install Python on Ubuntu. Below are two methods once can use to install python on Ubuntu Linux.

How to install Python from Ubuntu repositories

The quickest way to install Python on Ubuntu is to install it from Ubuntu default repositories. Python is available in Ubuntu default repositories so all one needs to do is simply run the apt get command to install it.

However, the versions of Python in Ubuntu repositories may not necessarily be the latest versions. Ubuntu repositories do not always have the latest software builds.

To install Python from Ubuntu repository, run the commands below

sudo apt update
sudo apt install python

After installing Python above, run the commands below to see which versions of Python is installed.

python --version

That should output similar line as below with the version of Python installed. As you can see, version 2.7.18 is the current build of Python available in Ubuntu.

Python 2.7.18

How to install Python from source code on Ubuntu

If you want to install the latest versions of Python on Ubuntu, you will want to build it from the source code. Installing Python from source code gives you control of which version to install.

Before installing Python from its source code, you must first install some required packages that are needed to build Python from source. To get these packages installed, run the commands below:

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

After installing the above packages, go and download the latest release’s source code from the Python download page using the following wget command.

At the time of writing this post, 3.9.7 is the latest Python version. If you find a later version on the site, you can download it instead.

cd /tmp
wget https://www.python.org/ftp/python/3.9.7/Python-3.9.7.tgz

After downloading the package, run the commands below extract the file and install.

tar -xf Python-3.9.7.tgz
cd Python-3.9.7
./configure --enable-optimizations

Next start the building process using the make command. Replace the #4 with the number of CPU cores on your system for faster build time.

My machine has 4 CPU cores, so I use the make command with -j 4 option.

make -j 4
sudo make altinstall

Do not use the standard make install as it will overwrite the default system with python3 binary.

After that, Python version 3.9 should be installed and ready to use.

To test if the latest version Python (which is 3.9) is installed and ready to use, run the commands below

python3.9 --version

You should see an output similar to the one below:

Python 3.9.7

That’s how you install Python from its source

Conclusion:

This post showed you how to install Python on Ubuntu Linux. If you find any error above or have something to add, please use the comment form below.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.