Python is the encouraged programming language of both inexperienced beginners and experienced developers. Python is adaptable and versatile, and it does well at back-end development, data analysis, automation, scripting, and machine learning. This guide will set up a Python 3 development environment on your Ubuntu 22.04 server. Collaboration between development projects is supported and there are several benefits to programming on a server. Any Debian Linux distribution will work with the fundamental ideas presented in this lesson.
Table of Contents
Follow the same procedure for all Ubuntu-based distributions including PopOS, Kubuntu, Linux Mint, and Elementary OS.
Prerequisites
- You should have a non-root user on an Ubuntu 22.04 server with sudo permissions in order to finish this tutorial.
- Read our basic server setup guide to find out how to accomplish this configuration.
Update System Packages
Refresh system packages, The system packages must be updated to the most current version before continuing with the Python installation. Then, execute the following command into the terminal by pressing Ctrl + Alt + T:
sudo apt update
When asked for your password, type it in and wait for the package manager to fetch the most recent package lists.
Then, upgrade the packages you have installed on your system to the most recent versions:
sudo apt -y upgrade
The -y flag will confirm that we are valid with everything being installed, but depending on your version of Linux, you might also need to consent to other prompts as your system updates and improves.
Check if Python is Installed
Python on ubuntu 3 is included by default in Ubuntu 22.04. To determine the Python version on your system, enter:
python3 -V
The version number will be output to you in the terminal window. While this number may change, the result will look something like this:
Python 3.10.6
If you require another or many Python on Ubuntu versions on your system, you should build them from source.
Installing Python on Ubuntu from Source
Compiling Python on Ubuntu from source allows you to install the most recent Python version and customize the build parameters. However, you will be unable to maintain your Python installation using the apt package manager.
Python is 3.11 is the most recent version of the most recent major release. This version incorporates several performance improvements and new features, such as new standards library modules, new syntax, and built-in capabilities, and more.
The instructions below describe how to compile Python on ubuntu 3.11 from source. Change the version number in the commands below if installing a newer release.
1. First, install the libraries and dependencies required to build Python on Ubuntu:
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev
2. Using the wget
command, below to get the necessary Python package:
wget https://www.python.org/ftp/python/3.11.3/Python-3.11.3.tgz
3. Once the download is complete, extract the archive as follows:
tar -xf Python-3.11.3.tgz
4. Open the Python on Ubuntu source directory and execute the configure command. This script runs a series of tests to ensure that all dependencies are present on your system:
cd Python-3.11.3
./configure --enable-optimizations
The --enable-optimizations
option allows the Python binary to be optimized by running multiple tests. This slows down the building process.
5. Start the build process:
make -j 12
To reduce build time, change the -j
to the number of cores in your processor. By entering nproc
, you may find the number of cores in your system.
6. Once the build is finished, install the Python binaries by typing:
sudo make altinstall
We’re using altinstall
instead of install
since the later command would replace the system’s default python3 binary.
That’s it. The latest Python has been installed on your system and is ready to be used by executing python3.11
. To verify it, type:
python3.11 -V
The output will show the Python version:
Python 3.11.3
You can also read how to install PHP 8.2 on Ubuntu 22.04
Setting up a Virtual Environment
virtual environments, you may create a separate area on your server for your Python projects, allowing each of them to have a unique set of requirements without interfering with any of the others.
A programming environment can be set up to give users more control over Python on Ubuntu projects and the handling of various package versions. Particularly when working with third-party software, this is crucial.
There is no limit to the number of Python programming environments you can set up. Each environment on your server is essentially a folder or directory with a few scripts inside of it that causes it to function as an environment.
While there are several ways to create a programming environment in Python on Ubuntu, we’ll use the venv module, which is a component of the standard Python 3 library. Let’s install Venv by entering:
sudo apt install -y python3-venv
Now that this is set up, we can create environments. Let’s either select the directory where we want to store our Python development environments or make a new directory using the mkdir command, like in:
mkdir environments
Next, go to the directory where you’ll keep your development environments:
cd environments
You can create an environment by executing the following command after you are in the directory where you want the environments to live:
python3 -m venv my_env
Basically, pyvenv creates a new directory containing the following few contents, which we can see with the ls command:
ls my_env
output
bin include lib lib64 pyvenv.cfg share
The python on Ubuntu 22.04 sharing directory will contain Python Wheels, a built-package format for Python that can speed up software development by reducing the number of compilations required.
You must activate this environment before using it, which you can do by entering the command that invokes the activate script as follows:
source my_env/bin/activate
You’re environment’s name, in this case my_env, will now appear before your command prompt. Your prefix may alter somewhat depending on the Debian Linux version you are using, but the name of your environment in parentheses should be the first item you see on your line:
(my_env) cloudbooklet@ubuntu:~/environments
This prefix informs us that the environment my_env is now active, which means that any programs we write here will only make use of the parameters and components specific to this environment.
Note: You can choose to use the commands pip or python instead of pip3 inside the virtual environment. You must only use the python3 and pip3 commands if you use Python 3 on a system that is not part of an environment.
Your virtual environment is ready for usage once you’ve completed these procedures.
Create a simple hello world program
The installation is completed now ready, so let’s make a classic “Hello, World!” program. This will allow us to test our environment and, if we haven’t already, provide us a chance to learn more about Python.
We’ll start a command-line text editor like nano and make a new file to complete this:
(my_env) cloudbooklet@ubuntu:~/environments nano hello.py
In the terminal window, we’ll type our program once the text file is opened:
hello.py
print("Hello, World!")
By hitting CTRL + X, Y, and then ENTER, you can save the file and exit nano.
After exiting the editor and return to your shell, you can run the following program:
(my_env) cloudbooklet@ubuntu:~/environments python3 hello.py
The output from your terminal should look like this thanks to the hello.py program you wrote:
output
Hello, World!
Enter the command deactivate to exit the environment, and your current directory will be where you start.
This article is to help you learn about how to install Python on Ubuntu 22.04 and Setting up the Python 3 development environment on your Ubuntu Linux server. We trust that it has been helpful to you. Please feel free to share your thoughts and feedback in the comment section below.