The preparation of a development environment is the first step in the learning of Python. I will cover the process of installing and configuring an environment for Python 3 on the Ubuntu LTS operating system. I will also show how to start and configure the venv virtual environment to avoid conflicts with other Python modules. Finally, to make it easier to write correct code, we will install an IDE editor, the VSCodium editor to be precise.
TABLE OF CONTENTS
Installing Python on Ubuntu LTS
Installing the latest Python version
Setting the default to the latest Python version
Installing Python on Ubuntu LTS
Checking Python version
Ubuntu and other versions of Linux based on Debian come with Python 3 installed. Before moving on, it’s important to ensure that the system packages are updated to the latest versions available:
sudo apt update && sudo apt upgrade -y
Now we can check the version of Python 3 installed on the system by running the command
python3 --version
or shorter:
python3 -V
This is how it looks to me on Ubuntu 22.04.2 LTS:
If we do not have Python 3 installed, we can install it with the command:
sudo apt install python3
Installing the latest Python version
If we need the latest version of Python, we can use the Deadsnakes PPA package, which is the easiest way to install Python on Ubuntu LTS. It also allows users to receive continuous updates, bug fixes and security updates.
The latest version of Python is Python 3.11.3, which was released on 5 April 2023.
First, we need to install the required dependency packages:
sudo apt install software-properties-common
We add PPA deadsnakes to the source list of the APT package manager:
sudo add-apt-repository ppa:deadsnakes/ppa
After adding the PPA, we can install Python 3.11.3 on Ubuntu using the command:
sudo apt install python3.11
Finally, we check that the new Python is installed:
python3.11 -V
This way we have the latest Python 3.11.3 installed
Setting the default to the latest Python version
After installing the latest version of Python, the python3 command can be set to point to the latest version of Python, rather than the older version as it does now.
This can be done using the alias command. The alias command allows you to create command shortcuts or override the default options for existing commands.
Let’s check what and where we have Python installed:
ls -ls /usr/bin/python*
So we create an alias for the python3 command to point to the latest installed version of Python 3.11.3:
alias python3=/usr/bin/python3.11
We now have the latest version of Python under the python3 command:
python3 -V
Unfortunately, when the system is shut down or rebooted, the alias settings are not retained. Therefore, we need to add our alias to the bash_aliases file to make the change permanent.
Using the nano editor, open the bash_aliases file:
sudo nano ~/.bash_aliases
We add our alias:
alias python3=/usr/bin/python3.11
We save the changes with Ctrl + o and exit the editor with Ctrl + x.
Finally, we activate the alias with the command:
source ~/.bash_aliases
venv – Virtual Environment on Ubuntu LTS
A virtual environment is an isolated place where different versions of Python and its modules can be installed and used without conflict with other installed modules. This allows us to work on different projects in different environments and avoid the problems associated with different conflicts between those projects.
The virtual environment venv is installed with the command:
sudo apt install python3-venv
If we have installed and are using Python 3.11, we install the venv virtual environment with the command:
sudo apt install python3.11-venv
For the purpose of learning Python, let’s create a new directory. Then navigate to it:
mkdir LearnPython
cd LearnPython
In my case, this is the LearnPython directory, where I will store virtual environments.
The virtual environment is created with the command:
python3 -m venv my_venv
I have named my environment my_venv, but any other name can be used.
Virtual environment activation:
source my_venv/bin/activate
This is the command line after enabling the virtual environment:
(my_venv) zibi@ubuntu:~/LearnPython$
The name of the environment my_venv, is in parentheses at the beginning.
We can also confirm that we are in a virtual environment by checking the location of the Python interpreter:
which python
The resulting location shows that Python is in our running environment.
/home/zibi/LearnPython/my_venv/bin/python
In a virtual environment, we can use the python command instead of python3 if that’s what we prefer. Outside the environment, we use only the python3 command.
If we want to leave the virtual environment, use the command:
deactivate
That’s how it looks to me:
IDE – Installing VSCodium on Ubuntu LTS
IDE (Integrated Development Environment) is a set of useful programs combined into a single graphical interface, giving you a range of useful tools to help you write correct code.
VSCodium is a clone of Microsoft’s popular Visual Studio Code editor. It looks and works exactly like VS Code, except that VSCodium doesn’t track users because it doesn’t include Microsoft’s tracking and telemetry add-ons.
To install VSCodium, we first add the repository’s GPG key:
wget -qO - https://gitlab.com/paulcarroty/vscodium-deb-rpm-repo/raw/master/pub.gpg | gpg --dearmor | sudo dd of=/usr/share/keyrings/vscodium-archive-keyring.gpg
Next, we add the repository itself:
echo 'deb [ signed-by=/usr/share/keyrings/vscodium-archive-keyring.gpg ] https://download.vscodium.com/debs vscodium main' | sudo tee /etc/apt/sources.list.d/vscodium.list
Now we update the system and install VSCodium:
sudo apt update && sudo apt install codium
Once installed, we can search for the editor in the list of installed applications and launch it: