In the previous tutorial, we covered how to secure ssh access
and in this tutorial, we are covering the following points 1. What
is Python?
2. What
is Virtual Environment?
3. How
to Install Python
4. How
to Create a Virtual Environment for Python
What is Python?
Python is a versatile, very high-level programming
language known for its rich, extensive libraries and readability. Python code
is executed line by line by an interpreter and making it easy to develop and
debug. Python relies on object-oriented programming and makes code and its implementation easily organized. Now Python is used in a vast range of applications as
web development, Artificial Intelligence, data analysis and many more. Python
supports a large collection of libraries, frameworks and is a powerful tool for
lot of tasks. Python syntax is clear and easy to understand for beginners.
Python creates virtual environments to avoid conflicts
between projects regarding dependencies and libraries. For simple and basic nature of
software development is for Python to managing libraries and dependencies. But
for complex software projects such as an API, software development kit multiple
files, dependencies and packages are required and it is difficult to maintain
big development projects without isolation.
What is Virtual
Environment?
A Python virtual environment is an isolated space where
you work independently on a specific project without affecting other projects.
It is a tool that helps to work on different versions of Python in different
projects without any conflicts and the system Python which is installed as a
package at the time of operating system installation. Multiple virtual
environments can be created for multiple projects and every environment has its own pip to install libraries, a library folder and an interpreter for the Python version
to activate the environment.
Benefits
With this virtual environment your projects isolate
themselves from other projects. These environments reside within these projects and
does not interfere with other virtual environments and system system-installed Python.
Multiple virtual environments can be created for
different Python versions without any conflicts. You can download different
packages easily in these environments according to your requirement without
admin privileges.
You can replicate your application with others and easily
manage your application.
How to install Python
Mostly Python is installed on a newly installed operating
system as part of it. Sometimes you need to install a specific version for
your needs so the apt package manager is used to install Python. First you need to
update the system with the sudo apt update command then sudo apt install
python3.x where x is the minor version of Python. You can also install pip for
Python package management.
In this tutorial, we will install Python on Ubuntu 25.04
Server
Prerequisites
- A
system running Ubuntu (current version 25.04)
- A
user with sudo Access or Root Privileges
- A
Terminal Access
Check if Python is installed
on this system or notBefore the installation of Python, check whether it is
already installed on the Ubuntu server; for this run the following command.
#python3 or python3 --version
If Python is installed, the Output will show Python
version, license and help for more information otherwise error message as bash:
python3 command not found.
Installation of
Python on Ubuntu Server
Different methods to install Python on an Ubuntu server.
- Use APT: the most common method
to install Python from the Ubuntu repository
- From source code: download the latest
version of Python from the official Python website and install
- Use PPA: use third third-party repository deadsnakes to install Python
Method 1: Install Python
using APTThis method is easy to install Python by using the apt package
manager and the Ubuntu default repository.
Step 1: Update the
package Repository
To get the latest version of Python, update the package
repository first. To update the package repository, run the following command
#sudo apt update
Step 2: Install Python
The package information repository is updated and now
install the latest version of Python 3.13.3 by the command
# sudo apt install python3.13
Step 3: Installation
verification
Verify the installation is successful by using python3 --version.
Method 2: Python
Installation from Source Code
First, download the latest version of the Python source
code from the official website and compile it. This is a little complicated but
following the right steps is not too difficult.
Step 1: Update the
repository
Update the local repositories
#sudo apt update
Step 2: Install
Supporting Libraries
Additional libraries are required to compile the source
code. Run following command to install these libraries.
#sudo apt install build-essential zlib1g-dev
libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev
wget
Step 3: Download the
latest version of the Python Source Code
Download the source code from the official website of Python
in /opt directory. This is your choice as you use /tmp directory for this
purpose or any directory of your choice.
#cd /opt
Use the wget command to download the selected Python
source code to the installation destination which is /opt.
#cd /opt
Step 4: Extract the Source
code
To extract the downloaded source code, run the following
command
#sudo tar –xvf python-3.13.5.tgz
Replace the version number with your downloaded version before
running the command.
Step 5: Configure
Python
Switch to the directory that is extracted and run the
configure command
#cd Python3.13.5
#sudo./configure –enable-optimizationsThe ./configure command prepares and evaluates the Python
installation and optimizes the speed of the code execution by enabling the optimization option.
Step 6: Install
Python
If the configure command is successful then run the sudo
make install command to install Python.
#sudo make install
Step 7: Verify
Installation
Verify the installation of Python by the command ./python.
#./python
Method 3: Installation
of Python by PPA
PPA (Personal Package Archive) is a third-party repository in Ubuntu that provides access to new versions of software that
aren’t included in standard repositories.
Step 1: Update the
Repository
In the terminal run the following command to update the system.
#sudo apt update
Step 2: Install
supported software
The software-properties-common helps the package manager
to add PPA repositories. Run the following command.
#sudo apt install software-properties-common
Step 3: Add
Deadsnakes PPA
Add deadsnakes PPA to the repository with the following
command.
#sudo add-apt-repository ppa:deadsnakes/ppa
Once again, update the system after adding the
repository.
#sudo apt update
Step 4: Install
Python
The ppa repository has old versions in their database,
you just specify the version that you want to install.
#sudo apt install python3.13
Step 5: Verify the
Installation
Verify installation by running
#python3 --version
How to Create a Virtual
Environment for Python
Above we explained what a virtual environment of Python
is, its importance and benefits. Now we will install the virtual environment if
it is already not installed then create a virtual environment and test it by
installing some packages in this environment.
Step 1: Install
Virtualenv
Before installing a virtualenv, make sure Python is
installed on your system or not. In case of not installed, follow any of the above
methods to install it. To check, run the following command
#python3 --version or python3 -v
Next install virtualenv by running the following
commands. First run
# sudo apt update
The purpose of running this command is to apply the currently
available updates. Now run the sudo apt install python3-virtualenv command.
#sudo apt install python3-virtualenv
You can install virtualenv using pip install virtualenv
to the latest version of virtualenv as an alternate method.
Step 2: Create a New Virtual Environment
The virtualenv is installed, now you can create a new
virtual environment in your desired directory. Just navigate to the directory
where you want to create your environment with this command.
I am going to create this environment in the /opt
directory as the purpose of this directory is to manage such kind of operations
easily.
#cd /opt
Now create a virtual environment with this command
#sudo virtualenv myApp01
Next is to activate this virtual environment with the
following command.
#source virtualenv
myApp01
As you can see the arrow indicating myapp01 means your
virtual environment is created and ready to work in it.
Step 3: Testing of a
Virtual Environment
Now it is ready to work, we install some packages of Python.
To install React, run
#pip install react
Note: Ownership is important to keep in mind when creating a virtual environment; otherwise, an error will occur during package installation due to permission denied. In such a case, give
ownership of the current user to the virtual environment with the chown
command.
#chown –R user:user your virtual_environment.
Step 4: Check
installed Packages
To see the list of installed packages, run this command.
#pip3 list
Step5 Deactivate
Virtual Environment
To deactivate the environment just run the command
deactivate.
#deactivate
That’s all from this tutorial. Keep visiting SeekLinux
for more updates and information.
Post a Comment