Install Python in Ubuntu



Python is a powerful, open-source and easy to learn programming language. It is one of the most popular modern programming languages with a huge community of developers and extensive documentation.

It usually comes pre-installed on all latest Linux systems as it is one of the building blocks of many operating system tools. Though, based on user requirement, one can install specific version of Python from available sources.

In this tutorial, we will show you two ways to install Python on an Ubuntu system ?

  • Installing Python using package manager (apt)
  • Installing Python using its source code

Using Package Manager (apt)

On Ubuntu, open a Terminal window (or connect to the system via SSH) and execute below commands ?

$ sudo apt update
$ sudo apt install python3

These commands will update your system's repository data and install the available Python 3.xx package, if not already installed.

$ sudo apt install python3
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
python3 is already the newest version (3.10.6-1~22.04.1).
python3 set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 3 not upgraded.
$

If you need a specific version of Python, specify its version in the command as ?

$ sudo apt install python3.11
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
   libpython3.11-minimal libpython3.11-stdlib python3.11-minimal
Suggested packages:
   python3.11-venv python3.11-doc binfmt-support
The following NEW packages will be installed:
   libpython3.11-minimal libpython3.11-stdlib python3.11 python3.11-minimal
0 upgraded, 4 newly installed, 0 to remove and 3 not upgraded.
Need to get 5679 kB of archives.
After this operation, 20.9 MB of additional disk space will be used.
Do you want to continue? [Y/n]

If the specific version of Python is not available from the repository, you can use the next method to build it from the source.

Installing Python Using its Source Code

Installing Python from its source code is a comparatively complex task and needs some specific build tools and its source code for the installation to succeed.

Ensure required dependencies are installed on the system for the Python build. Run the following command in a terminal/SSH session to do so ?

$ sudo apt install wget build-essential libreadline-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev

Next, to download its source code, go to the Downloads section on Official Python website (python.org)


On the next page, click on Linux/UNIX link as shown below ?


It'll open Python Source Releases page where you can find different Python releases, released over time (including developmental releases), and their source code available as Gzipped and XZ compressed formats.


Copy the link of the source code (we have chosen Gzipped format for Python 3.13.0) of your required Python version and in a Terminal window (or SSH), enter the following wget command to download the source code ?

$ wget https://www.python.org/ftp/python/3.13.0/Python-3.13.0.tgz

Extract the downloaded Gzipped source code using the following command ?

$ tar xvzf Python-3.13.0.tgz

In the extracted directory, run configure command to enable optimizations as shown below ?

$ cd Python-3.13.0/
$ ./configure --enable-optimizations

To start the build process, use the following command ?

$ sudo make install

The above command takes some time to complete depending upon system speed and resources and may throw a few warnings during the build process. If it encounters a dependency issue or any other error, it'll likely fail. Review the logs and fix the error first before re-running the build command again.

If the build process is successful, you should be able to verify the Python version with given command ?

$ python3.13 --version

Conclusion

Python is one of the popular programming languages used in building several system and user level applications and tools. It is required both for system to function normally as well as if you wish to start developing code on it.

In this tutorial, we explained two common ways to install Python using its repository or its source code. Usually, installing via repository is sufficient for most user requirements though for specific needs, one may need to directly build from its source code.

Updated on: 2024-11-21T13:04:08+05:30

302 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements