Easy Guide to Installing Python on Ubuntu
Python is a versatile and popular programming language, used for everything from web development to data science. Ubuntu, as a leading Linux distribution, is a preferred platform for developers due to its stability and rich repository of software packages. Installing Python on Ubuntu can be a straightforward process, whether you’re setting up a development environment or learning Python for the first time. This guide will walk you through the steps to install Python on Ubuntu, manage multiple Python versions, and set up a virtual environment for your Python projects.
Checking the Current Python Installation
Most Ubuntu releases come with Python pre-installed. To check if Python is already installed on your Ubuntu system and determine its version, open your terminal and type:
python --version
or
python3 --version
If Python is installed, this command will display the version number. Ubuntu systems typically link the python
command to Python 2 and python3
to Python 3.
Installing Python
If Python is not installed, or if you want to install a different version, Ubuntu’s package manager, APT, makes it easy.
Step 1: Updating Package List
First, open your terminal and update your package list to make sure you can download the latest version of the software:
sudo apt update
Step 2: Installing Python 3
Next, install Python 3 by executing the following command:
sudo apt install python3
This will install the default Python 3 package available in Ubuntu’s repositories, which could be Python 3.6, 3.8, or another version, depending on your specific version of Ubuntu.
Managing Multiple Python Versions with pyenv
If you need to manage multiple versions of Python on the same system — for instance, to test your software across different Python environments — pyenv
is an invaluable tool.
Installing pyenv
1. First, install the prerequisite packages:
sudo apt-get install -y build-essential libssl-dev zlib1g-dev \nlibbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \nlibncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python3-openssl git
2. Then, install pyenv
using the curl command:
curl https://pyenv.run | bash
This script will clone the pyenv repository to ~/.pyenv
and add initialization scripts to your shell’s configuration file.
After installation, restart your terminal or reload the shell configuration.
Using pyenv to Install Python Versions
With pyenv, you can install multiple versions of Python. For example, to install Python 3.8.1:
pyenv install 3.8.1
And to set it as the global Python version:
pyenv global 3.8.1
Setting Up a Virtual Environment
Virtual environments allow you to manage separate package installations for different projects. Python 3 comes with the venv
module to create virtual environments.
Creating a Virtual Environment
To create a virtual environment, choose a directory where you want to place it, and run:
python3 -m venv myprojectenv
This will create a directory named myprojectenv
in your current directory with a fresh Python installation.
Activating the Virtual Environment
Before installing packages, you need to activate your environment:
source myprojectenv/bin/activate
Your prompt will change to indicate that you are now operating within the virtual environment. It will show the name of your environment enclosed in parentheses.
Deactivating the Virtual Environment
Once you’re done working within the virtual environment, you can deactivate it by running:
deactivate
Conclusion
Installing Python on Ubuntu is a straightforward process that opens up a vast world of programming possibilities. Whether you’re installing Python for the first time, managing multiple versions with pyenv
, or setting up isolated environments with venv
, you’re well on your way to developing Python applications on one of the most popular operating systems in the technology sphere.
Use Cases:
- Beginner Programmers: Start with the default Python version that comes pre-installed on Ubuntu, and focus on learning the basics of the language.
- Data Scientists: Install multiple versions of Python with
pyenv
to test and run data analysis or machine learning projects across different Python environments. - Web Developers: Utilize
venv
to manage project-specific dependencies, ensuring that your development environment is clean and replicable.
FAQ
How do I switch between different Python versions using pyenv?
Use the pyenv global
command to set the global Python version, or pyenv local
to set the Python version for a specific directory.
Can I use Python 2 and Python 3 simultaneously on Ubuntu?
Yes, you can have both Python 2 and Python 3 installed on your Ubuntu system. Use the python
command for Python 2 and python3
for Python 3.
How do I install pip, Python’s package manager?
Pip is installed automatically with Python versions 3.4 and above. For older versions, you can install pip by running sudo apt install python3-pip
.
What’s the difference between pyenv and venv?
pyenv
is a tool for managing multiple Python versions on your system, whereas venv
creates isolated environments for Python projects, allowing you to manage project-specific dependencies independently.
How can I update Python to a newer version?
To update Python to a newer version, you can use the package manager to install the new version or use pyenv
to manage and switch between multiple versions.
We hope this guide has helped you successfully install Python on Ubuntu. If you have any corrections, comments, questions, or experiences to share, we encourage you to get in touch or leave a comment below. Your input is valuable to us and can help make this resource even better for others.