Software & AppsOperating SystemLinux

Switching to Python3 as Default in Ubuntu 18.04: A Safe Guide

Ubuntu 16

In this article, we will guide you through the process of safely switching to Python 3 as the default version after upgrading to Ubuntu 18.04. We will explore various methods, including using update-alternatives, updating the /usr/bin/python symlink, and setting an alias in your .bashrc file.

Quick Answer

Switching to Python 3 as the default in Ubuntu 18.04 can be done safely and easily using methods like update-alternatives, updating the /usr/bin/python symlink, or setting an alias in your .bashrc file. However, it’s important to note that some packages and applications may still rely on Python 2, so it’s recommended to ensure compatibility before making any changes to your system’s default Python version.

Why Switch to Python 3?

Python 3 is the future of the Python programming language. It includes several improvements over Python 2, such as simplified syntax, improved string formatting, and enhanced library support. However, Ubuntu 18.04, like many Linux distributions, still uses Python 2 as the default. This is because some packages and applications may still rely on Python 2.

Method 1: Using update-alternatives

The update-alternatives command in Ubuntu is a simple way to manage multiple installations of the same software. Here’s how to use it to switch between Python 2 and Python 3:

  1. Open your terminal and run the following commands:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 20
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 10

The --install parameter is used to add alternatives for a program. The first path after --install (/usr/bin/python) is the master link. The second path is the name of the program (python). The third path is the alternative being added (/usr/bin/python2.7 or /usr/bin/python3.6). The number at the end is the priority level; a higher number means higher priority.

  1. To switch between versions, run:
sudo update-alternatives --config python

The --config parameter allows you to manually select which version of Python to use.

Method 2: Updating the /usr/bin/python Symlink

Another method is to update the symbolic link that /usr/bin/python points to:

  1. Run the following command in the terminal:
sudo ln -sfn /usr/bin/python3.x /usr/bin/python

The ln command creates a new link, -s makes it symbolic, -f forces the creation of the link by removing existing files, and -n treats the target as a normal file if it is a symbolic link to a directory. Replace x with your installed Python 3 version.

Method 3: Setting an Alias in .bashrc

You can also set an alias in your .bashrc file:

  1. Open ~/.bashrc in a text editor.
  2. Add the following line:
alias python='python3.x'

Replace x with your installed Python 3 version.

  1. Save the file and run source ~/.bashrc to apply the changes.

The alias command allows you to launch Python 3 just by typing python in the terminal.

Conclusion

Switching to Python 3 as the default in Ubuntu 18.04 can be done safely and easily using the methods described above. However, it’s important not to remove Python 2 entirely, as some packages may still rely on it. Always ensure compatibility before making any changes to your system’s default Python version.

Why should I switch to Python 3 as the default in Ubuntu 18.04?

Switching to Python 3 as the default in Ubuntu 18.04 is recommended because Python 3 is the future of the Python programming language. It offers several improvements over Python 2, such as simplified syntax, improved string formatting, and enhanced library support.

Can I still use Python 2 after switching to Python 3 as the default?

Yes, you can still use Python 2 after switching to Python 3 as the default. However, it’s important to ensure compatibility with any packages or applications that rely on Python 2 before making any changes.

How can I switch between Python 2 and Python 3 using the `update-alternatives` method?

To switch between Python 2 and Python 3 using the update-alternatives method, you can use the sudo update-alternatives --config python command. This command allows you to manually select which version of Python to use.

How do I update the `/usr/bin/python` symlink to point to Python 3?

To update the /usr/bin/python symlink to point to Python 3, you can use the sudo ln -sfn /usr/bin/python3.x /usr/bin/python command. Replace x with your installed Python 3 version.

How can I set an alias in my `.bashrc` file to launch Python 3?

To set an alias in your .bashrc file to launch Python 3, you can open ~/.bashrc in a text editor, add the line alias python='python3.x' (replace x with your installed Python 3 version), save the file, and run source ~/.bashrc to apply the changes.

Is it safe to remove Python 2 entirely after switching to Python 3 as the default?

It is not recommended to remove Python 2 entirely after switching to Python 3 as the default. Some packages and applications may still rely on Python 2, so it’s important to ensure compatibility before making any changes to your system’s default Python version.

Leave a Comment

Your email address will not be published. Required fields are marked *