Table of contents
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:
- Giant vibrant community.
- Extremely large list of free 3rd party modules covering almost any need.
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.
Usually, Python comes pre-installed in almost all Linux distributions.
To check the version of Python on your system, use the following command:
Command output shows that we have Python of version 3.8.5 in our system.
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
Then, install Python3 with the following command:
sudo apt-get install python3 -y
Now, check the installed Python version:
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 (
sudo apt 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
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
Finally, you can install the latest version of Python:
sudo apt-get install python3.9 -y
Check the version of Python now:
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.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:
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
pip’s version to validate that it was successfully installed:
Here we go, we’ve just installed the Python package manager (
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.
sudo apt-get install python3-venv -y
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
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
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:
Using virtual environment
To start using this environment, you need to activate it first:
Pay attention that your shell command line now has
(aws) prefix what indicates, that you’re using Python virtual environment named
Now, let’s use
pip to install
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
wheel dependency and re-run previous command again:
pip install wheel pip install boto3
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.
Now, let’s deactivate virtual environment:
Try to import boto3 library again:
python3 -c "import boto3"
As you can see, the
boto3 module has not been found.
Deleting virtual environment
To delete your virtual environment you have to delete
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
Now, you can install all your dependencies in a bulk:
pip install -r requirements.txt
Managing your dependencies in this way is especially useful when your application has lots of them.
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.
In this post, we have covered how to install Python from scratch, Python upgrade to the latest version, Python package manager (
venv module, which allows you to make separate isolated environments for your Python projects.
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?