
In this article, we will guide you through the process of installing Python 3 alongside Python 2.7 on Ubuntu 18.04. This is particularly useful for developers who need to use both versions of Python for different projects.
Checking the Existing Python Installation
Before installing Python 3, it’s important to check if it’s already installed on your system. Ubuntu 18.04 comes with Python 3.6 pre-installed. You can check this by running the following command in your terminal:
python3 --version
This command will display the version of Python 3 installed on your system. If Python 3 is already installed, you can skip the next section.
Installing Python 3
If Python 3 is not installed, you can install it by running the following command in your terminal:
sudo apt install python3
The sudo
command allows you to run commands with administrative privileges. apt
is the package handling utility in Ubuntu. The install
command tells apt
to install the specified package, in this case, Python 3.
After the installation, verify it by running python3 --version
again. It should now display the version number of Python 3.
Setting Up a Virtual Environment
To avoid conflicts between Python 2.7 and Python 3, it’s recommended to set up a virtual environment for Python 3. A virtual environment is a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages.
To set up a virtual environment, first, install the venv
module using the following command:
sudo apt install python3-venv
Then, create a new virtual environment by running:
python3 -m venv myenv
Replace myenv
with the name you want to give to your virtual environment. The -m
option tells Python to run the venv
module as a script.
Activating the Virtual Environment
To start using the virtual environment, you need to activate it. Run the following command:
source myenv/bin/activate
This changes your shell’s environment variables so that running python
will get you this particular version and installation of Python. You will see (myenv)
in your terminal prompt, indicating that you are now working within the virtual environment.
Installing Additional Packages
Within the virtual environment, you can use pip
, the Python package installer, to install additional packages. First, install pip
for Python 3:
sudo apt install python3-pip
Then, you can install any package by running:
pip install packagename
Replace packagename
with the name of the package you want to install.
Deactivating the Virtual Environment
When you’re done working in the virtual environment, you can deactivate it by simply running:
deactivate
This will put you back to the system’s default Python interpreter.
By following these steps, you can successfully install Python 3 alongside Python 2.7 on Ubuntu 18.04 and switch between them as needed. Remember to use python3
instead of python
when you want to run Python 3 code. Happy coding!
Yes, you can have both Python 2.7 and Python 3 installed on Ubuntu 18.04. In fact, Ubuntu 18.04 comes with Python 3.6 pre-installed. You can install Python 2.7 alongside Python 3 by following the steps in this article.
To check if Python 3 is installed on your system, open the terminal and run the command python3 --version
. If Python 3 is installed, it will display the version number. If it is not installed, you can proceed with the installation steps mentioned in the article.
A virtual environment is a self-contained directory that contains a Python installation for a specific version of Python, along with additional packages. Setting up a virtual environment for Python 3 is recommended to avoid conflicts between Python 2.7 and Python 3. It allows you to isolate your Python 3 projects and their dependencies from the system’s default Python environment.
To activate the virtual environment, open the terminal and navigate to the directory where your virtual environment is located. Then, run the command source myenv/bin/activate
, replacing myenv
with the name of your virtual environment. You will see (myenv)
in your terminal prompt, indicating that the virtual environment is active and ready to use.
Once the virtual environment is activated, you can use pip
, the Python package installer, to install additional packages. First, ensure that pip
is installed for Python 3 by running sudo apt install python3-pip
. Then, you can install any package by running pip install packagename
, replacing packagename
with the name of the package you want to install.
To deactivate the virtual environment and return to the system’s default Python interpreter, simply run the command deactivate
in the terminal. This will restore the original environment variables and prompt.