How to Install Python 3 development environment for Ubuntu Linux

Andrei Maksimov

Andrei Maksimov

0
(0)

Python is a well-known dynamic, interpreted, and object-oriented high-level programming language. It is an easy-to-learn and well-optimized language which ultimately reduces software development and maintenance costs. Moreover, it comes with many modules and packages which facilitate software modularity and code reuse. Python is very popular among beginners and experienced developers because of its very easy-to-learn syntax. In this article we’ll cover how to install Python 3 development environment for Ubuntu Linux.

Python can be used for web and back-end development, cloud automation, data science, and many other things.

Here’s the list of the most important Python features for myself: 

  1. Giant vibrant community.
  2. Extremely large list of free 3rd party modules covering almost any need.
  3. Simplicity (compared to other programming languages such as C, C#, JavaScript, and Java).

Prerequisites

You must be logged in as a root or have sudo access (more information about sudo is available in our article How to allow the user to use sudo in Ubuntu Linux) on your system to install/upgrade Python on Ubuntu.

Installation Process

Usually, Python comes pre-installed in almost all Linux distributions.

To check the version of Python on your system, use the following command:

python3 --version
1. How to Install Python 3 development environment for Ubuntu Linux - python3 version

Command output shows that we have Python of version 3.8.5 in our system.

Installing Python

If you do not have Python in your Ubuntu system, all you need to do is to execute a couple of commands.

Installing Python 3 through APT is the simplest and straightforward method.

Get updates for system repositories packages:

sudo apt-get update
2. How to Install Python 3 development environment for Ubuntu Linux - apt-get update

Then, install Python3 with the following command:

sudo apt-get install python3 -y
3. How to Install Python 3 development environment for Ubuntu Linux - apt-get install python3

Now, check the installed Python version:

python3 --version
1. How to Install Python 3 development environment for Ubuntu Linux - python3 version

Upgrading Python

If you’d like to use the latest Python version (3.9), there are two ways to get it:

  • Installing Python 3.9 via APT using PPA repository.
  • Installing Python 3.9 using the source.

Using the second option is not recommended, as it would be very difficult to manage any dependencies and resolve software conflicts, so, we’ll not provide guidance for that option in this article.

Let’s begin Python upgrade to the latest version.

The first thing we need to do, is to update your system packages list.

In general, it is requirement step for any installation command (apt-get install):

sudo apt update
2. How to Install Python 3 development environment for Ubuntu Linux - apt-get update

Next, we have to install the dependency packages, that provide some useful scripts for adding and removing Personal Package Archive (PPA) repositories.

Note: PPA repositories allows application developers and Linux users to create their own repositories to distribute their own software. In Ubuntu world PPAs provide you an easy way to get a newer software versions that are not yet available at the official Ubuntu repositories.

But let’s continue:

sudo apt install software-properties-common -y
4. How to Install Python 3 development environment for Ubuntu Linux - apt-get install software-properties-common

Now, we’re ready to add the deadsnakes repository, which contains the most recent version of Python:

sudo add-apt-repository ppa:deadsnakes/ppa -y
5. How to Install Python 3 development environment for Ubuntu Linux - add-apt-repository deadsnakes

Finally, you can install the latest version of Python:

sudo apt-get install python3.9 -y
6. How to Install Python 3 development environment for Ubuntu Linux - apt-get install python3.9

Check the version of Python now:

python3.9 --version
7. How to Install Python 3 development environment for Ubuntu Linux - python3.9 version

The latest version of Python has been successfully installed on our system.

Note: the latest Python version has been installed as a separate Python distribution on your system. That means, that if you want to use Python 3.8 (default Python interpreter for Ubuntu 20.04), you need to call Python using python3 or python3.8 command. Use python3.9 to call explicitly the latest Python interpreter.

Python package manager

Python has it own package manager – pip, which allows you to simplify 3rd party dependencies management that may not be available in your system by default.

Let’s check if pip is already installed in our system:

pip3 --version
8. How to Install Python 3 development environment for Ubuntu Linux - pip version (not found)

We can see that pip has not been installed yet, so we have to install it.

To do that, run the following command:

sudo apt-get install python3-pip -y
9. How to Install Python 3 development environment for Ubuntu Linux - apt-get install python3-pip

Now, check pip’s version to validate that it was successfully installed:

pip3 --version
10. How to Install Python 3 development environment for Ubuntu Linux - pip3 version

Here we go, we’ve just installed the Python package manager (pip).

Virtual environments

So, at this point we have Python 3 interpreter and ability to install any Python packages using its package manager.

But it is not a good idea to install Python dependencies globally on your system, because you might need to use a different versions of the same dependency module in different Python programs.

That’s why, the next must-have tool for any Python developer is virtual environment manager (venv).

This tool or module allows you to set up an isolated Python environment and easily manage all the dependencies of your code in a separate folder.

Often, it is very useful to delete the virtual environment folder with all installed dependencies if you need to recreate your environment from scratch.

Installing venv

Let’s install venv module:

sudo apt-get install python3-venv -y
11. How to Install Python 3 development environment for Ubuntu Linux - apt-get install python3-venv

Once the venv module is installed, let’s take a look, how you need to use it.

For that, we’ll create a separate folder called environments, where we’ll store out Python virtual environments:

mkdir environments
cd environments
12. How to Install Python 3 development environment for Ubuntu Linux - virtual environments folder

Creating virtual environment

Let’s create a new environment called aws, where we’ll install boto3 library (the most well-known Python SDK library for AWS automation): 

python3 -m venv aws
13. How to Install Python 3 development environment for Ubuntu Linux - python3 -m venv aws

This command will create a folder aws with pip, isolated Python distribution and some helper scripts inside of it.

You may check what’s inside of this folder by using tree command:

tree aws
14. How to Install Python 3 development environment for Ubuntu Linux - virtual environment structure

Using virtual environment

To start using this environment, you need to activate it first:

source aws/bin/activate
15. How to Install Python 3 development environment for Ubuntu Linux - activate virtual environment

Pay attention that your shell command line now has (aws) prefix what indicates, that you’re using Python virtual environment named aws.

Now, let’s use pip to install boto3 module:

pip install boto3
24. How to Install Python 3 development environment for Ubuntu Linux - pip install boto3

Note: Python community is very dynamic and sometimes issues might happen during dependency installation. If you faced the following error during the installation:

error: invalid command 'bdist_wheel'
ERROR: Failed building wheel for boto3
16. How to Install Python 3 development environment for Ubuntu Linux - wheel dependency issue

Just install wheel dependency and re-run previous command again:

pip install wheel
pip install boto3
17. How to Install Python 3 development environment for Ubuntu Linux - wheel dependency issue fix

Try to import boto3 library to test the result:

python3 -c "import boto3"

It is working, great!

Now this environment is ready for AWS automation.

Saving virtual environment state

One of the most important feature of using virtual environments and pip together, is that you can easily save the information about all installed dependencies and their versions.

This allows you not only to control dependencies, but also easily recreate the same virtual environment on any other system.

Let’s save our environment state:

pip freeze > requirements.txt

Take a look at your requirements.txt file; it contains all your dependencies.

18. How to Install Python 3 development environment for Ubuntu Linux - pip freeze requirements

Now, let’s deactivate virtual environment:

deactivate
19. How to Install Python 3 development environment for Ubuntu Linux - deactivate virtual environment

Try to import boto3 library again:

python3 -c "import boto3"
20. How to Install Python 3 development environment for Ubuntu Linux - boto3 module not found

As you can see, the boto3 module has not been found.

Deleting virtual environment

To delete your virtual environment you have to delete aws folder:

21. How to Install Python 3 development environment for Ubuntu Linux - deleting virtual environment

Restoring virtual environment

Now, let’s recreate virtual environment from the saved state.

Create and activate aws environment again:

python3 -m venv aws
source aws/bin/activate
22. How to Install Python 3 development environment for Ubuntu Linux - recreating virtual environment - 1

Now, you can install all your dependencies in a bulk:

pip install -r requirements.txt
23. How to Install Python 3 development environment for Ubuntu Linux - recreating virtual environment - 2

Managing your dependencies in this way is especially useful when your application has lots of them.

Next steps

I strongly encourage you to build and activate the aws environment once more again, and try to find a way to list S3 buckets in your AWS account using boto3 library.

If you get stuck, try to find an example in our blog. It’s already there.

Now, you’re ready to go and start your Python 3 cloud automation.

Summary

In this post, we have covered how to install Python from scratch, Python upgrade to the latest version, Python package manager (pip), and venv module, which allows you to make separate isolated environments for your Python projects.

How useful was this post?

Click on a star to rate it!

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Top rated Udemy Courses to improve you career

Subscribe to our updates

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Want to be an author of another post?

We’re looking for skilled technical authors for our blog!

Leave a comment

If you’d like to ask a question about the code or piece of configuration, feel free to use https://codeshare.io/ or a similar tool as Facebook comments are breaking code formatting.