How to Install Python on Ubuntu

Python is a powerful and versatile programming language favored by beginner and intermediate developers. Its simplicity, extensive libraries, and vast community resources make it an ideal choice for various projects, from web development and data science to automation and machine learning. Python makes it an excellent language for anyone interested in coding. Python can be installed in Windows systems or Linux distributions.

In this post, we provide a comprehensive guide to installing Python on Ubuntu, a Linux operating system. This post is a gateway for those developers who start their programming journey.

Installing Python in Ubuntu

Before proceeding with the installation, check if Python is already on your Ubuntu system. Open the terminal from above and type the following command:

python --version

If Python is installed, the terminal will display the installed version number. If you receive a “command not found” error message, Python needs to be installed.

Installing Python 3

Python 3 is the current stable release of Python programming language. For this post, we will use Python 3 as the default. There are three primary methods for installing Python 3 on Ubuntu:

  1. Using the Advanced Package Tool (APT):

This is the recommended method for most users. APT provides a simple smooth and convenient way to install and manage software packages, including Python. For this open the terminal and type the following command:

sudo apt install python3

The sudo command grants administrative privileges for the installation. Enter your password when prompted. The system will download and install Python 3.

  1. Using the Personal Package Archive

This method allows you to install specific Python versions, including newer ones. It involves adding a Personal Package Archive (PPA) provided by the Deadsnakes project. For this add the repository with the following command in the terminal:

sudo add-apt-repository ppa:deadsnakes/ppa

After adding the repository, Update the package with the following command:

sudo apt update

After the update, you can install Python 3 along with your desired version. For example, if you want to use the 3.11 version you can type the following command:

sudo apt install python3.11
  1. Installing from source code

You can install Python by visiting its official website and downloading its binary for Ubuntu. You can find the latest version by downloading the compressed file for Ubuntu. After downloading place the folder in the directory and access them from the terminal.

cd ~/
sudo ./configure
make
make test

After building the essentials, Use the APT command to install Python.

Verifying the Installation:

Once the installation is complete, verify it by checking the version again. The terminal should display the installed Python version.

Managing Python in Ubuntu

While Python is now installed, it’s recommended to create a virtual environment for your projects. This isolates your project’s dependencies from the system’s Python environment, preventing conflicts and ensuring clean installations. To create a virtual environment, run the following command:

python3 -m venv my_venv

This creates a virtual environment named my_venv. To activate it, type:

source my_venv/bin/activate

Your terminal prompt will now indicate the active virtual environment. To deactivate it, type:

deactivate

Writing Your First Python Program

Now that Python is installed and your environment is set up, you can write your first Python program! Open a text editor and create a file named hello.py. Inside the file, write the following code:

print("Hello, World!")

Save the file and open a terminal window. Navigate to the directory containing the file and run the following command:

python3 hello.py

You should see the message “Hello, World!” displayed on your screen. Congratulations, you’ve successfully written and run your first Python program!

Additional Configuration

Python comes with a built-in package manager called PIP for installing and managing third-party libraries. Install PIP by running the following command:

sudo apt install python3-pip

If you want to set Python 3.11 as the default type the following command:

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 1

You can also run Python directly from any directory. For this, Add the path to your Python installation directory to the PATH environment variable.

Modifying your shell configuration file

  1. Open your shell configuration file using your preferred text editor. For Bash shells, this is usually ~/.bashrc or ~/.bash_profile.
  2. Add the following line to the end of the file:
export PATH="$PATH:/path/to/python/bin"

Replace /path/to/python/bin with the actual path to your Python installation’s bin directory. For example, if you have Python 3.11 installed, this path might be /usr/bin/python3.11/bin.

  1. Save the file and close the editor.
  2. To apply the changes, source the shell configuration file by running the following command:
source ~/.bashrc

Important Notes

  • You can add multiple directories to your PATH by separating them with colons.
  • Be cautious when modifying your PATH environment variable, as mistakes can lead to unexpected behavior.
  • Consider using virtual environments for your projects to manage dependencies effectively and maintain a clean system environment.

Conclusion

Installing Python on Ubuntu is a straightforward process. By following the steps outlined in this article, you can be up and running with this powerful and versatile programming language in no time. You can explore the various resources available online to enhance your learning and start building projects.

You can install Python on the Linux system. For this, you need a powerful platform to host your Python projects Look no further than Ultahost’s Linux VPS hosting! Our VPS plans offers ensure fast loading times for your applications and easily upgrade your resources as your needs grow. Get started today and enjoy the freedom and flexibility of a VPS!

Related Post

How to Mount SD Card in Linux

In computer language, mount refers to connecting an ext...

How to Remove the Lock Symbol from Files and

The lock symbol primarily indicates restricted or limit...

How to Install Docker on Ubuntu 22.04

Docker on Ubuntu is an open-source platform facilitatin...

How to Check Running Processes in Linux?

In Linux, you can efficiently manage your system by che...

What Is the .bashrc File in Linux?

The .bashrc file is a shell script that runs every time...

How to Setup Passwordless SSH on Linux

Passwordless SSH is a way to log in to a remote server ...

Leave a Comment