Python programming language is one of the most popular and versatile programming languages. It's a household name among experienced developers and is recommended for beginners given its easy-to-learn syntax.
Guido van Rossum a dutch programmer is the creator of Python. He is now hired by Microsoft, expecting more enhancement and speed in the coming version. As of writing this guide Python 3.10 is the stable release.
Python is used in a myriad of use cases including backend development, data science, artificial intelligence, and web development. It can do simple scripts to complex machine learning algorithms.
In this guide, we learn how to install Python on Ubuntu 22.04 and setup Python 3 programming environment.
- A server running Ubuntu 22.04 operating system.
- A non-root user with sudo privileges.
Python 3 on the Ubuntu system
Ubuntu 20.04 and later versions of Ubuntu systems such as Ubuntu 22.04 come with Python3 already preinstalled. The base installation of almost all Linux distributions now comes with Python preinstalled.
To set up Python3, it's prudent to, first and foremost, ensure that the local package index is up to date. So update Ubuntu as follows:
sudo apt update
Next, install the software-properties-common package.
sudo apt-get install software-properties-common
Next, upgrade all the installed packages on your system including security updates. To list all packages with pending upgrades, run the following command:
sudo apt --list upgradable
If you recently upgraded all the packages, this is not necessarily required. To upgrade the packages, run the command:
sudo apt upgrade
When the upgrade of the packages is complete, be sure to confirm the version of python installed on your system.
As of writing this guide Python 3.10.4 which is the default version that ships with Ubuntu 22.04.
With that out of the way, let's focus on managing Python Packages. Management of Python packages is handled by PIP. This is the defacto and recommended package manager for Python. We will use this to install Python packages in our python projects.
Python3 provides pip3. You can easily install it from the apt repository using the APT package manager.
To install pip on Ubuntu type:
sudo apt install python3-pip
To check the version of pip3 that is installed on the Ubuntu system, run the command:
The output below confirms that pip3 is installed.
pip 22.0.2 from /usr/lib/python3/dist-packages/pip (python 3.10)
To install a Python package, use the syntax:
pip3 install package_name
In this case, package_name refers to any python library or package. For example, to install pandas which is a Python library used for data analysis and machine learning, run the following command:
pip3 install pandas
Also, consider installing the following development tools for a robust setup of your environment.
sudo apt install build-essential libssl-dev libffi-dev python3-dev
Add deadsnakes PPA for the latest version of python
The apt repository provides Python 3.10. This is the current stable release of Python that takes over from Python 3.9. However, this is not the latest python version. There is a newer version - Python 3.11 which happens to be the latest version at the time of writing this guide.
To install the latest version of python, you need to add the deadsnakes PPA to your system.
The deadsnakes PPA is a PPA that lets you install multiple versions of Python and usually provides the latest python releases as well as older versions.
To add the PPA, run the command:
sudo add-apt-repository ppa:deadsnakes/ppa
Next, update the local package index to sync with the newly added PPA.
sudo apt update
Next, install the latest python version on ubuntu by specifying the Python version as follows
sudo apt install python3.11
Once the installation is complete, verify the version of python installed as follows.
The latest version helps maintainers of third-party Python projects to test and report issues.
Install Python 3.11 on Ubuntu from Source
The default version available on the Ubuntu system is well-suitable for your regular programming. Installing from the source provides the latest version, option for customization, security patch, or a new feature.
To install Python 3.10 from the source, go through the following steps.
Step 1: Install packages to build Python:
sudo apt install libgdbm-dev build-essential libnss3-dev libreadline-dev libffi-dev libsqlite3-dev libbz2-dev libncurses5-dev libssl-dev zlib1g-dev
Step 2: Download the source code from the Python download page:
Change to /tmp directory and download the Python version 3.11 beta version's source file using wget command:
cd /tmp wget https://www.python.org/ftp/python/3.11.0/Python-3.11.0b1.tgz
Step 3: Extract the gzipped compressed file
tar -xf Python-3.11.0b1.tgz
Step 4: Configure and Optimize Python
Let's make files ready for installation and optimize for speed code execution.
cd Python-3.11.0b1 ./configure --enable-optimizations
Step 5: Build Python from the source code
You may use -j option with the number of CPU cores that help speed up the build process.
Step 6: Install Python Binaries
You can install it in two methods:
altinstall or just
install. The altinstall will tell the system to keep the existing Python intact so you can use multiple versions at the same time. While the install method overwrites the existing Python.
sudo make altinstallor
sudo make install
Wait patiently for a while as this process would take some time to complete.
Unless configured the path when installation from source, the software is by default installed to /usr/local/ directory, executables in /usr/local/bin/ and data files in /usr/local/share/.
Step 7: Verify by checking the Python Version
Run the following command:
Setting Up a Virtual Environment for Python
In Python, a virtual environment is an isolated workspace on your server with its own Python interpreter, libraries, scripts, and dependencies that are isolated from other Python projects. It helps to keep the dependencies required by various Python projects separate.
Each virtual environment is typically a directory structure or folder containing a few scripts to provide the functionality of a virtual environment.
To create a virtual environment, we will use the venv module which forms parts of the standard Python 3 library. To install venv, run the command:
sudo apt install python3-venv
With that out of the way, we are going to create a new directory for the virtual environment using the mkdir command. We are going to name it virtual-env.
Thereafter, navigate into the directory.
Once inside the directory, create the virtual environment as follows.
python3 -m venv my_env
The command initializes the virtual environment and creates a few directories with Python libraries, scripts, and dependencies.
All the files contained in the directories work in tandem to ensure that your projects are set apart from the rest of your server working environment. This isolation ensures that project files don't get mixed up with the rest of the system files and software packages.
To activate the virtual environment,
Once activated, the command prompt will be prefixed by the environment's name.
The prefix indicates that the virtual environment is live and active and you can start creating Python applications or programs that will use the environment's packages and settings.
Create a hello world program
With our virtual environment in place, we will create a test application that will print out "Hello World" to the terminal window. Inside the virtual environment directory, create a new Python file
$ nano hello.py
Paste the following lines
Save the changes and close the text file.
Finally, run the Python program as follows:
$ python3 hello.py
In this guide, we learned how to install Python 3 on Ubuntu 22.04 and configured a virtual environment for your development projects. Happy coding!
Please feel free to send your suggestions and feedback in the below comment section.